Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Earthporndownloader.py
- # Downloads the top images from reddit.com/r/earthporn and saves them to a wallpapers folder
- import requests
- import json
- import sys
- import uuid
- import re
- from PIL import Image
- import os.path
- import os
- if hasattr(sys, 'real_prefix'):
- print("in venv")
- WALLPAPER_PATH = os.path.dirname(os.path.realpath(__file__))
- req = requests.get("https://www.reddit.com/r/earthporn.json",
- headers = {'User-agent' : 'Your Bot 0.1'})
- if req.status_code != 200:
- print("Error downloading the earthporn.json. Exiting")
- sys.exit()
- js = json.loads(req.text)
- children_array = js['data']['children']
- downloaded_ids = []
- # Load downloaded IDs
- with open('downloaded_images.txt', 'r') as downloaded_images_file :
- for id in downloaded_images_file:
- # Loads as array, remove line endings to make it cleaner/easier to match
- id = id.replace('\n', '').replace('\r', '')
- downloaded_ids.append(id)
- # Now open the file to append
- downloaded_images_file = open('downloaded_images.txt', 'a')
- path = os.path.abspath(WALLPAPER_PATH)
- for post in children_array:
- url = post['data']['url']
- id = re.match(r"https?://.*/(.*).jpg", url)
- # Check if an http url is found
- if id:
- id = id.group(1)
- # IF Image has not been downloaded before, download it now
- # Catches issues where you would download duplicates
- if(id not in downloaded_ids):
- downloaded_images_file.write(id + "\n")
- filename = os.path.join(path, str(uuid.uuid4()) + ".jpg")
- image_request = requests.get(url)
- with open(filename, 'wb') as file:
- file.write(image_request.content)
- # TODO filter by width/height/some other attribute to pick only the high quality monitors that look good for a background
- im = Image.open(filename)
- width, height = im.size
- downloaded_images_file.close()
Add Comment
Please, Sign In to add comment