funapi / routes /osuApi /getPreview.py
imperialwool's picture
upd
f02b1de
raw
history blame
1.9 kB
from .. import helpers
from .findSong import *
from requests import get
from random import randint as rand
def getPreview(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(request, 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
if beatmapId != None:
tryment = get(f"https://b.ppy.sh/preview/{beatmapId}.mp3")
if int(tryment.status_code) not in [404, 403]:
return {"status": "pass", "details": {"code": int(tryment.status_code), "result": f"https://b.ppy.sh/preview/{beatmapId}.mp3"}}
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
return {"status": "pass", "details": {"code": fffff['details']['code'], "result": "https://b.ppy.sh/preview/{}.mp3".format(fffff['details']['result'][rand(0,len(fffff['details']['result']))]['beatmapId'])}}
return {}