Created
May 29, 2017 07:19
-
-
Save reddragon/628999730fcbbb20635cdd9fea4d8ea9 to your computer and use it in GitHub Desktop.
Get all of those graduation pics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import urllib2 | |
def normalize_path(path): | |
if path[-1] == '/': | |
path = path[:-1] | |
return path | |
def get_dir_name(path): | |
return normalize_path(path)[-5:] | |
def get_image(url, dirname, filename): | |
try: | |
response = urllib2.urlopen(url) | |
content = response.read() | |
f = open(dirname + "/" + filename, 'w') | |
f.write(content) | |
f.close() | |
return False | |
except urllib2.URLError as e: | |
return True | |
def ensure_dir_exists(dirname): | |
if not os.path.isdir(dirname): | |
os.makedirs(dirname) | |
def get_data_for_path(path): | |
print 'Analyzing path:', path | |
last_fs = path.rfind('/') | |
parent_dir_path = path[:last_fs] | |
parent_dir_name = get_dir_name(parent_dir_path) | |
print 'Parent Dir URL:', parent_dir_path | |
print 'Parent Dir Name:', parent_dir_name | |
last_dot = path.rfind('.') | |
extension = path[last_dot:] | |
print 'Extension:', extension | |
filename = path[last_fs+1:last_dot] | |
filename_format_str = ('%%0%dd' % len(filename)) | |
print 'Filename:', filename | |
print 'Filename Length:', len(filename) | |
print 'Filename Format String: ', filename_format_str | |
print 'Creating directory if necessary' | |
ensure_dir_exists(parent_dir_name) | |
print 'Beginning fetching images' | |
errored = False | |
cur_filenum = 1 | |
downloaded_fine = 0 | |
while not errored: | |
cur_file = (filename_format_str % cur_filenum) + extension | |
cur_path = parent_dir_path + '/' + cur_file | |
print 'Will try file path:', cur_path | |
errored = get_image(cur_path, parent_dir_name, cur_file) | |
if errored: | |
print 'There was an error fetching the file', cur_file, 'ending loop' | |
break | |
else: | |
print cur_file, ': OK' | |
downloaded_fine = downloaded_fine + 1 | |
cur_filenum = cur_filenum + 1 | |
print 'Downloaded', downloaded_fine, 'images.' | |
if len(sys.argv) != 2: | |
print 'Usage: python script.py path_to_any_image' | |
print 'Example: python script.py http://images.partypics.com/events/26615859/00004/0008.jpg' | |
else: | |
path = sys.argv[1] | |
get_data_for_path(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment