-
Notifications
You must be signed in to change notification settings - Fork 1
/
random-imgur.py
executable file
·102 lines (83 loc) · 3.43 KB
/
random-imgur.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
import os
import sys
import random
import hashlib
import argparse
import requests
import requests.exceptions
def get_imgur_url():
"""
Builds an imgur url in the form http://i.imgur.com/{5 characters}.jpg.
@return: (tuple) - url (string), file name (string)
"""
imgur_url = "http://i.imgur.com/"
ext = ".jpg"
r1 = random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
r2 = random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
r3 = random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
r4 = random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
r5 = random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
code = r1 + r2 + r3 + r4 + r5
file_name = code
full_url = imgur_url + file_name + ext
return (full_url, file_name)
def is_placeholder_image(img_data):
"""
Checks for the placeholder image. If an imgur url is not valid (such as
http//i.imgur.com/12345.jpg), imgur returns a blank placeholder image.
@param: img_data (bytes) - bytes representing the image.
@return: (boolean) True if placeholder image otherwise False
"""
sha256_placeholder = "9b5936f4006146e4e1e9025b474c02863c0b5614132ad40db4b925a10e8bfbb9"
m = hashlib.sha256()
m.update(img_data)
return m.hexdigest() == sha256_placeholder
def save_image(download_dir, file_name, file_ext, img_data):
"""
Saves an image to the download directory with a given file name and extension.
@param: img_data (bytes) - bytes representing the image.
@param: file_name (string) - name to the save the file as i.e. foo.jpg.
@param: download_dir (string) - path to the download directory.
@return: None
"""
try:
file_path = "{0}{1}{2}.{3}".format(download_dir, os.sep, file_name, file_ext)
with open(file_path, "wb") as f:
f.write(img_data)
except FileNotFoundError as e:
raise e
def download_imgur(url):
"""
Downloads an image from imgur.
@param: url (string) - Imgur url to download i.e. http://i.imgur.com/1a2b3c.jpg.
@return: None if the download fails else images binary data and content type.
"""
try:
r = requests.get(url)
if not r.ok:
return None
sub_type = r.headers["content-type"][6:] # turns image/gif to gif
file_type = "jpg" if sub_type not in ["gif", "webm", "png"] else sub_type
return r.content, file_type
except (exceptions.ConnectionError, exceptions.HTTPError) as e:
pass
def main(max_imgs, download_dir, verbose=False):
count = 0
while count < max_imgs:
imgur_url, file_name = get_imgur_url()
img_data, file_type = download_imgur(imgur_url)
if not img_data or is_placeholder_image(img_data):
continue
save_image(download_dir, file_name, file_type, img_data)
if verbose: print('Downloaded image',imgur_url)
count += 1
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Process command line arguments for " + __file__)
parser.add_argument('--img', type=int, required=True, nargs='?', help='number of images to download.')
parser.add_argument('--dir', type=str, default="downloaded_imgurs", nargs='?', help="download directory.")
parser.add_argument('--verbose', action='store_true', help='print downloaded image urls.')
args = parser.parse_args()
if not os.path.exists(args.dir):
os.mkdir(args.dir)
sys.exit(main(args.img, args.dir, args.verbose))