mirror of
https://github.com/Xevion/v1.xevion.dev.git
synced 2025-12-07 03:16:58 -06:00
PyCharm grand repo wide reformat
This commit is contained in:
58
app/sound.py
58
app/sound.py
@@ -1,20 +1,16 @@
|
||||
from app import app, db, limiter
|
||||
from app.sound_models import YouTubeAudio, SoundcloudAudio, CouldNotDecode, CouldNotDownload, CouldNotProcess
|
||||
from flask import Response, send_file, redirect, url_for, render_template, request, jsonify
|
||||
from flask import Response, send_file, request, jsonify
|
||||
from flask_login import current_user
|
||||
from multiprocessing import Value
|
||||
from mutagen.mp3 import MP3
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
import subprocess
|
||||
|
||||
from app import app, db, limiter
|
||||
from app.sound_models import YouTubeAudio, CouldNotDecode, CouldNotDownload, CouldNotProcess
|
||||
|
||||
# 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')
|
||||
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.
|
||||
@@ -22,22 +18,24 @@ def get_youtube(mediaid):
|
||||
audio = YouTubeAudio.query.get(mediaid)
|
||||
if audio is not None:
|
||||
audio.access()
|
||||
return audio # sets the access time to now
|
||||
return audio # sets the access time to now
|
||||
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
|
||||
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
|
||||
|
||||
|
||||
basic_responses = {
|
||||
CouldNotDecode : 'Could not decode process response.',
|
||||
CouldNotDownload : 'Could not download video.',
|
||||
CouldNotProcess : 'Could not process.'
|
||||
CouldNotDecode: 'Could not decode process response.',
|
||||
CouldNotDownload: 'Could not download video.',
|
||||
CouldNotProcess: 'Could not process.'
|
||||
}
|
||||
|
||||
|
||||
# A simple function among the routes to determine what should be returned.
|
||||
# Not particularly sure how request context is passed, but it seems that either it passed or can access current_user's authenitcation/role's properly, so no problem.
|
||||
# Shows error in full context IF authenticated + admin, otherwise basic error description, OTHERWISE a basic error message.
|
||||
@@ -48,7 +46,8 @@ def errorCheck(e):
|
||||
raise e
|
||||
if current_user.is_authenticated and current_user.has_role('Admin'): response = str(e) + '\n' + response
|
||||
return Response(response, status=200, mimetype='text/plain')
|
||||
|
||||
|
||||
|
||||
# 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():
|
||||
@@ -60,9 +59,10 @@ def downloadLimiter():
|
||||
else:
|
||||
return '10/minute'
|
||||
|
||||
|
||||
# Streams back the specified media back to the client
|
||||
@app.route('/stream/<service>/<mediaid>')
|
||||
@limiter.limit(downloadLimiter, lambda : 'global', error_message='429 Too Many Requests')
|
||||
@limiter.limit(downloadLimiter, lambda: 'global', error_message='429 Too Many Requests')
|
||||
def stream(service, mediaid):
|
||||
if service == 'youtube':
|
||||
if YouTubeAudio.isValid(mediaid):
|
||||
@@ -80,6 +80,7 @@ def stream(service, mediaid):
|
||||
else:
|
||||
return getBadRequest()
|
||||
|
||||
|
||||
# Returns the duration of a specific media
|
||||
@app.route('/duration/<service>/<mediaid>')
|
||||
def duration(service, mediaid):
|
||||
@@ -93,6 +94,7 @@ def duration(service, mediaid):
|
||||
else:
|
||||
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.
|
||||
@app.route('/status/<service>/<mediaid>')
|
||||
@@ -113,6 +115,7 @@ def status(service, mediaid):
|
||||
else:
|
||||
return getBadRequest()
|
||||
|
||||
|
||||
@app.route('/list/<service>')
|
||||
def list(service):
|
||||
if service == 'youtube':
|
||||
@@ -125,6 +128,7 @@ def list(service):
|
||||
else:
|
||||
return getBadRequest()
|
||||
|
||||
|
||||
@app.route('/all/<service>')
|
||||
def all(service):
|
||||
if service == 'youtube':
|
||||
@@ -136,4 +140,4 @@ def all(service):
|
||||
elif service == 'spotify':
|
||||
return getNotImplemented()
|
||||
else:
|
||||
return getBadRequest()
|
||||
return getBadRequest()
|
||||
|
||||
Reference in New Issue
Block a user