Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix download.py to work without global variables #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions download.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def load_urls(urls_file):
return iurls


def download_and_check_image(iurl):
index, url = iurl
def download_and_check_image(params):
images_dir, index, url, force = params
save_file = join(images_dir, '%08d.jpg' % index)
try:
if args.force or not os.path.isfile(save_file):
if force or not os.path.isfile(save_file):
# Load into bytes
img_data = urllib.request.urlopen(url).read()

Expand All @@ -61,10 +61,7 @@ def download_and_check_image(iurl):
open(save_file, 'wb').write(img_data)
img.close()
else:
global redownloading_warning
if not redownloading_warning:
print('[WARN] Some images already exist, and are not being redownloaded. Use --force to redownload these')
redownloading_warning = True
print('[WARN] Some images already exist, and are not being redownloaded. Use --force to redownload these')
except KeyboardInterrupt:
print('KeyboardInterrupt: Exiting early!')
sys.exit(130) # Avoid humungous backtraces when ctrl+c is pressed
Expand Down Expand Up @@ -101,12 +98,14 @@ def download_and_check_image(iurl):

pool = multiprocessing.Pool(args.threads)

zip_params = [(images_dir, *iurl, args.force) for iurl in iurls]

if tqdm: # Use tqdm progressbar
bar = tqdm(total=len(iurls))
for i, _ in enumerate(pool.imap_unordered(download_and_check_image, iurls)):
for i, _ in enumerate(pool.imap_unordered(download_and_check_image, zip_params)):
bar.update()
else: # Use a basic status print
for i, _ in enumerate(pool.imap_unordered(download_and_check_image, iurls)):
for i, _ in enumerate(pool.imap_unordered(download_and_check_image, zip_params)):
print('Download images: %7d / %d Done' % (i + 1, len(iurls)), end='\r', flush=True)
print()

Expand Down