SHOW:
|
|
- or go back to the newest paste.
1 | #gets 200 random imgur images and saves them to the folder images/ | |
2 | #note that all of them will be saved with the extension .jpg, regardless of the actual format | |
3 | ||
4 | import urllib | |
5 | import random | |
6 | import os | |
7 | ||
8 | def getimgstr(number): | |
9 | alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' | |
10 | res = '' | |
11 | while number: | |
12 | number, i = divmod(number, 62) | |
13 | res = alphabet[i] + res | |
14 | while len(res) < 5: | |
15 | res = '0' + res | |
16 | return res | |
17 | ||
18 | i = 0 | |
19 | while i < 200: | |
20 | st = getimgstr(random.randint(0, 916132831)) #62 ^ 5 - 1 | |
21 | print st | |
22 | urllib.urlretrieve("http://i.imgur.com/" + st + ".jpg", "images/" + st + ".jpg") | |
23 | sz = os.stat("images/" + st + ".jpg").st_size | |
24 | if sz < 1024: #reject invalid images | |
25 | os.remove("images/" + st + ".jpg") | |
26 | print " - invalid" | |
27 | else: | |
28 | i += 1 | |
29 | print " - valid - " + str(sz) + " bytes" |