from flask import Blueprint, jsonify, request from dejavu import Dejavu from dejavu.logic.recognizer.file_recognizer import FileRecognizer import os import ipfshttpclient import tempfile import psycopg2 mazash = Blueprint(name="mazash", import_name=__name__) config = { "database" : { "host" : os.getenv("DATABASE_HOST", "db"), "user" : os.getenv("DATABASE_USER","postgres"), "password" : os.getenv("DATABASE_PASSWORD", "changeme"), "database" : os.getenv("DATABASE_DB", "dejavu") }, "database_type": os.getenv("DATABASE_TYPE", "mysql") } SELECT_HASH_BY_SONG_ID = """ SELECT upper(encode(songs.file_sha1, 'hex')) AS file_sha1 FROM songs WHERE songs.song_name = %s; """ def transform_resultToJsonable(r): jsonable = { "song_id": r["song_id"], "song_name": r["song_name"].decode("utf-8"), "input_total_hashes": r["input_total_hashes"], "fingerprinted_hashes_in_db": r["fingerprinted_hashes_in_db"], "hashes_matched_in_input": r["hashes_matched_in_input"], "input_confidence": r["input_confidence"], "fingerprinted_confidence": r["fingerprinted_confidence"], "offset": int(r["offset"]), "offset_seconds": r["offset_seconds"], "file_sha1": r["file_sha1"].decode("utf-8") } return jsonable @mazash.route('/fingerprint', methods=['POST']) def fingerprint(): """ --- post: description: fingerprint a file by CID IPFS requestBody: required: true content: application/json: schema: FingerprintInputSchema responses: '200': description: call successful content: application/json: schema: FingerprintOuputSchema tags: - mazash """ ipfs_api = ipfshttpclient.connect(os.getenv("IPFS_HOST")) djv = Dejavu(config) if request.is_json : ipfs_hash = request.json.get("cid") song_name = request.json.get("song") extension = request.json.get("extension") content = ipfs_api.cat(ipfs_hash) temp_file = tempfile.NamedTemporaryFile(suffix=extension) temp_file.write(content) link_filename = f"/tmp/{song_name}{extension}" os.link(temp_file.name, link_filename) print("{} tempfile created for {}".format(temp_file.name, song_name)) temp_file.close() djv.fingerprint_directory("/tmp/", [extension]) os.remove(link_filename) output = {"reponse":"success", "fingerPrint":""} dbConnection = psycopg2.connect(database=config["database"]["database"], user=config["database"]["user"], password=config["database"]["password"], host=config["database"]["host"]) cursor = dbConnection.cursor() try: cursor.execute(SELECT_HASH_BY_SONG_ID, (song_name,)) except psycopg2.Error as e: return jsonify({"error_message":e}) row = cursor.fetchone() output["fingerPrint"] = row[0] else : return 400, "waiting JSON valid" dbConnection.close() return jsonify(output) @mazash.route('/recognize', methods=['POST']) def recognize(): """ --- post: description: recognize a song by CID IPFS requestBody: required: true content: application/json: schema: RecognizeInputSchema responses: '200': description: call successful content: application/json: schema: RecognizeOuputSchema tags: - mazash """ ipfs_api = ipfshttpclient.connect(os.getenv("IPFS_HOST")) djv = Dejavu(config) if request.is_json : ipfs_hash = request.json.get("cid") extension = request.json.get("extension") content = ipfs_api.cat(ipfs_hash) temp_file = tempfile.NamedTemporaryFile(suffix=extension) temp_file.write(content) print("{} tempfile created for {}".format(temp_file.name, ipfs_hash)) song = djv.recognize(FileRecognizer, temp_file.name) temp_file.close() print(f"{song}") result = { "total_time": song["total_time"], "fingerprint_time": song["fingerprint_time"], "query_time": song["query_time"], "align_time": song["align_time"], "results": list(map(transform_resultToJsonable, song["results"])) } else : return 400, "waiting JSON valid" return jsonify(result)