Check feed entries before sending
This commit is contained in:
parent
bc47b2505b
commit
59b7f4e392
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user