overhauled checking of JSON database/folders with myriad of existence/error checking/data clearing measures. Pre-overhaul to SQLAlchemy based database system.

This commit is contained in:
Xevion
2019-12-23 18:43:54 -06:00
parent 393af89750
commit 55bc0c95af

View File

@@ -1,10 +1,12 @@
from app import app from app import app
from flask import send_file, redirect, url_for, render_template from flask import send_file, redirect, url_for, render_template
from multiprocessing import Value
from mutagen.mp3 import MP3 from mutagen.mp3 import MP3
import os import os
import re import re
import json import json
import subprocess import subprocess
import shutil
# Services => Expected Data # Services => Expected Data
# YouTube => Video ID # YouTube => Video ID
@@ -16,6 +18,12 @@ import subprocess
# https://soundcloud.com/yungraredeath/fall-in-line-w-kxng-prod-mars-mission => /stream/soundcloud/fall-in-line-w-kxng-prod-mars-mission---yungraredeath => soundcloud/fall-in-line-w-kxng-prod-mars-mission---yungraredeath.mp3 # https://soundcloud.com/yungraredeath/fall-in-line-w-kxng-prod-mars-mission => /stream/soundcloud/fall-in-line-w-kxng-prod-mars-mission---yungraredeath => soundcloud/fall-in-line-w-kxng-prod-mars-mission---yungraredeath.mp3
# spotify:track:16PmczUxlX7dpr6ror6pXd => /duration/spotify/16PmczUxlX7dpr6ror6pXd => spotify/16PmczUxlX7dpr6ror6pXd.mp3 # spotify:track:16PmczUxlX7dpr6ror6pXd => /duration/spotify/16PmczUxlX7dpr6ror6pXd => spotify/16PmczUxlX7dpr6ror6pXd.mp3
class SoundcloudHandler:
pass
class SpotifyHandler:
pass
class YouTubeHandler: class YouTubeHandler:
@staticmethod @staticmethod
def url(videoid): def url(videoid):
@@ -68,30 +76,55 @@ class YouTubeHandler:
service_functions = { service_functions = {
'youtube' : YouTubeHandler, 'youtube' : YouTubeHandler,
'spotify' : {'url' : None, 'path' : None}, 'spotify' : SpotifyHandler,
'soundcloud' : {'url' : None, 'path' : None} 'soundcloud' : SoundcloudHandler
} }
# Default JSON format, will be used in case of corruption
JSONDefault = {'youtube' : {}, 'soundcloud' : {}, 'spotify' : {}}
if not os.path.exists(os.path.join('app', 'sounds')): if not os.path.exists(os.path.join('app', 'sounds')):
print('Sounds folder not found. Creating.')
os.mkdir(os.path.join('app', 'sounds')) os.mkdir(os.path.join('app', 'sounds'))
# Test JSON file existence
if not os.path.exists(os.path.join('app', 'sounds', 'filenames.json')): if not os.path.exists(os.path.join('app', 'sounds', 'filenames.json')):
print('JSON database file not found. Creating with default data structure.')
with open(os.path.join('app', 'sounds', 'filenames.json'), 'w+') as file: with open(os.path.join('app', 'sounds', 'filenames.json'), 'w+') as file:
json.dump({'youtube' : {} }, file) json.dump({'youtube' : {} }, file)
else:
# File exists, but is it valid JSON?
try:
print('Testing JSON database file.')
with open(os.path.join('app', 'sounds', 'filenames.json'), 'r') as file:
json.load(file)
except json.JSONDecodeError:
# Corruption or other has occurred. Clearing all service folders. Resetting and clearing all services
print('Corruption/Invalid formatting in JSON file detected, clearing all config/media.')
print('Clearing JSON database file.')
with open(os.path.join('app', 'sounds', 'filenames.json'), 'w+') as file:
json.dump(JSONDefault, file)
for service in service_functions.keys():
print(f"Clearing '{service}' folder.")
servicePath = os.path.join('app', 'sounds', service)
if os.path.exists(servicePath):
shutil.rmtree(servicePath)
os.mkdir(servicePath)
else:
os.mkdir(servicePath)
# Streams a prepared MP3 back to the client # Streams a prepared MP3 back to the client
@app.route('/stream/<service>/<mediaid>') @app.route('/stream/<service>/<mediaid>')
def stream(service, mediaid): def stream(service, mediaid):
prepare(service, mediaid) prepare(service, mediaid)
if service == 'youtube': config = service_functions[service].getConfig(mediaid)
config = YouTubeHandler.getConfig(mediaid)
return send_file(os.path.join(os.path.dirname(__file__), '..', config['path']), attachment_filename=config['filename']) return send_file(os.path.join(os.path.dirname(__file__), '..', config['path']), attachment_filename=config['filename'])
return '???'
# Prepares a URL for download, returning the duration it should play for if streamed # Prepares a URL for download, returning the duration it should play for if streamed
@app.route('/prepare/<service>/<mediaid>') @app.route('/prepare/<service>/<mediaid>')
def prepare(service, mediaid): def prepare(service, mediaid):
filepath = service_functions[service].path(mediaid) filepath = service_functions[service].path(mediaid)
service_functions[service].download(mediaid) service_functions[service].download(mediaid)
print(filepath)
return str(MP3(filepath).info.length) return str(MP3(filepath).info.length)