Advertisement
TankorSmash

Untitled

Jan 20th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.77 KB | None | 0 0
  1. import requests
  2. import json
  3. from pprint import pprint
  4. import datetime
  5. import os
  6.  
  7.  
  8.  
  9. #get json object from imgur gallery. can be appended with /month or /week for
  10. # more recent entries
  11. r = requests.get(r'http://imgur.com/r/scarlettjohansson/top.json')
  12. #creates a python dict from the JSON object
  13. j = json.loads(r.text)
  14.  
  15. #prints the dict, if necessary. Used for debug mainly
  16. #pprint(j)
  17.  
  18. #get the list of images from j['gallery']
  19. image_list = j['gallery']
  20.  
  21. #print the number of images found
  22. print len(image_list), 'images found in the gallery'
  23.  
  24. #debugging, examine the first image in the gallery, confirm no errors
  25. pprint(image_list[0])
  26.  
  27. ## Create a dynamically named folder
  28.  
  29. #get the time object for today
  30. folder = datetime.datetime.today()
  31. #turn it into a printable string
  32. string_folder = str(folder)
  33. #replace some illegal chars
  34. legal_folder = string_folder.replace(':', '.')
  35. #create the folder using the name legal_folder
  36. os.mkdir(str(legal_folder))
  37.  
  38. ## Extract image info from the gallery
  39.  
  40. #list of pairs containing the image name and file extension
  41. image_pairs = []
  42. #extract image and file extension from dict
  43. for image in image_list:
  44.     #get the raw image name
  45.     img_name = image['hash']
  46.     #get the image extension(jpg, gif etc)
  47.     img_ext = image['ext']
  48.     #append pair to list
  49.     image_pairs.append((img_name, img_ext))
  50.  
  51. ## Download images from imgur.com
  52.  
  53. #current image number, for looping limits
  54. current = 0
  55. #run download loop, until DL_LIMIT is reached
  56. for name, ext in image_pairs:
  57.     #so long as we haven't hit the download limit:
  58.     if current < DL_LIMIT:
  59.         #this is the image URL location
  60.         url = r'http://imgur.com/{name}{ext}'.format(name=name, ext=ext)
  61.         #print the image we are currently downloading
  62.         print 'Current image being downloaded:', url
  63.  
  64.         #download the image data
  65.         response = requests.get(url)
  66.         #set the file location
  67.         path = r'./{fldr}/{name}{ext}'.format(fldr=legal_folder,
  68.                                               name=name,
  69.                                               ext=ext)
  70.         #open the file object in write binary mode
  71.         fp = open(path, 'wb')
  72.         #perform the write operation
  73.         fp.write(response.content)
  74.         #close the file
  75.         fp.close()
  76.         #advance the image count
  77.         current += 1
  78.  
  79. #print off a completion string
  80. print 'Finished downloading {cnt} images to {fldr}!'.format(cnt=current,
  81.                                                             fldr=legal_folder)
  82. import requests
  83. import json
  84. from pprint import pprint
  85. import datetime
  86. import os
  87.  
  88. ##Set constants for script
  89.  
  90. DL_LIMIT = 5
  91.  
  92. ##Download and load the JSON information for the Gallery
  93.  
  94. #get json object from imgur gallery. can be appended with /month or /week for
  95. # more recent entries
  96. r = requests.get(r'http://imgur.com/r/scarlettjohansson/top.json')
  97. #creates a python dict from the JSON object
  98. j = json.loads(r.text)
  99.  
  100. #prints the dict, if necessary. Used for debug mainly
  101. #pprint(j)
  102.  
  103. #get the list of images from j['gallery']
  104. image_list = j['gallery']
  105.  
  106. #print the number of images found
  107. print len(image_list), 'images found in the gallery'
  108.  
  109. #debugging, examine the first image in the gallery, confirm no errors
  110. pprint(image_list[0])
  111.  
  112. ## Create a dynamically named folder
  113.  
  114. #get the time object for today
  115. folder = datetime.datetime.today()
  116. #turn it into a printable string
  117. string_folder = str(folder)
  118. #replace some illegal chars
  119. legal_folder = string_folder.replace(':', '.')
  120. #create the folder using the name legal_folder
  121. os.mkdir(str(legal_folder))
  122.  
  123. ## Extract image info from the gallery
  124.  
  125. #list of pairs containing the image name and file extension
  126. image_pairs = []
  127. #extract image and file extension from dict
  128. for image in image_list:
  129.     #get the raw image name
  130.     img_name = image['hash']
  131.     #get the image extension(jpg, gif etc)
  132.     img_ext = image['ext']
  133.     #append pair to list
  134.     image_pairs.append((img_name, img_ext))
  135.  
  136. ## Download images from imgur.com
  137.  
  138. #current image number, for looping limits
  139. current = 0
  140. #run download loop, until DL_LIMIT is reached
  141. for name, ext in image_pairs:
  142.         #this is the image URL location
  143.         url = r'http://imgur.com/{name}{ext}'.format(name=name, ext=ext)
  144.         #print the image we are currently downloading
  145.         print 'Current image being downloaded:', url
  146.  
  147.         #open the file object in write binary mode
  148.         fp = open("urls.txt", 'a')
  149.         #perform the write operation
  150.         fp.writeline(url)
  151.         #close the file
  152.         fp.close()
  153.  
  154. #print off a completion string
  155. print 'Finished downloading {cnt} images to {fldr}!'.format(cnt=current,
  156.                                                             fldr=legal_folder)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement