From 4b0a75b85d1892e04230de828fe09b90d4f73f09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gl=C3=BCpker?= Date: Thu, 29 Jul 2021 10:44:00 +0200 Subject: Handle empty twitter feeds --- twitter.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'twitter.py') diff --git a/twitter.py b/twitter.py index 7f2dfae..87dfc68 100755 --- a/twitter.py +++ b/twitter.py @@ -103,22 +103,25 @@ def twitter(user): logging.error('Request to twitter failed.', exc_info=exc) return None + feed = { + 'title': 'Twitter: ' + user, + 'url': 'https://twitter.com/' + user, + 'description': 'The latest entries of the twitter account of ' + user, + 'content': [] + } + if not response["meta"]["result_count"]: - return [] + return feed - tweets = [parse_tweet( + feed['content'] = [parse_tweet( user, tweet, response.get("includes", {}).get("tweets", []), response.get("includes", {}).get("media", []), ) for tweet in response["data"]] - return { - 'title': 'Twitter: ' + user, - 'url': 'https://twitter.com/' + user, - 'description': 'The latest entries of the twitter account of ' + user, - 'content': tweets - } + return feed + def parse_tweet(user, tweet, included_tweets, included_media): title = description = tweet["text"] -- cgit v1.2.3