diff options
author | André Glüpker <git@wgmd.de> | 2021-07-30 12:52:11 +0200 |
---|---|---|
committer | André Glüpker <git@wgmd.de> | 2021-07-30 12:52:11 +0200 |
commit | a056cdf87b88fcc086d5426880cef5a37661c6e7 (patch) | |
tree | 40e08a3d30483c7645ac96b568b0bcb4a0037a74 /twitter.py | |
parent | 4641d83fcbffead62e8b41c4452f3abc5358c81e (diff) | |
download | rss-feeds-a056cdf87b88fcc086d5426880cef5a37661c6e7.tar.gz rss-feeds-a056cdf87b88fcc086d5426880cef5a37661c6e7.tar.bz2 rss-feeds-a056cdf87b88fcc086d5426880cef5a37661c6e7.zip |
Fetch twitter replies / quoted tweets
Diffstat (limited to 'twitter.py')
-rwxr-xr-x | twitter.py | 34 |
1 files changed, 32 insertions, 2 deletions
@@ -83,6 +83,7 @@ def twitter(user): tweet, response.get("includes", {}).get("tweets", []), response.get("includes", {}).get("media", []), + headers, ) for tweet in response["data"] ] @@ -90,13 +91,13 @@ def twitter(user): return feed -def parse_tweet(user, tweet, included_tweets, included_media): +def parse_tweet(user, tweet, included_tweets, included_media, headers): title = description = tweet["text"] link = "https://twitter.com/" + user + "/status/" + str(tweet["id"]) # Check included re-tweets / replace by Retweet + ref_enclosures = [] for rt in tweet.get("referenced_tweets", []): - if rt["type"] == "retweeted": rt_info = title[: title.index(":") + 2] ref_tweet = next(t for t in included_tweets if t["id"] == rt["id"]) @@ -107,8 +108,14 @@ def parse_tweet(user, tweet, included_tweets, included_media): ) elif rt["type"] == "replied_to": description += "<br/>This was a reply to: " + rt["id"] + text, enclosures = fetch_single_tweet(rt["id"], headers) + description += text + ref_enclosures.extend(enclosures) elif rt["type"] == "quoted": description += "<br/>Quoted tweet: " + rt["id"] + text, enclosures = fetch_single_tweet(rt["id"], headers) + description += text + ref_enclosures.extend(enclosures) else: description += f"<br/><br/>Unknown reference type: {rt['type']}" @@ -129,6 +136,7 @@ def parse_tweet(user, tweet, included_tweets, included_media): description += '<br/><img src="' + ref_media["url"] + '" />' else: enclosures.append(ref_media["url"]) + enclosures.extend(ref_enclosures) # Append Retweets etc description += "<br/><br/>" @@ -150,6 +158,28 @@ def parse_tweet(user, tweet, included_tweets, included_media): } +def fetch_single_tweet(id, headers): + url = f"https://api.twitter.com/2/tweets/{id}?tweet.fields=entities&expansions=attachments.media_keys&media.fields=url" + try: + res = urlopen(Request(url, headers=headers)) + response = json.loads(res.read().decode("UTF-8")) + except Exception as exc: + logging.error("Request to twitter failed (single tweet).", exc_info=exc) + return None + + text = response['data'].get('text', 'no text') + + enclosures = [] + for media in response['data'].get('includes', {}).get('media', []): + if "url" not in media: + continue + if media.get("type", "") == "photo": + text += '<br/><img src="' + media["url"] + '" />' + else: + enclosures.append(media["url"]) + + return text, enclosures + def main(channel): print(twitter(channel)) |