1
0

Check feed entries before sending
Some checks failed
Verify Python project can be installed, loaded and have version checked / Test (push) Waiting to run
Run Python tests (through Pytest) / Test (push) Has been cancelled

This commit is contained in:
Jon Michael Aanes 2024-12-19 14:25:35 +01:00
parent bc47b2505b
commit 59b7f4e392

View File

@ -1,21 +1,51 @@
from json import dumps
import requests 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') WEBHOOK_URL = secrets.load_or_fail('WEBHOOK_URL')
FEED_URL = secrets.load_or_fail('FEED_URL')
def main(): def send_feed_notice(text: str):
"""Google Chat incoming webhook quickstart.""" app_message = {"text": text}
app_message = {"text": "Hello from a Python script!"}
message_headers = {"Content-Type": "application/json; charset=UTF-8"} message_headers = {"Content-Type": "application/json; charset=UTF-8"}
response = requests.post( response = requests.post(
WEBHOOK_URL, WEBHOOK_URL,
headers=message_headers, headers=message_headers,
json=app_message, 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__': if __name__ == '__main__':
main() main()