audiothek

alternative frontend for ardaudiothek.de  https://audiothek.ce9e.org/
git clone https://git.ce9e.org/audiothek.git

commit
eea858e33d43235b9f5da7508a3264e311bd5479
parent
03b4f16ddba31365c0163f262223b83b4180fb6b
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-12-22 08:42
add pagination link to feed

Links are only added if an origin is passed as a parameter.

Getting the origin from the request would only be possible if we
configureed the proxy to pass this information along, which in turn
would open potential security issues. So this approach is simpler.

See https://docs.aiohttp.org/en/stable/web_advanced.html#aiohttp-web-forwarded-support

Diffstat

M audiothek.py 25 ++++++++++++++++++++++---

1 files changed, 22 insertions, 3 deletions


diff --git a/audiothek.py b/audiothek.py

@@ -32,7 +32,7 @@ query ProgramSetEpisodesQuery($id:ID!, $offset:Int!, $count:Int!) {
   32    32 }
   33    33 """.replace('\t', '').replace('\n', ' ').strip()
   34    34 
   35    -1 FEED = """<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
   -1    35 FEED = """<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd">
   36    36 	<channel>
   37    37 		<title>{title}</title>
   38    38 		<link>{url}</link>
@@ -46,6 +46,8 @@ FEED = """<rss version="2.0" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.
   46    46 	</channel>
   47    47 </rss>"""
   48    48 
   -1    49 LINK = '<atom:link href="{href}" rel="{rel}" type="application/rss+xml"/>'
   -1    50 
   49    51 ENTRY = """		<item>
   50    52 			<title>{title}</title>
   51    53 			<link>{url}</link>
@@ -63,8 +65,9 @@ def escape_data(data):
   63    65 	}
   64    66 
   65    67 
   66    -1 def render_feed(show, episodes):
   -1    68 def render_feed(show, episodes, links):
   67    69 	entries = [ENTRY.format(**escape_data(ep)) for ep in reversed(episodes)]
   -1    70 	entries += [LINK.format(**escape_data(link)) for link in links]
   68    71 	return FEED.format(entries='\n'.join(entries), **escape_data(show))
   69    72 
   70    73 
@@ -109,7 +112,19 @@ async def feed(request):
  109   112 		'duration': str(node['duration']),
  110   113 	} for node in show['items']['nodes']]
  111   114 
  112    -1 	body = render_feed(show_data, episode_data)
   -1   115 	links = []
   -1   116 	if ORIGIN:
   -1   117 		links.append({
   -1   118 			'href': f'{ORIGIN}{request.url.path_qs}',
   -1   119 			'rel': 'self',
   -1   120 		})
   -1   121 		if show['items']['pageInfo']['hasNextPage']:
   -1   122 			links.append({
   -1   123 				'href': f'{ORIGIN}{request.url.path}?offset={offset + COUNT}',
   -1   124 				'rel': 'next',
   -1   125 			})
   -1   126 
   -1   127 	body = render_feed(show_data, episode_data, links)
  113   128 
  114   129 	return web.Response(
  115   130 		body=body.encode('utf8'),
@@ -120,8 +135,12 @@ async def feed(request):
  120   135 if __name__ == '__main__':
  121   136 	parser = argparse.ArgumentParser()
  122   137 	parser.add_argument('--port', type=int)
   -1   138 	parser.add_argument('--origin')
  123   139 	args = parser.parse_args()
  124   140 
   -1   141 	global ORIGIN
   -1   142 	ORIGIN = args.origin
   -1   143 
  125   144 	app = web.Application()
  126   145 	app.router.add_route('GET', '/{id:[0-9]+}.rss', feed)
  127   146 	web.run_app(app, host='localhost', port=args.port)