Skip to content

Instantly share code, notes, and snippets.

@jermenkoo
Created May 27, 2012 11:43
Show Gist options
  • Save jermenkoo/2809178 to your computer and use it in GitHub Desktop.
Save jermenkoo/2809178 to your computer and use it in GitHub Desktop.
imgur random image downloader
#imports
import os
import random
import string
import urllib.request
#variables
BASE_URL = "http://imgur.com/"
CHARSET = string.ascii_letters + string.digits
DIR_PATH = "images/"
#lambda
gen_alpha = lambda: ''.join(random.sample(CHARSET, 5))
gen_url = lambda suffix: ''.join(BASE_URL + suffix)
#functions
def main():
CURR_SUFF = gen_alpha()
print(CURR_SUFF, sep = '', end = '')
CURR_URL = gen_url(CURR_SUFF) + ".jpg"
if not os.path.exists(DIR_PATH):
os.makedirs(DIR_PATH)
urllib.request.urlretrieve(CURR_URL, DIR_PATH + CURR_SUFF + ".jpg")
if os.stat(DIR_PATH + CURR_SUFF + ".jpg").st_size < 1024:
os.remove(DIR_PATH + CURR_SUFF + ".jpg")
print(" - invalid, was removed :(")
else: print(" - valid, was downloaded :)")
if __name__ == '__main__':
for i in range(int(input("How many images should I try to download? "))):
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment