mirror of
https://github.com/Xevion/v1.xevion.dev.git
synced 2025-12-09 10:09:10 -06:00
changed responses into simple zero arg lambda, remove Response object from flask limiter error message, add else to getter/adder, ID validation to YouTube stream
I was noticing that a lot of the Responses I was creating were literal duplicates, so I decided to move them all into a function so I could change them all at will. This could support optional args later so I could generate better and more concise Responses.
This commit is contained in:
53
app/sound.py
53
app/sound.py
@@ -8,6 +8,13 @@ import re
|
||||
import json
|
||||
import subprocess
|
||||
|
||||
# Selection of Lambdas for creating new responses
|
||||
# Not sure if Responses change based on Request Context, but it doesn't hurt.
|
||||
getBadRequest = lambda : Response('Bad request', status=400, mimetype='text/plain')
|
||||
getNotImplemented = lambda : Response('Not implemented', status=501, mimetype='text/plain')
|
||||
getInvalidID = lambda : Response('Invalid ID', status=400, mimetype='text/plain')
|
||||
getNotDownloaded = lambda : Response('Media not yet downloaded', status=400, mimetype='text/plain')
|
||||
|
||||
# Retrieves the YouTubeAudio object relevant to the mediaid if available. If not, it facilitates the creation and writing of one.
|
||||
# Also helps with access times.
|
||||
def get_youtube(mediaid):
|
||||
@@ -15,13 +22,14 @@ def get_youtube(mediaid):
|
||||
if audio is not None:
|
||||
audio.access()
|
||||
return audio # sets the access time to now
|
||||
audio = YouTubeAudio(id=mediaid)
|
||||
audio.fill_metadata()
|
||||
audio.download()
|
||||
# Commit and save new audio object into the database
|
||||
db.session.add(audio)
|
||||
db.session.commit()
|
||||
return audio
|
||||
else:
|
||||
audio = YouTubeAudio(id=mediaid)
|
||||
audio.fill_metadata()
|
||||
audio.download()
|
||||
# Commit and save new audio object into the database
|
||||
db.session.add(audio)
|
||||
db.session.commit()
|
||||
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
|
||||
@@ -37,17 +45,20 @@ def downloadLimiter():
|
||||
|
||||
# Streams back the specified media back to the client
|
||||
@app.route('/stream/<service>/<mediaid>')
|
||||
@limiter.limit(downloadLimiter, error_message=Response('Rate limit hit', status=429, mimetype='text/plain'))
|
||||
@limiter.limit(downloadLimiter, error_message='Rate Limit Hit')
|
||||
def stream(service, mediaid):
|
||||
if service == 'youtube':
|
||||
audio = get_youtube(mediaid)
|
||||
return send_file(audio.getPath(alt=True), attachment_filename=audio.filename)
|
||||
if YouTubeAudio.isValid(mediaid):
|
||||
audio = get_youtube(mediaid)
|
||||
return send_file(audio.getPath(alt=True), attachment_filename=audio.filename)
|
||||
else:
|
||||
return getInvalidID()
|
||||
elif service == 'soundcloud':
|
||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
||||
return getNotImplemented()
|
||||
elif service == 'spotify':
|
||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
||||
return getNotImplemented()
|
||||
else:
|
||||
return Response('Bad request', status=400, mimetype='text/plain')
|
||||
return getBadRequest()
|
||||
|
||||
# Returns the duration of a specific media
|
||||
@app.route('/duration/<service>/<mediaid>')
|
||||
@@ -56,11 +67,11 @@ def duration(service, mediaid):
|
||||
duration = get_youtube(mediaid).duration
|
||||
return Response(str(duration), status=200, mimetype='text/plain')
|
||||
elif service == 'soundcloud':
|
||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
||||
return getNotImplemented()
|
||||
elif service == 'spotify':
|
||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
||||
return getNotImplemented()
|
||||
else:
|
||||
return Response('Bad request', status=400, mimetype='text/plain')
|
||||
return getBadRequest()
|
||||
|
||||
# Returns a detailed JSON export of a specific database entry.
|
||||
# Will not create a new database entry where one didn't exist before.
|
||||
@@ -70,14 +81,14 @@ def status(service, mediaid):
|
||||
audio = YouTubeAudio.query.get(mediaid)
|
||||
if audio is None:
|
||||
if YouTubeAudio.isValid(mediaid):
|
||||
return Response('Media not yet downloaded', status=400, mimetype='text/plain')
|
||||
return getNotDownloaded()
|
||||
else:
|
||||
return Response('Invalid ID', status=400, mimetype='text/plain')
|
||||
return getInvalidID()
|
||||
else:
|
||||
return Response(audio.toJSON(), status=200, mimetype='application/json')
|
||||
elif service == 'soundcloud':
|
||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
||||
return getNotImplemented()
|
||||
elif service == 'spotify':
|
||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
||||
return getNotImplemented()
|
||||
else:
|
||||
return Response('Bad request', status=400, mimetype='text/plain')
|
||||
return getBadRequest()
|
||||
Reference in New Issue
Block a user