From 5774dbfb2caa42cb55bafab98a40e47f395e44d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gl=C3=BCpker?= Date: Wed, 5 May 2021 20:09:30 +0200 Subject: Initial commit of RSS converter application --- zdf.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100755 zdf.py (limited to 'zdf.py') diff --git a/zdf.py b/zdf.py new file mode 100755 index 0000000..37749ac --- /dev/null +++ b/zdf.py @@ -0,0 +1,87 @@ +import logging +from urllib.request import urlopen, Request +from datetime import datetime +from xml.dom.minidom import parse, parseString +import locale + +def getText(dom, element): + textNode = dom.getElementsByTagName(element)[0].firstChild + if textNode: + return textNode.data + return "" + +def zdf(feed): + url = f"https://www.zdf.de/rss/zdf/{feed}" + + try: + res = urlopen(Request(url)) + except Exception as exc: + logging.error('Request to zdf failed.', exc_info=exc) + return None + + try: + rss = res.read() + xml = parseString(rss) + except Exception as exc: + logging.error('Parsing to zdf failed.', exc_info=exc) + return None + + try: + title = getText(xml, 'title') + description = getText(xml, 'description') + + content = [] + for show in xml.getElementsByTagName('item'): + s_url = getText(show, 'link') + if not s_url: + continue + # Full episodes have the ID 100 + if not s_url.endswith('-100.html'): + continue + + s_title = getText(show, 'title') + if not s_title.startswith(title): + continue + + s_date = getText(show, 'pubDate') + s_date_parsed = datetime.strptime(s_date, "%a, %d %b %Y %H:%M:%S %z") + + if s_date_parsed.timestamp() > datetime.now().timestamp(): + continue + + # s_tmp = s_title = getText(show, 'title') + # if s_tmp.startswith(f'{title} vom '): + # s_tmp = s_tmp[len(f'{title} vom '):] + # saved = locale.setlocale(locale.LC_TIME) + # locale.setlocale(locale.LC_TIME, "de_DE.utf8") + # tmp = datetime.strptime(s_tmp, "%d. %B %Y") + # locale.setlocale(locale.LC_TIME, saved) + + s_desc = getText(show, 'description') + s_guid = getText(show, 'guid') + print("Adding", s_url, s_desc) + content.append({ + 'title': s_title, + 'url': s_url, + 'content': s_desc, + 'date': s_date, + 'guid': s_guid, + }) + + return title, url, description, content + except Exception as exc: + logging.error('Working with zdf failed.', exc_info=exc) + return None + + +def main(): + # print(zdf("comedy/heute-show")) + # print(zdf("comedy/die-anstalt")) + print(zdf("comedy/zdf-magazin-royale")) + +if __name__ == "__main__": + # if len(sys.argv) != 2: + # print('Usage:', sys.argv[0], '') + # sys.exit(1) + # main(sys.argv[1]) + main() -- cgit v1.2.3