-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify_api.py
68 lines (57 loc) · 2.09 KB
/
spotify_api.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
import os
import pandas as pd
from flask import Flask, render_template, render_template_string, request, flash
import spotipy
import re
from spotipy.oauth2 import SpotifyClientCredentials
app = Flask(__name__)
# Set up Spotify API credentials
client_id = ""
client_secret = ""
client_credentials_manager = SpotifyClientCredentials(client_id, client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
def get_track_info(song_link):
track_id = re.search(r"track/(\w+)", song_link)
if track_id:
track_id = track_id.group(1)
else:
raise ValueError("Invalid song link")
track = sp.track(track_id)
audio_features = sp.audio_features(track_id)[0]
audio_analysis = sp.audio_analysis(track_id)
artist = track['artists'][0]['name']
track_name = track['name']
album = track['album']['name']
release_date = track['album']['release_date']
popularity = track['popularity']
duration_ms = track['duration_ms']
album_cover_url = track['album']['images'][0]['url'] if track['album']['images'] else None
preview_url = track['preview_url']
return {
'artist': artist,
'track_name': track_name,
'album': album,
'release_date': release_date,
'popularity': popularity,
'duration_ms': duration_ms,
'album_cover_url': album_cover_url,
'preview_url': preview_url,
'audio_features': audio_features,
'audio_analysis': audio_analysis
}
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
try:
song_link = request.form["song_link"]
print(f"Song link received: {song_link}")
track_info = get_track_info(song_link)
print(f"Track info: {track_info}")
return render_template("dashboard.html", track_info=track_info)
except Exception as e:
print(f"Error: {e}")
flash(f"Error: {e}")
return render_template("index.html")
if __name__ == "__main__":
app.secret_key = "test" # Replace with your own secret key
app.run(debug=True)