- commit
- f286a408b9df2ba966182f7d03ccfce67ae50375
- parent
- c8a04d20618ef09522df7743c696cee216975a0d
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-02-14 08:33
refactor xml (again)
Diffstat
| M | audiothek.py | 92 | +++++++++++++++++++++++++++++-------------------------------- |
1 files changed, 43 insertions, 49 deletions
diff --git a/audiothek.py b/audiothek.py
@@ -34,73 +34,67 @@ query ProgramSetEpisodesQuery($id:ID!, $offset:Int!, $count:Int!) {
34 34 """.replace(' ', '').replace('\n', ' ').strip()
35 35
36 36
-1 37 def indent(lines):
-1 38 return [f'\t{line}' for line in lines]
-1 39
-1 40
37 41 def el(tag, content=None, attrs={}):
38 42 a = ''.join(f' {k}={quoteattr(v)}' for k, v in attrs.items())
39 43 if content is None:
40 44 return f'<{tag}{a} />'
-1 45 elif isinstance(content, list):
-1 46 return [
-1 47 f'<{tag}{a}>',
-1 48 *indent(content),
-1 49 f'</{tag}>',
-1 50 ]
41 51 else:
42 52 return f'<{tag}{a}>{escape(content)}</{tag}>'
43 53
44 54
45 -1 def indent(lines):
46 -1 return [f'\t{line}' for line in lines]
47 -1
48 -1
49 55 def render_items(items):
50 56 lines = []
51 57 for item in reversed(items):
52 -1 lines += [
53 -1 '<item>',
54 -1 *indent([
55 -1 el('title', item['title']),
56 -1 el('link', 'https://www.ardaudiothek.de' + item['path']),
57 -1 el('guid', 'https://www.ardaudiothek.de' + item['path']),
58 -1 el('enclosure', attrs={
59 -1 'url': item['audios'][0]['url'],
60 -1 'type': 'audio/mpeg',
61 -1 }),
62 -1 el('pubDate', item['publishDate']),
63 -1 el('description', item['summary']),
64 -1 el('itunes:duration', str(item['duration'])),
65 -1 ]),
66 -1 '</item>',
67 -1 ]
-1 58 lines += el('item', [
-1 59 el('title', item['title']),
-1 60 el('link', 'https://www.ardaudiothek.de' + item['path']),
-1 61 el('guid', 'https://www.ardaudiothek.de' + item['path']),
-1 62 el('enclosure', attrs={
-1 63 'url': item['audios'][0]['url'],
-1 64 'type': 'audio/mpeg',
-1 65 }),
-1 66 el('pubDate', item['publishDate']),
-1 67 el('description', item['summary']),
-1 68 el('itunes:duration', str(item['duration'])),
-1 69 ])
68 70 return lines
69 71
70 72
71 73 def render_feed(show, links):
72 74 feed = [
73 75 '<?xml version="1.0" encoding="UTF-8"?>',
74 -1 (
75 -1 '<rss version="2.0" '
76 -1 'xmlns:atom="http://www.w3.org/2005/Atom" '
77 -1 'xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">'
78 -1 ),
79 -1 *indent([
80 -1 '<channel>',
81 -1 *indent([
82 -1 el('title', show['title']),
83 -1 el('link', 'https://www.ardaudiothek.de' + show['path']),
84 -1 '<image>',
85 -1 *indent([
86 -1 el('url', show['image']['url1X1'].replace('{width}', '64')),
87 -1 el('width', '64'),
88 -1 el('height', '64'),
89 -1 ]),
90 -1 '</image>',
91 -1 *render_items(show['items']['nodes']),
92 -1 *[
93 -1 el('atom:link', attrs={
94 -1 'href': href,
95 -1 'rel': rel,
96 -1 'type': 'application/rss+xml',
97 -1 })
98 -1 for rel, href in links.items()
99 -1 ],
-1 76 *el('rss', attrs={
-1 77 'version': '2.0',
-1 78 'xmlns:atom': 'http://www.w3.org/2005/Atom',
-1 79 'xmlns:itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd',
-1 80 }, content=el('channel', [
-1 81 el('title', show['title']),
-1 82 el('link', 'https://www.ardaudiothek.de' + show['path']),
-1 83 *el('image', [
-1 84 el('url', show['image']['url1X1'].replace('{width}', '64')),
-1 85 el('width', '64'),
-1 86 el('height', '64'),
100 87 ]),
101 -1 '</channel>',
102 -1 ]),
103 -1 '</rss>',
-1 88 *render_items(show['items']['nodes']),
-1 89 *[
-1 90 el('atom:link', attrs={
-1 91 'href': href,
-1 92 'rel': rel,
-1 93 'type': 'application/rss+xml',
-1 94 })
-1 95 for rel, href in links.items()
-1 96 ],
-1 97 ])),
104 98 ]
105 99
106 100 return ''.join(f'{line}\n' for line in feed)