import os import ffmpeg from .. import helpers from .findSong import * from requests import get from random import randint as rand def getFull(request): try: if request.method == 'POST': signature = request.form['signature'] else: signature = request.args['signature'] except: return {"status": "error", "details": { "error_code": 103, "error_details": "No signature" }} if not helpers.checkSignature(signature): return {"status": "error", "details": { "error_code": 105, "error_details": "Invalid signature" }} try: if request.method == 'POST': beatmapId = request.form['beatmapId'] else: beatmapId = request.args['beatmapId'] if beatmapId.strip() in ['', None]: raise Exception() except: beatmapId = None try: if request.method == 'POST': query = request.form['query'] else: query = request.args['query'] if query.strip() in ['', None]: raise Exception() except: if beatmapId == None: return {"status": "error", "details": { "error_code": 133, "error_details": "No details for finding preview" }} else: query = None config = helpers.configFile() if beatmapId != None: if os.path.exists(f"{config['full-path']}/{beatmapId}.ogg"): return {"status": "pass", "details": {"code": 200, "result": f"{config['url']}/static/full/{beatmapId}.ogg"}} tryment = get(f"https://kitsu.moe/api/audio/{beatmapId}", allow_redirects=True) if int(tryment.status_code) not in [404, 403, 429]: open(f"{config['temp-path']}/{beatmapId}.mp3", "wb").write(tryment.content) audio_input = ffmpeg.input(f"{config['temp-path']}/{beatmapId}.mp3") audio_output = ffmpeg.output(audio_input, f"{config['full-path']}/{beatmapId}.ogg", audio_bitrate="96K") ffmpeg.run(audio_output) helpers.deleteAudio(f"temp/{beatmapId}.ogg") return {"status": "pass", "details": {"code": int(tryment.status_code), "result": f"{config['url']}/static/full/{beatmapId}.ogg"}} else: return {"status": "error", "details": {"code": int(tryment.status_code), "answer": tryment.text}} if query != None: fffff = findSong(request) if fffff['status'] == "error": return fffff beatmapId = fffff['details']['result'][rand(0,len(fffff['details']['result'])-1)]['beatmapId'] if os.path.exists(f"{config['full-path']}/{beatmapId}.ogg"): return {"status": "pass", "details": {"code": 200, "result": f"{config['url']}/static/full/{beatmapId}.ogg"}} tryment = get(f"https://kitsu.moe/api/audio/{beatmapId}", allow_redirects=True) if int(tryment.status_code) not in [404, 403, 429]: open(f"{config['temp-path']}/{beatmapId}.mp3", "wb").write(tryment.content) audio_input = ffmpeg.input(f"{config['temp-path']}/{beatmapId}.mp3") audio_output = ffmpeg.output(audio_input, f"{config['full-path']}/{beatmapId}.ogg", audio_bitrate="96K") ffmpeg.run(audio_output) helpers.deleteAudio(f"temp/{beatmapId}.ogg") return {"status": "pass", "details": {"code": int(tryment.status_code), "name": f"{beatmapId}.ogg", "result": f"{config['url']}/static/full/{beatmapId}.ogg"}} else: return {"status": "error", "details": {"code": int(tryment.status_code), "answer": tryment.text}} return {}