From 47c2010f7fe55191ed6a77d926b235a08bafd881 Mon Sep 17 00:00:00 2001 From: blank X Date: Fri, 4 Dec 2020 11:44:50 +0700 Subject: [PATCH] Fix KeyError: 'fallback_url' --- redditbot.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/redditbot.py b/redditbot.py index a962db5..cdf3147 100644 --- a/redditbot.py +++ b/redditbot.py @@ -293,7 +293,8 @@ async def main(): url = None break else: - url = reddit_video['fallback_url'] + if 'fallback_url' in reddit_video: + url = reddit_video['fallback_url'] elif getattr(random_post, 'is_gallery', None): files = [] captions = [] @@ -352,12 +353,15 @@ async def main(): gfyitem = apidata.get('gfyItem') if gfyitem: url = gfyitem.get('mp4Url', url) - elif random_post.is_reddit_media_domain and splitted[1] == '.gif' and preview: - preview = preview['images'][0]['variants'] - for i in ('mp4', 'gif'): - if i in preview: - url = preview[i]['source']['url'] - break + elif random_post.is_reddit_media_domain and preview: + ppreview = preview['images'][0] + if splitted[1] == '.gif': + for i in ('mp4', 'gif'): + if i in ppreview['variants']: + url = ppreview['variants'][i]['source']['url'] + break + elif random_post.is_video: + url = ppreview['source']['url'] if url: url = urlunparse(urlparse(url, 'https')) await _download_file(filename, url)