Created
August 22, 2023 12:53
-
-
Save namob/5a5ae060fcba056d27fbfe912d23cf7c to your computer and use it in GitHub Desktop.
Wallpapers from reddit /r/wallpapers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import re | |
# Set this to somewhere | |
folder = "/Users/namob/Pictures/Wallpapers/" | |
# Change t=day to something else, t=hour, day, week, month, year, all | |
url = "https://www.reddit.com/r/wallpaper/top.json?t=day" | |
headers = {"User-Agent": "RandomWallpaperNamobBot 0.1"} | |
r = requests.get(url, headers=headers) | |
jsondoc = json.loads(r.text) | |
posts = jsondoc["data"]["children"] | |
for post in posts: | |
title = post["data"]["title"] | |
title = title.replace(" ", "_").replace("&", "") | |
title = re.sub("[^0-9a-zA-Z_]+", "", title) | |
if "gallery_data" in post["data"]: | |
galleryitems = post["data"]["gallery_data"]["items"] | |
i = 0 | |
for item in galleryitems: | |
mid = item["media_id"] | |
filext = post["data"]["media_metadata"][mid]["m"].split("/")[-1] | |
url = f"https://i.redd.it/{mid}.{filext}" | |
print(f"URL: {url}, title: {title}, filext: {filext}") | |
r = requests.get(url) | |
open(f"{folder}{title}_{i}.{filext}", "wb").write(r.content) | |
i += 1 | |
else: | |
url = post["data"]["url"] | |
filext = url.split(".")[-1] | |
print(f"URL: {url}, title: {title}, filext: {filext}") | |
r = requests.get(url) | |
open(f"{folder}{title}.{filext}", "wb").write(r.content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment