diff --git a/app/get_from_reddit.py b/app/get_from_reddit.py index 84aa88e6..6f9cf741 100644 --- a/app/get_from_reddit.py +++ b/app/get_from_reddit.py @@ -1,9 +1,9 @@ import random import requests -def get_image(boards): +def get_image(boards, nsfw=False): """ - get_image(boards) + 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 @@ -18,15 +18,25 @@ def get_image(boards): 'i.imgur.com', 'i.redd.it', 'i.reddituploads.com', + 'i3.kym-cdn.com', 'imgur.com', 'media.giphy.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'] + request_string = "https://reddit.com/r/{}/random.json".format(boards) - image_urls = list(filter(lambda x: x['data']['domain'] in domains, response)) - return random.choice(image_urls)['data']['url'].replace('http://', 'https://') + 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':'discord dragon-bot'} + ).json()[0]['data']['children'][0] + + if response['data']['domain'] not in domains: + # If we dont find an approved domain, re-try the request + return get_image(boards, nsfw=nsfw) + + return response['data']['url'].replace('http://', 'https://') diff --git a/app/lewds.py b/app/lewds.py index 08a216ff..92060bb4 100644 --- a/app/lewds.py +++ b/app/lewds.py @@ -4,7 +4,7 @@ import requests import get_from_reddit -def get_from_danbooru(boards): +def get_from_danbooru(boards, nsfw=True): """ get_from_danbooru(boards) @@ -61,5 +61,5 @@ def get_lewd(channel_name): ] # This is really bad practice but pass boards to get_from_reddit AND danbooru so it doesnt error - return random.choice([get_from_reddit.get_image, get_from_danbooru])(boards=boards) + return random.choice([get_from_reddit.get_image, get_from_danbooru])(boards=boards, nsfw=True) return 'You can only use this command in NSFW channels'