Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #gets 200 random imgur images and saves them to the folder images/
- #note that all of them will be saved with the extension .jpg, regardless of the actual format
- import urllib
- import random
- import os
- def getimgstr(number):
- alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
- res = ''
- while number:
- number, i = divmod(number, 62)
- res = alphabet[i] + res
- while len(res) < 5:
- res = '0' + res
- return res
- i = 0
- while i < 200:
- st = getimgstr(random.randint(0, 916132831)) #62 ^ 5 - 1
- print st
- urllib.urlretrieve("http://i.imgur.com/" + st + ".jpg", "images/" + st + ".jpg")
- sz = os.stat("images/" + st + ".jpg").st_size
- if sz < 1024: #reject invalid images
- os.remove("images/" + st + ".jpg")
- print " - invalid"
- else:
- i += 1
- print " - valid - " + str(sz) + " bytes"
Add Comment
Please, Sign In to add comment