Skip to content

Commit

Permalink
Move to download images using requests library.
Browse files Browse the repository at this point in the history
More modern, hopefully somewhat faster.
  • Loading branch information
Yaniv Kaul committed Mar 15, 2018
1 parent 802e6d1 commit b831a10
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
33 changes: 20 additions & 13 deletions lago/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
import logging
import os
import posixpath
import requests
import shutil
import urllib
import sys
import urllib

import lockfile

Expand Down Expand Up @@ -158,6 +159,9 @@ def open_url(self, url, suffix='', dest=None):
(full_url, response.code)
)

if dest is None:
return response

meta = response.info()
file_size_kb = int(meta.getheaders("Content-Length")[0]) / 1024
if file_size_kb > 0:
Expand All @@ -166,18 +170,21 @@ def open_url(self, url, suffix='', dest=None):
(file_size_kb, full_url)
)

def report(count, block_size, total_size):
percent = (count * block_size * 100 / float(total_size))
sys.stdout.write(
"\r% 3.1f%%" % percent + " complete (%d " %
(count * block_size / 1024) + "Kilobytes)"
)
sys.stdout.flush()

if dest:
response.close()
urllib.urlretrieve(full_url, dest, report)
sys.stdout.write("\n")
response.close()
one_percent = int(file_size_kb / 100)
streamed = 0
dl_chunk_size_kb = 128
r = requests.get(full_url, stream=True)
with open(dest, 'wb') as f:
for chunk in r.iter_content(chunk_size=dl_chunk_size_kb * 1024):
if chunk:
f.write(chunk)
streamed += dl_chunk_size_kb
if streamed > one_percent:
sys.stdout.write('.')
sys.stdout.flush()
streamed = 0
sys.stdout.write("\n")
return response

def download_image(self, handle, dest):
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ wrapt
Jinja2
xmltodict
netaddr
requests

0 comments on commit b831a10

Please sign in to comment.