31 lines
831 B
Python
31 lines
831 B
Python
import random
|
|
import requests
|
|
|
|
def get_image(boards):
|
|
"""
|
|
get_image(boards)
|
|
|
|
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
|
|
"""
|
|
|
|
if isinstance(boards, list):
|
|
boards = random.choice(boards)
|
|
|
|
domains = [
|
|
'cdn.awwni.m',
|
|
'i.imgur.com',
|
|
'i.redd.it',
|
|
'i.reddituploads.com',
|
|
'imgur.com',
|
|
'my.mixtape.moe',
|
|
]
|
|
|
|
response = requests.get(
|
|
"https://reddit.com/r/{}.json?limit=500".format(boards),
|
|
headers = {'User-agent':'discord dragon-bot'}
|
|
).json()['data']['children']
|
|
|
|
image_urls = list(filter(lambda x: x['data']['domain'] in domains, response))
|
|
return random.choice(image_urls)['data']['url'].replace('http://', 'https://')
|