Skip to content

Commit

Permalink
Cache rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemaeyer committed Feb 8, 2024
1 parent 7117db5 commit f0f10c6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions spoqify/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spoqify/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit f0f10c6

Please sign in to comment.