import json from .. import helpers from requests import get from random import randint as rand def findSong(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': query = request.form['query'] else: query = request.args['query'] if query.strip() in ['', None]: raise Exception() except: return {"status": "error", "details": { "error_code": 133, "error_details": "No query" }} tryment = get("https://api.chimu.moe/v1/search", params={"query": query}) if int(tryment.status_code) not in [404, 403]: res = [] counter = 0 for chunk in tryment.json()['data']: res.append({ "beatmapId": chunk['SetId'], "artist": chunk['Artist'], "title": chunk['Title'], "creator": chunk['Creator'], "source": chunk['Source'], "tags": chunk['Tags'] }) counter += 1 if counter >= rand(3,7): break return {"status": "pass", "details": {"code": int(tryment.status_code), "result": res}} else: return {"status": "error", "details": {"code": int(tryment.status_code), "answer": tryment.text}}