summaryrefslogtreecommitdiff
path: root/twitter.py
diff options
context:
space:
mode:
Diffstat (limited to 'twitter.py')
-rwxr-xr-xtwitter.py72
1 files changed, 20 insertions, 52 deletions
diff --git a/twitter.py b/twitter.py
index 40cd5ac..b7088cd 100755
--- a/twitter.py
+++ b/twitter.py
@@ -12,42 +12,6 @@ import json
bearer = None
-def _format_date(dt):
- """convert a datetime into an RFC 822 formatted date
- Input date must be in GMT.
- Stolen from PyRSS2Gen.
- """
- # Looks like:
- # Sat, 07 Sep 2002 00:00:01 GMT
- # Can't use strftime because that's locale dependent
- #
- # Isn't there a standard way to do this for Python? The
- # rfc822 and email.Utils modules assume a timestamp. The
- # following is based on the rfc822 module.
- return "%s, %02d %s %04d %02d:%02d:%02d GMT" % (
- ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()],
- dt.day,
- [
- "Jan",
- "Feb",
- "Mar",
- "Apr",
- "May",
- "Jun",
- "Jul",
- "Aug",
- "Sep",
- "Oct",
- "Nov",
- "Dec",
- ][dt.month - 1],
- dt.year,
- dt.hour,
- dt.minute,
- dt.second,
- )
-
-
def getBearer():
global bearer
if bearer:
@@ -100,25 +64,28 @@ def twitter(user):
res = urlopen(Request(url, headers=headers))
response = json.loads(res.read().decode("UTF-8"))
except Exception as exc:
- logging.error('Request to twitter failed.', exc_info=exc)
+ 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': []
+ "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 feed
- feed['content'] = [parse_tweet(
+ feed["content"] = [
+ parse_tweet(
user,
tweet,
response.get("includes", {}).get("tweets", []),
response.get("includes", {}).get("media", []),
- ) for tweet in response["data"]]
+ )
+ for tweet in response["data"]
+ ]
return feed
@@ -132,9 +99,7 @@ def parse_tweet(user, tweet, included_tweets, included_media):
if rt["type"] == "retweeted":
rt_info = title[: title.index(":") + 2]
- ref_tweet = next(
- t for t in included_tweets if t["id"] == rt["id"]
- )
+ ref_tweet = next(t for t in included_tweets if t["id"] == rt["id"])
title = rt_info + ref_tweet["text"]
description = rt_info + ref_tweet["text"]
title, description = unshorten_urls(
@@ -143,7 +108,7 @@ def parse_tweet(user, tweet, included_tweets, included_media):
elif rt["type"] == "replied_to":
description += "<br/>This was a reply to:<br/>" + rt["id"]
elif rt["type"] == "quoted":
- description += '<br/>Quoted tweet:<br/>' + rt["text"]
+ description += "<br/>Quoted tweet:<br/>" + rt["text"]
else:
description += f"<br/><br/>Unknown reference type: {rt['type']}"
@@ -153,16 +118,17 @@ def parse_tweet(user, tweet, included_tweets, included_media):
# Attach media
enclosures = []
- included_media_keys = tweet.get('attachments', {}).get('media_keys', [])
+ included_media_keys = tweet.get("attachments", {}).get("media_keys", [])
for included_media_key in included_media_keys:
ref_media = next(
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':
- description += "<br/><img src=\"" + ref_media['url'] + "\" />"
+ 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'])
+ enclosures.append(ref_media["url"])
# Append Retweets etc
description += "<br/><br/>"
@@ -183,9 +149,11 @@ def parse_tweet(user, tweet, included_tweets, included_media):
"enclosures": enclosures,
}
+
def main(channel):
print(twitter(channel))
+
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage:", sys.argv[0], "<twitter channel>")