118 lines
3.5 KiB
Python
118 lines
3.5 KiB
Python
|
from datetime import datetime
|
||
|
import daemon
|
||
|
import requests
|
||
|
import time
|
||
|
from time import sleep
|
||
|
import os
|
||
|
import re
|
||
|
from bs4 import BeautifulSoup
|
||
|
import dotenv
|
||
|
|
||
|
CONFIG = dotenv.dotenv_values(".env")
|
||
|
|
||
|
|
||
|
def read_known() -> list:
|
||
|
known: list = []
|
||
|
try:
|
||
|
with open(CONFIG['STORAGE_FILE'], 'r+') as f:
|
||
|
known = f.read().splitlines()
|
||
|
f.close()
|
||
|
|
||
|
except Exception as e:
|
||
|
print("Could not initialize known titles: %s" % e)
|
||
|
|
||
|
print("="*120 + "\nLoading from file\n" + "="*120)
|
||
|
for item, index in zip(known, range(0, len(known))):
|
||
|
print(f"{index}:\t{item}")
|
||
|
|
||
|
print("="*120)
|
||
|
return known
|
||
|
|
||
|
def daemon():
|
||
|
|
||
|
try:
|
||
|
while True:
|
||
|
r = requests.get(CONFIG['URL'])
|
||
|
soup = BeautifulSoup(r.content, features='xml')
|
||
|
articles = soup.findAll('item')
|
||
|
article_list: list = []
|
||
|
for a in articles:
|
||
|
title: str = str(a.find('title')).replace("<title>", "").replace("</title>", "")
|
||
|
desc: str = str(a.find('description')).replace("<description>", "").replace("</description>", "")
|
||
|
link: str = str(a.find('enclosure'))
|
||
|
link = re.sub(r"<.*>", "", link)
|
||
|
dur: str = str(a.find('duration')).replace("<duration>", "").replace("</duration>", "")
|
||
|
pub: str = str(a.find('pubDate')).replace("<pubDate>", "").replace("</pubDate>", "")
|
||
|
article = {
|
||
|
'title': title,
|
||
|
'desc': desc,
|
||
|
'link': link,
|
||
|
'dur': dur,
|
||
|
'pub': pub
|
||
|
}
|
||
|
article_list.append(article)
|
||
|
|
||
|
known: list = read_known()
|
||
|
for a in article_list:
|
||
|
if a['title'] in known:
|
||
|
print("found known: %s" % a['title'])
|
||
|
|
||
|
else:
|
||
|
print("found unknown: %s" % a['title'])
|
||
|
|
||
|
title = a['title']
|
||
|
desc = a['desc']
|
||
|
link = a['link']
|
||
|
dur = a['dur']
|
||
|
pub = a['pub']
|
||
|
|
||
|
content = f"""
|
||
|
=============== Neue Folge ===============
|
||
|
Titel: {title}
|
||
|
|
||
|
Beschreibung:
|
||
|
{desc}
|
||
|
|
||
|
Link: {link}
|
||
|
Datum: {pub}
|
||
|
Länge: {dur}
|
||
|
==========================================
|
||
|
"""
|
||
|
|
||
|
print(content)
|
||
|
|
||
|
#notify = requests.post(
|
||
|
# url=WEBHOOK,
|
||
|
# json={
|
||
|
# "content": content
|
||
|
# }
|
||
|
# )
|
||
|
#print(notify.text)
|
||
|
|
||
|
known.append(a['title'])
|
||
|
|
||
|
with open(CONFIG['STORAGE_FILE'], 'w+') as file:
|
||
|
try:
|
||
|
for item in known:
|
||
|
file.write(item + '\n')
|
||
|
except Exception as e:
|
||
|
print("Could not write to file: %s" % e)
|
||
|
pass
|
||
|
finally:
|
||
|
file.close()
|
||
|
|
||
|
time.sleep(0.5)
|
||
|
|
||
|
print("="*120 + "\nentering sleep for %d seconds at %s" % (int(CONFIG['SLEEP_SECS']), datetime.now()))
|
||
|
print("="*120)
|
||
|
sleep(int(CONFIG['SLEEP_SECS']))
|
||
|
except ValueError as e:
|
||
|
print(e)
|
||
|
pass
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
daemon()
|
||
|
|
||
|
with daemon.DaemonContext():
|
||
|
daemon()
|