diff --git a/feed_chat_webhook/__main__.py b/feed_chat_webhook/__main__.py index 6b58081..faa9ac6 100644 --- a/feed_chat_webhook/__main__.py +++ b/feed_chat_webhook/__main__.py @@ -1,21 +1,51 @@ -from json import dumps import requests -import secrets_loader +import secret_loader +import feedparser +from markdownify import markdownify -secrets = secrets_loader.SecretsLoader() +SESSION = requests.Session() +secrets = secret_loader.SecretLoader() WEBHOOK_URL = secrets.load_or_fail('WEBHOOK_URL') +FEED_URL = secrets.load_or_fail('FEED_URL') -def main(): - """Google Chat incoming webhook quickstart.""" - app_message = {"text": "Hello from a Python script!"} +def send_feed_notice(text: str): + app_message = {"text": text} message_headers = {"Content-Type": "application/json; charset=UTF-8"} response = requests.post( WEBHOOK_URL, headers=message_headers, json=app_message, ) - print(response) + +def get_feed(): + response = SESSION.get(FEED_URL) + return feedparser.parse(response.text) + +def get_seen_entry_ids() -> set[str]: + with open('output/entries_db.txt') as f: + return f.read().split('\n') + +def mark_as_sent(entry_id: str): + with open('output/entries_db.txt', 'a') as f: + f.write(entry_id) + f.write('\n') + + +def check_for_new_updates(): + seen_entries = get_seen_entry_ids() + feed = get_feed() + for entry in reversed(feed.entries): + if entry.id in seen_entries: + continue + + msg = f'Feed <{feed.feed.link}|{feed.feed.title}> updated: <{entry.link}|{entry.title}>' + send_feed_notice(msg) + mark_as_sent(entry.id) + + +def main(): + check_for_new_updates() if __name__ == '__main__': main()