Advertisement
Guest User

ImgurAlbumDownload

a guest
Apr 22nd, 2023
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. '''
  2.  
  3. Downloading images from imgur album
  4. Get the json data, and from that we can find the number of images in the album
  5. Finds the image type as well (jpg or png) so we dont have to hardcode it
  6.  
  7. Saves all the images into the a folder with the name of the album code
  8. e.g The 615 images in "https://imgur.com/gallery/oEX2D" will be saved in a folder
  9. called 'oEX2D', and numbered sequentially as 'image0.type', 'image1.type', ...
  10.  
  11. '''
  12.  
  13.  
  14. import requests
  15. import json
  16. import re
  17. import os
  18. import time
  19.  
  20.  
  21. def getJsonData(imgur_album_url):
  22.     print('Getting json data from', imgur_album_url, '...')
  23.     json_url = imgur_album_url + '.json'
  24.  
  25.     page_data = requests.get(json_url)
  26.     json_data = page_data.text
  27.  
  28.     print('Data retrieved.\n')
  29.  
  30.     return json_data
  31.  
  32.  
  33. def getAlbumSize(json_data):
  34.     print('Getting number of images in album.')
  35.     album_size = 0
  36.  
  37.     data = json.loads(json_data)
  38.     album_size = data['data']['image']['num_images']
  39.  
  40.     print('There are', album_size, 'images in the album.\n')
  41.  
  42.     return album_size
  43.  
  44.  
  45. # Downloading an image from an url and choosing the name to be downloaded as
  46. def downloadImageFromUrl(image_url, file_name):
  47.     with open(file_name, 'wb') as handle:
  48.         response = requests.get(image_url, stream=True)
  49.  
  50.         for block in response.iter_content(1024):
  51.             handle.write(block)
  52.  
  53.  
  54.  
  55. def main():
  56.     json_data = getJsonData('https://imgur.com/gallery/oEX2D')
  57.  
  58.     album_size = getAlbumSize(json_data)
  59.  
  60.     data = json.loads(json_data)
  61.  
  62.     image_type = data['data']['image']['ext']
  63.     list_images = data['data']['image']['album_images']['images']
  64.     album_code = data['data']['image']['hash']
  65.  
  66.     print('Creating folder for images.')
  67.     os.mkdir(album_code)
  68.     print('Folder', album_code, 'created.\n')
  69.    
  70.  
  71.     print('Downloading images ...')
  72.     for i, image_info in enumerate(list_images):
  73.         image_code = image_info['hash']
  74.  
  75.         image_url = 'https://i.imgur.com/' + image_code + image_type
  76.        
  77.  
  78.         file_name = album_code + '/image' + str(i) + image_type
  79.         downloadImageFromUrl(image_url, file_name)
  80.  
  81.         print('Downloaded image from ', image_url, 'as', file_name)
  82.  
  83.         time.sleep(0.3)
  84.  
  85.  
  86. if __name__ == "__main__":
  87.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement