From b526cc68929250a7f71ff21ed8410ffd8db87a9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gl=C3=BCpker?= Date: Thu, 29 Jul 2021 10:48:55 +0200 Subject: Format using black --- twitter.py | 72 +++++++++++++++++--------------------------------------------- 1 file changed, 20 insertions(+), 52 deletions(-) (limited to 'twitter.py') 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 += "
This was a reply to:
" + rt["id"] elif rt["type"] == "quoted": - description += '
Quoted tweet:
' + rt["text"] + description += "
Quoted tweet:
" + rt["text"] else: description += f"

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 += "
" + if "url" not in ref_media: + continue + if ref_media.get("type", "") == "photo": + description += '
' else: - enclosures.append(ref_media['url']) + enclosures.append(ref_media["url"]) # Append Retweets etc description += "

" @@ -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], "") -- cgit v1.2.3