mirror of
https://github.com/Xevion/v1.xevion.dev.git
synced 2025-12-06 01:16:47 -06:00
fixed ratelimiting to proper "view" args of the request context, changed format to be more respectable, less hideous error message
kinda wanted to create a dynamically changing error message to properly convey which rate limit was hit, but that would mean 2 database accesses per error at worst (minimum)
This commit is contained in:
17
app/sound.py
17
app/sound.py
@@ -1,4 +1,4 @@
|
|||||||
from app import app, db
|
from app import app, db, limiter
|
||||||
from app.sound_models import YouTubeAudio, SoundcloudAudio
|
from app.sound_models import YouTubeAudio, SoundcloudAudio
|
||||||
from flask import Response, send_file, redirect, url_for, render_template, request
|
from flask import Response, send_file, redirect, url_for, render_template, request
|
||||||
from multiprocessing import Value
|
from multiprocessing import Value
|
||||||
@@ -23,20 +23,21 @@ def get_youtube(mediaid):
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
return audio
|
return audio
|
||||||
|
|
||||||
|
# Under the request context, it grabs the same args needed to decide whether the stream has been downloaded previously
|
||||||
|
# It applies rate limiting differently based on service, and whether the stream has been accessed previously
|
||||||
def downloadLimiter():
|
def downloadLimiter():
|
||||||
service = request.args.get('service')
|
service, mediaid = request.view_args['service'], request.view_args['mediaid']
|
||||||
mediaid = request.args.get('mediaid')
|
|
||||||
if service == 'youtube':
|
if service == 'youtube':
|
||||||
if YouTubeAudio.query.get(mediaid) is not None:
|
if YouTubeAudio.query.get(mediaid) is not None:
|
||||||
return '5 per minute'
|
return '5/minute'
|
||||||
else:
|
else:
|
||||||
return '1 per 30 seconds'
|
return '1/30seconds'
|
||||||
else:
|
else:
|
||||||
return '10 per minute'
|
return '10/minute'
|
||||||
|
|
||||||
# Streams back the specified media back to the client
|
# Streams back the specified media back to the client
|
||||||
@app.route('/stream/<service>/<mediaid>')
|
@app.route('/stream/<service>/<mediaid>')
|
||||||
@limiter.limit(downloadLimiter)
|
@limiter.limit(downloadLimiter, error_message=Response('Rate limit hit', status=429, mimetype='text/plain'))
|
||||||
def stream(service, mediaid):
|
def stream(service, mediaid):
|
||||||
if service == 'youtube':
|
if service == 'youtube':
|
||||||
audio = get_youtube(mediaid)
|
audio = get_youtube(mediaid)
|
||||||
|
|||||||
Reference in New Issue
Block a user