From e090b368d5feed0f92d17944d171c19eb6b6e6bb Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Wed, 7 Mar 2018 12:49:08 -0800 Subject: [PATCH] Fixing the way we retrieve images from danbooru --- app/lewds.py | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/app/lewds.py b/app/lewds.py index f2806f2b..08a216ff 100644 --- a/app/lewds.py +++ b/app/lewds.py @@ -10,11 +10,36 @@ def get_from_danbooru(boards): returns a URL to an image on danbooru """ - file_path = requests.get( - 'https://danbooru.donmai.us/posts/random.json?tags=rating%3Aexplicit' - ).json()['large_file_url'] - return "https://danbooru.donmai.us{}".format(file_path) + request = requests.get( + 'https://danbooru.donmai.us/posts/random.json?tags=rating%3Aexplicit' + ).json() + + # List of tags we dont want images from + undesired_tags = [ + 'bestiality', + 'pee', + 'futa', + 'futanari', + 'yaoi' + ] + # If any undesired tags show up in the request, try again + if not set(request['tag_string'].split()).isdisjoint(undesired_tags): + return get_from_danbooru(boards) + + # Return the large firl url if available + if 'large_file_url' in request.keys(): + if request['large_file_url'].startswith('/data/'): + return "https://danbooru.donmai.us{}".format(request['large_file_url']) + return request['large_file_url'] + + if 'file_url' in request.keys(): + if request['file_url'].startswith('/data/'): + return "https://danbooru.donmai.us{}".format(request['file_url']) + return request['file_url'] + + return get_from_danbooru(boards) + def get_lewd(channel_name): if 'nsfw' in channel_name: