From 93f3613dab38125d9ae7d5e74498c5395ac80ab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gl=C3=BCpker?= Date: Sun, 9 May 2021 20:13:08 +0200 Subject: Unify method returns / return RSS object --- twitter.py | 135 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 61 insertions(+), 74 deletions(-) (limited to 'twitter.py') diff --git a/twitter.py b/twitter.py index 5ddf8ad..4c53933 100755 --- a/twitter.py +++ b/twitter.py @@ -106,92 +106,79 @@ def twitter(user): if not response["meta"]["result_count"]: return [] - for tweet in response["data"]: - title = tweet["text"] - description = tweet["text"] - link = "https://twitter.com/" + user + "/status/" + str(tweet["id"]) - - # Check included tweets - if ( - "referenced_tweets" in tweet - and len(tweet["referenced_tweets"]) == 1 - and tweet["referenced_tweets"][0]["type"] == "retweeted" - ): + tweets = [parse_tweet( + user, + tweet, + response["includes"]["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 + } + +def parse_tweet(user, tweet, media): + title = description = tweet["text"] + link = "https://twitter.com/" + user + "/status/" + str(tweet["id"]) + + # Check included re-tweets / replace by Retweet + for rt in tweet.get("referenced_tweets", []): + + if rt["type"] == "retweeted": rt_info = title[: title.index(":") + 2] - ref_id = tweet["referenced_tweets"][0]["id"] ref_tweet = next( - t for t in response["includes"]["tweets"] if t["id"] == ref_id + t for t in response["includes"]["tweets"] if t["id"] == rt["id"] ) title = rt_info + ref_tweet["text"] description = rt_info + ref_tweet["text"] title, description = unshorten_urls( title, description, ref_tweet.get("entities", {}).get("urls", []) ) + elif rt["type"] == "replied_to": + description += f"

This was a reply to {rt['id']}" + else: + description += f"

Unknown reference type: {rt['type']}" - title, description = unshorten_urls( - title, description, tweet.get("entities", {}).get("urls", []) - ) - - # Attach media - enclosures = [] - medias = tweet.get('attachments', {}).get('media_keys', []) - for media in medias: - ref_media = next( - t for t in response["includes"]["media"] if t["media_key"] == media - ) - if 'url' not in ref_media: continue - if ref_media.get('type', '') == 'photo': - description += "
" - else: - enclosures.append(ref_media['url']) - - # Append Retweets etc - description += "

" - description += str(tweet["public_metrics"]["retweet_count"]) + " Retweets, " - description += str(tweet["public_metrics"]["like_count"]) + " Likes, " - description += str(tweet["public_metrics"]["reply_count"]) + " Replies, " - description += str(tweet["public_metrics"]["quote_count"]) + " Quotes" - description += "
" - description += "Source: " + tweet["source"] - - date = datetime.strptime(tweet["created_at"], "%Y-%m-%dT%H:%M:%S.%fZ") - - - yield title, description, link, date, enclosures - - -def main(channel): - print( - """ - - - Twitter: """ - + channel - + """ - https://twitter.com/""" - + channel - + """ - The latest entries of the twitter account of """ - + channel - + """ - """ - + _format_date(datetime.now()) - + """""" + title, description = unshorten_urls( + title, description, tweet.get("entities", {}).get("urls", []) ) - for title, description, link, date, enclosures in twitter(channel): - print(" ") - print(" <![CDATA[" + title + "]]>") - print(" " + link + "") - print(" ") - print(" " + _format_date(date) + "") - for enclosure in enclosures: - print(' ') - print(" ") - - print(" ") - print("") + # Attach media + enclosures = [] + 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 + ) + if 'url' not in ref_media: continue + if ref_media.get('type', '') == 'photo': + description += "
" + else: + enclosures.append(ref_media['url']) + + # Append Retweets etc + description += "

" + description += str(tweet["public_metrics"]["retweet_count"]) + " Retweets, " + description += str(tweet["public_metrics"]["like_count"]) + " Likes, " + description += str(tweet["public_metrics"]["reply_count"]) + " Replies, " + description += str(tweet["public_metrics"]["quote_count"]) + " Quotes" + description += "
" + description += "Source: " + tweet["source"] + + date = datetime.strptime(tweet["created_at"], "%Y-%m-%dT%H:%M:%S.%fZ") + + return { + "title": title, + "url": link, + "content": description, + "date": date, + "enclosures": enclosures, + } +def main(channel): + print(twitter(channel)) if __name__ == "__main__": if len(sys.argv) != 2: -- cgit v1.2.3