From f0f10c6966eef1795cbc99bb456fca6e4cfbee59 Mon Sep 17 00:00:00 2001 From: Jakob de Maeyer Date: Thu, 8 Feb 2024 08:17:07 +0100 Subject: [PATCH] Cache rejections --- spoqify/app.py | 1 + spoqify/routes.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/spoqify/app.py b/spoqify/app.py index 702cb9c..b056f40 100644 --- a/spoqify/app.py +++ b/spoqify/app.py @@ -45,6 +45,7 @@ async def startup(): app.session = aiohttp.ClientSession(raise_for_status=True) app.tasks = {} app.recent_reqs = RecentCounter() + app.rejected_urls = {} @app.after_serving diff --git a/spoqify/routes.py b/spoqify/routes.py index c06957d..cbb4636 100644 --- a/spoqify/routes.py +++ b/spoqify/routes.py @@ -76,6 +76,11 @@ def _remove_task(task): async def anonymize(stream=True): # Fallback to 'playlist' for legacy support url = quart.request.args.get('url', quart.request.args.get('playlist')) + if e := app.rejected_urls.get(url): + # Most of our rejections are bots requesting the same URL over and + # over, no need to bother Spotify every time + app.recent_reqs.record('cached') + return quart.abort(400, str(e)) if stream: response = await quart.make_response( stream_task_status(url), @@ -91,6 +96,8 @@ async def anonymize(stream=True): try: task = _get_task(url) except (Rejected, ValueError) as e: + if isinstance(e, Rejected): + app.rejected_urls[url] = e return quart.abort(400, str(e)) result_url = await asyncio.shield(task) return quart.redirect(result_url) @@ -116,6 +123,7 @@ async def stream_task_status(url): yield encode_event('queued', task_idx) except Rejected as e: app.logger.info("Rejected request for %s: %s", url, e) + app.rejected_urls[url] = e app.recent_reqs.record('rejected') yield encode_event('error', str(e)) break