1
0
feed-chat-webhook/feed_chat_webhook/__main__.py

52 lines
1.3 KiB
Python
Raw Normal View History

2024-12-19 12:58:39 +00:00
import requests
2024-12-19 13:25:35 +00:00
import secret_loader
import feedparser
from markdownify import markdownify
2024-12-19 12:58:39 +00:00
2024-12-19 13:25:35 +00:00
SESSION = requests.Session()
2024-12-19 12:58:39 +00:00
2024-12-19 13:25:35 +00:00
secrets = secret_loader.SecretLoader()
2024-12-19 12:58:39 +00:00
WEBHOOK_URL = secrets.load_or_fail('WEBHOOK_URL')
2024-12-19 13:25:35 +00:00
FEED_URL = secrets.load_or_fail('FEED_URL')
2024-12-19 12:58:39 +00:00
2024-12-19 13:25:35 +00:00
def send_feed_notice(text: str):
app_message = {"text": text}
2024-12-19 12:58:39 +00:00
message_headers = {"Content-Type": "application/json; charset=UTF-8"}
response = requests.post(
WEBHOOK_URL,
headers=message_headers,
json=app_message,
)
2024-12-19 13:25:35 +00:00
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()
2024-12-19 12:58:39 +00:00
if __name__ == '__main__':
main()