from sync.connectors import mysql from sync.models.song import Song from sync.app import application from sync.logic.result import Result from sync import presentation import json from flask import request import requests from sync.app import application from sync import config @application.route('/') def index(): return application.send_static_file('sync.html') @application.route('/echonest_songs', methods=['GET']) def echonest_songs(): title = request.args.get('title') result = requests.get(config.ECHONEST_SONG_URL+'/search?api_key='+config.ECHONEST_API_KEY+'&format=json&title='+title+'&sort=song_hotttnesss-desc') songs = [] for song in result.json()['response']['songs']: songs.append({'value': song['id'],'label': song['title'] + ' - ' + song['artist_name']}) return json.dumps(songs) @application.route('/echonest_song_detail', methods=['GET']) def echonest_song_detail(): id = request.args.get('id') result = requests.get(config.ECHONEST_SONG_URL+'/profile?api_key='+config.ECHONEST_API_KEY +'&format=json&bucket=audio_summary&id='+id) return json.dumps(result.json()) @application.route('/hello', methods=['GET']) def hello_world(): return 'Hello World!', 200 @application.route('/orchard_songs', methods=['GET']) def orchard_songs(): speechiness_threshold = 0.19944732736623594 instrumentalness_threshold = 0.3603698824715403 acousticness_threshold = 0.37709007593586 energy_threshold = 0.28070328493544416 duration_threshold = 231.43655396439422 tempo_threshold = 33.25263330744799 danceability_threshold = 0.1920095800631704 valence_threshold = 0.2681629457102368 params = {'speechiness': float(request.args.get('speechiness')), 'instrumentalness': float(request.args.get('instrumentalness')), 'acousticness': float(request.args.get('acousticness')), 'energy': float(request.args.get('energy')), 'duration': float(request.args.get('duration')), 'tempo': float(request.args.get('tempo')), 'danceability': float(request.args.get('danceability')), 'valence': float(request.args.get('valence'))} session = mysql.sync_session() songs = session.query(Song).filter( Song.speechiness < params['speechiness'] + speechiness_threshold, Song.speechiness > params['speechiness'] - speechiness_threshold, Song.instrumentalness < params['instrumentalness'] + instrumentalness_threshold, Song.instrumentalness > params['instrumentalness'] - instrumentalness_threshold, Song.acousticness < params['acousticness'] + acousticness_threshold, Song.acousticness > params['acousticness'] - acousticness_threshold, Song.energy < params['energy'] + energy_threshold, Song.energy > params['energy'] - energy_threshold, Song.duration < params['duration'] + duration_threshold, Song.duration > params['duration'] - duration_threshold, Song.tempo < params['tempo'] + tempo_threshold, Song.tempo > params['tempo'] - tempo_threshold, Song.danceability < params['danceability'] + danceability_threshold, Song.danceability > params['danceability'] - danceability_threshold, Song.valence < params['valence'] + valence_threshold, Song.valence > params['valence'] - valence_threshold).all() result = Result( data={ 'songs': songs}, status=200) return presentation.get_orchard_songs_response(result)