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 --- netto.py | 37 ++++++----------- rss.py | 52 +++++++++++++----------- twitter.py | 135 ++++++++++++++++++++++++++++--------------------------------- webapp.py | 51 +++++++---------------- zdf.py | 7 +++- 5 files changed, 122 insertions(+), 160 deletions(-) diff --git a/netto.py b/netto.py index fda6409..2867edf 100755 --- a/netto.py +++ b/netto.py @@ -36,34 +36,21 @@ def netto(store_id): year = str(datetime.now().year) title = url[ url.find(year) : url.find(year) + 7 ] - return title, url + return { + title: 'Netto Angebote für ' + store_id, + url: 'https://www.netto-online.de/ueber-netto/Online-Prospekte.chtm/' + store_id, + description: 'PDF der neuen Netto Angebote für den Laden um die Ecke.', + content: [{ + 'title': 'Angebote für ' + title, + 'url': url, + 'content': 'Angebote für ' + title + ' finden sich unter ' + url, + }] + } -def main(store_id = 9110): - url = 'https://www.netto-online.de/ueber-netto/Online-Prospekte.chtm/' + str(store_id) - print(""" - - - Netto Angebote """ + str(store_id) + """ - """ + url + """ - PDF der neuen Netto Angebote für den Laden um die Ecke. - """ + _format_date(datetime.now()) + """""") - - title, link = netto(url) - print(' ') - print(' <![CDATA[Angebote für ' + title + ']]>') - print(' ' + link + '') - # print(' ') - # print(' ' + date + '') - # print(' ') - print(' ') +def main(store_id = 9110): + print(netto(store_id)) - print(' ') - print('') if __name__ == "__main__": - # if len(sys.argv) != 2: - # print('Usage:', sys.argv[0], '') - # sys.exit(1) - # main(sys.argv[1]) main() diff --git a/rss.py b/rss.py index 66ffb35..f11a956 100755 --- a/rss.py +++ b/rss.py @@ -21,36 +21,40 @@ def _format_date(dt): "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][dt.month-1], dt.year, dt.hour, dt.minute, dt.second) -def buildRSS(title, url, description, content): +def buildRSS(feed_data): """ - Feed basic info: title, url, descriptions - Content: List[Dict{title, url, content, date, enclosures, guid}] + feed_data = { + title, url, description, + content = [{ + title, url, content, date, [enclosures], guid + }] + } """ - feed = """ + feed = f""" - """ + title + """ - """ + url + """ - """ + description + """ - """ + _format_date(datetime.now()) + """""" + {feed_data['title']} + {feed_data['url']} + {feed_data['description']} + {_format_date(datetime.now())}""" - for item in content: - feed += ' ' - feed += ' <![CDATA[' + item.get('title', 'N/A') + ']]>' - feed += ' ' + item.get('url', 'N/A') + '' - feed += ' ' - if 'date' in item: - if type(item['date']) is str: - feed += ' ' + item['date'] + '' + for item in feed_data["content"]: + feed += " " + feed += f" <![CDATA[{item.get('title', 'N/A')}]]>" + feed += f" {item.get('url', 'N/A')}" + feed += f" " + if "date" in item: + if type(item["date"]) is str: + feed += f" {item['date']}" else: - feed += ' ' + _format_date(item['date']) + '' - for enclosure in item.get('enclosures', []): - feed += ' ' - if 'guid' in item: - feed += ' ' + item['guid'] + '' - feed += ' ' + feed += f" {_format_date(item['date'])}" + for enclosure in item.get("enclosures", []): + feed += f" " + if "guid" in item: + feed += f" {item['guid']}" + feed += " " - feed += ' ' - feed += '' + feed += " " + feed += "" return feed 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: diff --git a/webapp.py b/webapp.py index b992150..332cfc6 100755 --- a/webapp.py +++ b/webapp.py @@ -35,55 +35,34 @@ def not_found(e): @app.route("/twitter/") def feedTwitter(account): - content = [{'title': t, 'url': u, 'content': c, 'date': d, 'enclosures': e} - for t,c,u,d,e in twitter(account)] - xml = buildRSS( - title = 'Twitter: ' + account, - url = 'https://twitter.com/' + account, - description = 'The latest entries of the twitter account of ' + account, - content = content) + xml = buildRSS(twitter(account)) response = Response(xml, mimetype='text/xml') response.headers['Access-Control-Allow-Origin'] = '*' return response -@app.route("/telegram/") -def feedTelegram(account): - content = [{'title': t, 'url': u, 'content': c, 'date': d} - for t,c,u,d in telegram(account)] - xml = buildRSS( - title = 'Telegram: ' + account, - url = 'https://t.me/s/' + account, - description = 'The latest entries of the telegram channel of ' + account, - content = content) - response = Response(xml, mimetype='text/xml') - response.headers['Access-Control-Allow-Origin'] = '*' - return response +# @app.route("/telegram/") +# def feedTelegram(account): +# content = [{'title': t, 'url': u, 'content': c, 'date': d} +# for t,c,u,d in telegram(account)] +# xml = buildRSS( +# title = 'Telegram: ' + account, +# url = 'https://t.me/s/' + account, +# description = 'The latest entries of the telegram channel of ' + account, +# content = content) +# response = Response(xml, mimetype='text/xml') +# response.headers['Access-Control-Allow-Origin'] = '*' +# return response @app.route("/netto/") def feedNetto(market): - title, url = netto(market) - content = [{ - 'title': 'Angebote für ' + title, - 'url': url, - 'content': 'Angebote für ' + title + ' finden sich unter ' + url, - }] - xml = buildRSS( - title = 'Netto Angebote für ' + market, - url = 'https://www.netto-online.de/ueber-netto/Online-Prospekte.chtm/' + market, - description = 'PDF der neuen Netto Angebote für den Laden um die Ecke.', - content = content) + xml = buildRSS(netto(market)) response = Response(xml, mimetype='text/xml') response.headers['Access-Control-Allow-Origin'] = '*' return response @app.route("/zdf/") def filterZDFFeed(feed): - title, url, description, content = zdf(feed) - xml = buildRSS( - title = title, - url = url, - description = description, - content = content) + xml = buildRSS(zdf(feed)) response = Response(xml, mimetype='text/xml') response.headers['Access-Control-Allow-Origin'] = '*' return response diff --git a/zdf.py b/zdf.py index 37749ac..28373ea 100755 --- a/zdf.py +++ b/zdf.py @@ -68,7 +68,12 @@ def zdf(feed): 'guid': s_guid, }) - return title, url, description, content + return { + "title": title, + "url": url, + "description": description, + "content": content, + } except Exception as exc: logging.error('Working with zdf failed.', exc_info=exc) return None -- cgit v1.2.3