summaryrefslogtreecommitdiff
path: root/twitter.py
diff options
context:
space:
mode:
authorAndré Glüpker <git@wgmd.de>2021-05-09 20:13:08 +0200
committerAndré Glüpker <git@wgmd.de>2021-05-09 20:23:18 +0200
commit93f3613dab38125d9ae7d5e74498c5395ac80ab0 (patch)
tree4f1a80138a83c98348464f1b9ddd949766441488 /twitter.py
parent5774dbfb2caa42cb55bafab98a40e47f395e44d9 (diff)
downloadrss-feeds-93f3613dab38125d9ae7d5e74498c5395ac80ab0.tar.gz
rss-feeds-93f3613dab38125d9ae7d5e74498c5395ac80ab0.tar.bz2
rss-feeds-93f3613dab38125d9ae7d5e74498c5395ac80ab0.zip
Unify method returns / return RSS object
Diffstat (limited to 'twitter.py')
-rwxr-xr-xtwitter.py135
1 files changed, 61 insertions, 74 deletions
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"<br/><br/>This was a reply to {rt['id']}"
+ else:
+ description += f"<br/><br/>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 += "<br/><img src=\"" + ref_media['url'] + "\" />"
- else:
- enclosures.append(ref_media['url'])
-
- # Append Retweets etc
- description += "<br/><br/>"
- 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 += "<br/>"
- 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(
- """<?xml version="1.0" encoding="UTF-8"?>
-<rss version="2.0">
- <channel>
- <title>Twitter: """
- + channel
- + """</title>
- <link>https://twitter.com/"""
- + channel
- + """</link>
- <description>The latest entries of the twitter account of """
- + channel
- + """</description>
- <lastBuildDate>"""
- + _format_date(datetime.now())
- + """</lastBuildDate>"""
+ title, description = unshorten_urls(
+ title, description, tweet.get("entities", {}).get("urls", [])
)
- for title, description, link, date, enclosures in twitter(channel):
- print(" <item>")
- print(" <title><![CDATA[" + title + "]]></title>")
- print(" <link>" + link + "</link>")
- print(" <description><![CDATA[" + description + "]]></description>")
- print(" <pubDate>" + _format_date(date) + "</pubDate>")
- for enclosure in enclosures:
- print(' <media:content url="' + enclosure + '" />')
- print(" </item>")
-
- print(" </channel>")
- print("</rss>")
+ # 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 += "<br/><img src=\"" + ref_media['url'] + "\" />"
+ else:
+ enclosures.append(ref_media['url'])
+
+ # Append Retweets etc
+ description += "<br/><br/>"
+ 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 += "<br/>"
+ 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: