- commit
- bbee907e67099fcee9f5ae4e4e83d0a3613ca906
- parent
- 4df60eb0e97ee155a67dd9e20e877f6955e883a2
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-05-30 21:15
make rendering more robust
Diffstat
| M | audiothek.py | 73 | ++++++++++++++++++++++++++++++++----------------------------- |
1 files changed, 38 insertions, 35 deletions
diff --git a/audiothek.py b/audiothek.py
@@ -76,62 +76,65 @@ def el(tag, content=None, attrs={}):
76 76 return f'<{tag}{a}>{escape(content)}</{tag}>'
77 77
78 78
79 -1 def get_itunes_category(show):
80 -1 title = show['editorialCategory']['title'].strip()
81 -1 return CATEGORIES.get(title, title)
82 -1
83 -1
84 79 def render_items(items):
85 80 lines = []
86 81 for item in reversed(items):
87 -1 lines += el('item', [
-1 82 fields = [
88 83 el('title', item['title']),
89 84 el('link', item['sharingUrl']),
90 85 el('guid', item['sharingUrl']),
91 -1 el('enclosure', attrs={
92 -1 'url': item['audios'][0]['url'],
93 -1 'length': str(item['audios'][0]['size'] or 0),
94 -1 'type': item['audios'][0]['mimeType'],
95 -1 }),
96 86 el('pubDate', item['publishDate']),
97 87 el('description', item['summary']),
98 88 el('itunes:duration', str(item['duration'])),
99 89 el('itunes:image', attrs={
100 90 'href': item['image']['url1X1'].replace('{width}', '1400'),
101 91 }),
102 -1 ])
-1 92 ]
-1 93 if item['audios']:
-1 94 fields.append(el('enclosure', attrs={
-1 95 'url': item['audios'][0]['url'],
-1 96 'length': str(item['audios'][0]['size'] or 0),
-1 97 'type': item['audios'][0]['mimeType'],
-1 98 }))
-1 99 lines += el('item', fields)
103 100 return lines
104 101
105 102
106 103 def render_feed(show, links):
-1 104 fields = [
-1 105 el('title', show['title']),
-1 106 el('description', show['synopsis']),
-1 107 el('link', show['sharingUrl']),
-1 108 el('language', 'de-DE'),
-1 109 el('itunes:author', show['publicationService']['title']),
-1 110 el('itunes:image', attrs={
-1 111 'href': show['image']['url1X1'].replace('{width}', '1400'),
-1 112 }),
-1 113 el('generator', 'audiothek.ce9e.org'),
-1 114 ]
-1 115
-1 116 if show['editorialCategory']:
-1 117 title = show['editorialCategory']['title'].strip()
-1 118 fields.append(el('category', title))
-1 119 fields.append(el('itunes:category', attrs={
-1 120 'text': CATEGORIES.get(title, title)
-1 121 }))
-1 122
-1 123 fields += render_items(show['items']['nodes'])
-1 124 for rel, href in links.items():
-1 125 fields.append(el('atom:link', attrs={
-1 126 'href': href,
-1 127 'rel': rel,
-1 128 'type': 'application/rss+xml',
-1 129 }))
-1 130
107 131 feed = [
108 132 '<?xml version="1.0" encoding="UTF-8"?>',
109 -1 *el('rss', attrs={
-1 133 *el('rss', content=el('channel', fields), attrs={
110 134 'version': '2.0',
111 135 'xmlns:atom': 'http://www.w3.org/2005/Atom',
112 136 'xmlns:itunes': 'http://www.itunes.com/dtds/podcast-1.0.dtd',
113 -1 }, content=el('channel', [
114 -1 el('title', show['title']),
115 -1 el('description', show['synopsis']),
116 -1 el('link', show['sharingUrl']),
117 -1 el('language', 'de-DE'),
118 -1 el('category', show['editorialCategory']['title']),
119 -1 el('itunes:author', show['publicationService']['title']),
120 -1 el('itunes:category', attrs={'text': get_itunes_category(show)}),
121 -1 el('itunes:image', attrs={
122 -1 'href': show['image']['url1X1'].replace('{width}', '1400'),
123 -1 }),
124 -1 el('generator', 'audiothek.ce9e.org'),
125 -1 *render_items(show['items']['nodes']),
126 -1 *[
127 -1 el('atom:link', attrs={
128 -1 'href': href,
129 -1 'rel': rel,
130 -1 'type': 'application/rss+xml',
131 -1 })
132 -1 for rel, href in links.items()
133 -1 ],
134 -1 ])),
-1 137 }),
135 138 ]
136 139
137 140 return ''.join(f'{line}\n' for line in feed)