Skip to content

Instantly share code, notes, and snippets.

@dansbecker
Created March 1, 2019 18:26
Show Gist options
  • Save dansbecker/d94de8770bddead4088fd38f2dd3b997 to your computer and use it in GitHub Desktop.
Save dansbecker/d94de8770bddead4088fd38f2dd3b997 to your computer and use it in GitHub Desktop.
import os
import re
import requests
import shutil
def get_url_and_fname(line):
if 'i.imgur.com/' in line:
regex = r"(https?://i.imgur.com/(.*.png))"
elif 'imgur.com' in line:
regex = r"(https?://imgur.com/(.*.png))"
else:
print("No valid imgur url found in {}".format(line))
try:
url, fname = re.findall(regex, line)[0]
except:
import pdb; pdb.set_trace()
return url, fname
def dl_image(url, out_fname):
response = requests.get(url, stream=True)
with open(out_fname, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
os.makedirs('images', exist_ok=True)
with open('urls.txt', 'r') as f:
for line in f:
url, fname = get_url_and_fname(line)
print('Downloading {}'.format(url))
dl_image(url, fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment