dragon-bot/app/get_from_reddit.py
Luke R 2ba286a770
All checks were successful
Build and push / changes (push) Successful in 4s
Build and push / Lint-Python (push) Successful in 34s
Build and push / Build-and-Push-Docker (push) Successful in 1m45s
Build and push / sync-argocd-app (push) Successful in 8s
good enough, fuckin wwith the get_from_reddit stuff mainly for sheebs
2024-10-24 14:19:12 -07:00

56 lines
1.4 KiB
Python
Executable File

import random
import string
import requests
def get_image(boards, nsfw=False):
"""
get_image(boards, nsfw=False)
Returns a URL to an image on reddit from a random board in the boards list
as long as it is hosted on one of the domains in the domains list
"""
domains = [
"cdn.awwni.m",
"gfycat.com",
"i.imgur.com",
"i.redd.it",
"i.reddituploads.com",
"i3.kym-cdn.com",
"imgur.com",
"media.giphy.com",
"v.redd.it",
"reddit.com",
]
# Spoof our user agent with each request so we dont get rate limited
random_user_agent = "".join(
random.SystemRandom().choice(string.ascii_uppercase + string.digits)
for _ in range(5)
)
if isinstance(boards, list):
boards = random.choice(boards)
request_string = f"https://reddit.com/r/{boards}/random.json"
if not nsfw:
# Append this header to the request. Tells the API to only return SFW results
request_string += "?obey_over18=true"
response = requests.get(
request_string, headers={"User-agent": random_user_agent}
).json()[0]["data"]["children"][0]["data"]
import pprint
pprint.pprint(response, indent=2)
image_url = "https://rxddit.com" + response["permalink"]
if "youtu.be" in response["url"]:
image_url = response["url"]
return image_url