From 1d8f04124d1eab31b104731a6e672a014b818859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gl=C3=BCpker?= Date: Sun, 16 May 2021 23:20:13 +0200 Subject: Fix twitter references --- twitter.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'twitter.py') diff --git a/twitter.py b/twitter.py index 4c53933..7f2dfae 100755 --- a/twitter.py +++ b/twitter.py @@ -109,7 +109,8 @@ def twitter(user): tweets = [parse_tweet( user, tweet, - response["includes"]["media"], + response.get("includes", {}).get("tweets", []), + response.get("includes", {}).get("media", []), ) for tweet in response["data"]] return { @@ -119,7 +120,7 @@ def twitter(user): 'content': tweets } -def parse_tweet(user, tweet, media): +def parse_tweet(user, tweet, included_tweets, included_media): title = description = tweet["text"] link = "https://twitter.com/" + user + "/status/" + str(tweet["id"]) @@ -129,7 +130,7 @@ def parse_tweet(user, tweet, media): if rt["type"] == "retweeted": rt_info = title[: title.index(":") + 2] ref_tweet = next( - t for t in response["includes"]["tweets"] if t["id"] == rt["id"] + t for t in included_tweets if t["id"] == rt["id"] ) title = rt_info + ref_tweet["text"] description = rt_info + ref_tweet["text"] @@ -150,7 +151,7 @@ def parse_tweet(user, tweet, media): included_media_keys = tweet.get('attachments', {}).get('media_keys', []) for included_media_key in included_media_keys: ref_media = next( - t for t in media if t["media_key"] == included_media_key + t for t in included_media if t["media_key"] == included_media_key ) if 'url' not in ref_media: continue if ref_media.get('type', '') == 'photo': -- cgit v1.2.3