mirror of
https://github.com/Xevion/v1.xevion.dev.git
synced 2025-12-12 22:13:28 -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:
39
app/sound.py
39
app/sound.py
@@ -8,6 +8,13 @@ import re
|
|||||||
import json
|
import json
|
||||||
import subprocess
|
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.
|
# 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.
|
# Also helps with access times.
|
||||||
def get_youtube(mediaid):
|
def get_youtube(mediaid):
|
||||||
@@ -15,6 +22,7 @@ def get_youtube(mediaid):
|
|||||||
if audio is not None:
|
if audio is not None:
|
||||||
audio.access()
|
audio.access()
|
||||||
return audio # sets the access time to now
|
return audio # sets the access time to now
|
||||||
|
else:
|
||||||
audio = YouTubeAudio(id=mediaid)
|
audio = YouTubeAudio(id=mediaid)
|
||||||
audio.fill_metadata()
|
audio.fill_metadata()
|
||||||
audio.download()
|
audio.download()
|
||||||
@@ -37,17 +45,20 @@ def downloadLimiter():
|
|||||||
|
|
||||||
# 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, error_message=Response('Rate limit hit', status=429, mimetype='text/plain'))
|
@limiter.limit(downloadLimiter, error_message='Rate Limit Hit')
|
||||||
def stream(service, mediaid):
|
def stream(service, mediaid):
|
||||||
if service == 'youtube':
|
if service == 'youtube':
|
||||||
|
if YouTubeAudio.isValid(mediaid):
|
||||||
audio = get_youtube(mediaid)
|
audio = get_youtube(mediaid)
|
||||||
return send_file(audio.getPath(alt=True), attachment_filename=audio.filename)
|
return send_file(audio.getPath(alt=True), attachment_filename=audio.filename)
|
||||||
elif service == 'soundcloud':
|
|
||||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
|
||||||
elif service == 'spotify':
|
|
||||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
|
||||||
else:
|
else:
|
||||||
return Response('Bad request', status=400, mimetype='text/plain')
|
return getInvalidID()
|
||||||
|
elif service == 'soundcloud':
|
||||||
|
return getNotImplemented()
|
||||||
|
elif service == 'spotify':
|
||||||
|
return getNotImplemented()
|
||||||
|
else:
|
||||||
|
return getBadRequest()
|
||||||
|
|
||||||
# Returns the duration of a specific media
|
# Returns the duration of a specific media
|
||||||
@app.route('/duration/<service>/<mediaid>')
|
@app.route('/duration/<service>/<mediaid>')
|
||||||
@@ -56,11 +67,11 @@ def duration(service, mediaid):
|
|||||||
duration = get_youtube(mediaid).duration
|
duration = get_youtube(mediaid).duration
|
||||||
return Response(str(duration), status=200, mimetype='text/plain')
|
return Response(str(duration), status=200, mimetype='text/plain')
|
||||||
elif service == 'soundcloud':
|
elif service == 'soundcloud':
|
||||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
return getNotImplemented()
|
||||||
elif service == 'spotify':
|
elif service == 'spotify':
|
||||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
return getNotImplemented()
|
||||||
else:
|
else:
|
||||||
return Response('Bad request', status=400, mimetype='text/plain')
|
return getBadRequest()
|
||||||
|
|
||||||
# Returns a detailed JSON export of a specific database entry.
|
# Returns a detailed JSON export of a specific database entry.
|
||||||
# Will not create a new database entry where one didn't exist before.
|
# 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)
|
audio = YouTubeAudio.query.get(mediaid)
|
||||||
if audio is None:
|
if audio is None:
|
||||||
if YouTubeAudio.isValid(mediaid):
|
if YouTubeAudio.isValid(mediaid):
|
||||||
return Response('Media not yet downloaded', status=400, mimetype='text/plain')
|
return getNotDownloaded()
|
||||||
else:
|
else:
|
||||||
return Response('Invalid ID', status=400, mimetype='text/plain')
|
return getInvalidID()
|
||||||
else:
|
else:
|
||||||
return Response(audio.toJSON(), status=200, mimetype='application/json')
|
return Response(audio.toJSON(), status=200, mimetype='application/json')
|
||||||
elif service == 'soundcloud':
|
elif service == 'soundcloud':
|
||||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
return getNotImplemented()
|
||||||
elif service == 'spotify':
|
elif service == 'spotify':
|
||||||
return Response('Not implemented', status=501, mimetype='text/plain')
|
return getNotImplemented()
|
||||||
else:
|
else:
|
||||||
return Response('Bad request', status=400, mimetype='text/plain')
|
return getBadRequest()
|
||||||
Reference in New Issue
Block a user