-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
126 lines (102 loc) · 3.67 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from flask import Flask, jsonify, request, send_file, send_from_directory
from io import BytesIO
from yt_dlp import YoutubeDL
import json
from ytmusicapi import YTMusic
import dotenv
from contextlib import redirect_stdout
from recommendation.recommendor import getRecommendations
import spotipy
import os
dotenv.load_dotenv()
spotify_client_id = os.getenv("SPOTIFY_CLIENT_ID")
spotify_client_secret = os.getenv("SPOTIFY_CLIENT_SECRET")
sp = spotipy.Spotify(client_credentials_manager=spotipy.oauth2.SpotifyClientCredentials(
client_id=spotify_client_id,
client_secret=spotify_client_secret
))
yt = YTMusic()
app = Flask(__name__,
static_url_path='/',
static_folder='frontend/build/')
def search_songs(search_term):
results = sp.search(q=search_term, type='track', limit=10)
data = []
for track in results["tracks"]["items"]:
track_name = track["name"]
track_id = track["id"]
preview_url = track["preview_url"]
artist_name = track["artists"][0]["name"]
thumbnail_url = track['album']['images'][0]['url']
data.append({
"title": track_name,
"artist": artist_name,
"image": thumbnail_url,
"id": track_id,
"preview_url": preview_url
})
return data
def get_youtube_link(song, artist):
youtube_query = f"{song} {artist}"
search_results = yt.search(youtube_query, "songs")
with open("youtube_results.json", "w") as file:
json.dump(search_results, file, indent=2)
return search_results[0]['videoId']
def get_audio_features(trackId):
results = sp.audio_features(trackId)
print(results)
return results[0]
def get_track_metadata(trackId_list):
results = sp.tracks(trackId_list)
data = []
for track in results["tracks"]:
track_name = track["name"]
track_id = track["id"]
preview_url = track["preview_url"]
artist_name = track["artists"][0]["name"]
thumbnail_url = track['album']['images'][0]['url']
data.append({
"title": track_name,
"artist": artist_name,
"image": thumbnail_url,
"id": track_id,
"preview_url": preview_url
})
return data
@app.route('/')
def home():
return send_from_directory('frontend/build', 'index.html')
@app.route('/api/search', methods=["POST"])
def search():
search = request.form['search_box']
search_results = search_songs(search)
return jsonify(search_results), 200
@app.route('/api/stream')
def stream():
artist = request.args.get("artist")
song = request.args.get("song")
song_yt_id = get_youtube_link(song, artist)
youtube_url = f"https://www.youtube.com/watch?v={song_yt_id}"
ctx = {
"outtmpl": "-",
"logtostderr": True,
"format": "mp3/bestaudio/best",
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3'
}]
}
buffer = BytesIO()
with redirect_stdout(buffer), YoutubeDL(ctx) as ytdl:
ytdl.download([youtube_url])
return send_file(buffer, mimetype="audio/mpeg")
@app.route('/api/recommend/<trackId>')
def recommend(trackId):
audio_features = get_audio_features(trackId)
keys = ["acousticness","danceability","duration_ms","energy","instrumentalness","key","liveness","loudness","mode","speechiness","tempo","time_signature","valence"]
query_vector = [audio_features.get(key) for key in keys]
recommendations = getRecommendations(query_vector=query_vector)
song_data = get_track_metadata([track.get("id") for track in recommendations])
return jsonify(song_data)
if __name__ == '__main__':
app.run(debug=True)