summaryrefslogtreecommitdiff
path: root/zdf.py
blob: b81278d1a1c5f2cbe18e71acda0727c70f3c952b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3

import logging
from datetime import datetime
from urllib.request import Request, urlopen
from xml.dom.minidom import parseString

from rss_types import RSSFeed, RSSItem


def getText(dom, element):
    textNode = dom.getElementsByTagName(element)[0].firstChild
    if textNode:
        return textNode.data
    return ""


def zdf(show_url):
    url = f"https://www.zdf.de/rss/zdf/{show_url}"

    try:
        response = urlopen(Request(url))
    except Exception as exc:
        logging.error("Request to zdf failed.", exc_info=exc)
        return None

    try:
        rss_raw = response.read()
        xml = parseString(rss_raw)
    except Exception as exc:
        logging.error("Parsing to zdf failed.", exc_info=exc)
        return None

    try:
        title = getText(xml, "title")
        description = getText(xml, "description")

        rss_items = []
        for show in xml.getElementsByTagName("item"):
            s_url = getText(show, "link")
            if not s_url:
                continue

            # Full episodes have the ID 100 (others aswell sometimes)
            if not s_url.endswith("-100.html"):
                continue

            s_title = getText(show, "title")
            # if not s_title.startswith(title):  # broke vor Maithink X
            #     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")
            rss_items.append(
                RSSItem(
                    title=s_title,
                    url=s_url,
                    content=s_desc,
                    date=s_date,
                    guid=s_guid,
                    enclosures=[],
                )
            )

        return RSSFeed(
            title=title,
            url=url,
            description=description,
            content=rss_items,
        )
    except Exception as exc:
        logging.error("Working with zdf failed.", exc_info=exc)
        return None


if __name__ == "__main__":
    pass
    # print(zdf("comedy/heute-show"))
    # print(zdf("comedy/die-anstalt"))
    # print(zdf("comedy/zdf-magazin-royale"))
    # print(zdf("show/mai-think-x-die-show"))