Repository navigation
Expand file tree
/
Copy pathinterface.py
More file actions
303 lines (256 loc) · 10.7 KB
/
Copy pathinterface.py
File metadata and controls
303 lines (256 loc) · 10.7 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from flask import Flask, render_template, request, jsonify, redirect, session, url_for, flash
import requests
import csv
import mysql.connector
from collections import Counter
CLIENT_ID = "01dfa2aba3384ac4ba4b278af29d2f6d"
CLIENT_SECRET = "e672de7d54d34ef4b745be950e084e5b"
REDIRECT_URI = "http://localhost:8888/spotify_callback"
SCOPE = "user-top-read playlist-modify-public playlist-modify-private"
# Flask App Initialization
app = Flask(__name__)
app.secret_key = "your_secret_key"
# Database Configuration
db_config = {
'host': 'localhost',
'user': 'root',
'password': '20050616',
'database': 'final_project'
}
# Database Connection
def get_db_connection():
return mysql.connector.connect(**db_config)
# Function to reset the table
def reset_table():
try:
connection = get_db_connection()
cursor = connection.cursor()
# Truncate the 'playlist' table to remove all rows and reset auto-increment
cursor.execute("TRUNCATE TABLE playlist")
connection.commit()
print("Table 'playlist' has been reset.")
except mysql.connector.Error as e:
print(f"Database Error: {e}")
finally:
if 'connection' in locals() and connection.is_connected():
connection.close()
def insert_csv_data_into_playlist(csv_file):
reset_table()
try:
with open(csv_file, mode='r', encoding='utf-8-sig') as file:
csv_reader = csv.DictReader(file)
# Print the fieldnames (headers) to ensure they're correct
print(f"CSV Headers: {csv_reader.fieldnames}")
connection = get_db_connection()
cursor = connection.cursor()
insert_query = "INSERT INTO playlist (song_name, singer_name, song_language, song_id) VALUES (%s, %s, %s,%s)"
for row in csv_reader:
print(f"Row data: {row}") # Print the row to check its contents
cursor.execute(insert_query, (row['song_name'], row['singer_name'], row['song_language'],row['track_id']))
connection.commit()
print(f"Data from {csv_file} inserted successfully.")
except mysql.connector.Error as e:
print(f"Database Error: {e}")
except Exception as e:
print(f"Error: {e}")
finally:
if 'connection' in locals() and connection.is_connected():
connection.close()
# Call the function to insert data from the CSV
insert_csv_data_into_playlist('playlist.csv')
# Fetch data from the 'playlist' table
def fetch_playlist_data():
try:
connection = get_db_connection()
cursor = connection.cursor(dictionary=True)
query='''
SELECT p.song_name, p.singer_name,
CASE
WHEN p.singer_name IN (SELECT singer_name FROM Singer) THEN
(SELECT singer_gender FROM Singer WHERE Singer.singer_name = p.singer_name LIMIT 1)
ELSE
'unknown'
END AS singer_gender,
p.song_language,
CASE
WHEN p.song_name IN (SELECT song_name FROM Song) THEN
(SELECT song_genre FROM Song WHERE Song.song_name = p.song_name LIMIT 1)
ELSE
'unknown'
END AS song_genre,
CASE
WHEN p.song_name IN (SELECT song_name FROM Song) THEN
(SELECT song_timing FROM Song WHERE Song.song_name = p.song_name LIMIT 1)
ELSE
'unknown'
END AS song_timing,
CASE
WHEN p.song_name IN (SELECT song_name FROM Album) THEN
(SELECT album_name FROM Album WHERE Album.song_name = p.song_name LIMIT 1)
ELSE
'unknown'
END AS album_name
FROM playlist p
'''
cursor.execute(query)
return cursor.fetchall()
except mysql.connector.Error as e:
print(f"Database Error: {e}")
return []
finally:
if 'connection' in locals() and connection.is_connected():
connection.close()
# Flask Routes
@app.route('/')
def index():
# Fetch playlist data
playlist_data = fetch_playlist_data()# Prepare data for the pie charts
genres = [row['song_genre'] for row in playlist_data]
languages = [row['song_language'] for row in playlist_data]
singers = [row['singer_name'] for row in playlist_data]
timings = [row['song_timing'] for row in playlist_data]
# Count occurrences for each category
genre_count = Counter(genres)
language_count = Counter(languages)
singer_count = Counter(singers)
timing_count = Counter(timings)
# Pass the data to the template
return render_template('interface.html',
playlist=playlist_data,
genre_count=genre_count,
language_count=language_count,
singer_count=singer_count,
timing_count=timing_count)
@app.route('/create_spotify_playlist', methods=['POST'])
def create_spotify_playlist():
try:
filtered_songs = request.json.get('filtered_songs', [])
if not filtered_songs:
return jsonify({'error': '沒有選擇任何歌曲'}), 400
if 'spotify_token' not in session:
# 保存篩選後的歌曲到 session
session['filtered_songs'] = filtered_songs
auth_url = (
"https://accounts.spotify.com/authorize"
f"?client_id={CLIENT_ID}"
"&response_type=code"
f"&redirect_uri={REDIRECT_URI}"
f"&scope={SCOPE}"
"&show_dialog=true"
)
return jsonify({'redirect': auth_url})
# 直接使用 create_spotify_playlist_with_songs
playlist_link = create_spotify_playlist_with_songs(session['spotify_token'], filtered_songs)
return jsonify({'playlist_link': playlist_link})
print("playlist link",playlist_link)
except Exception as e:
print(f"Error creating playlist: {str(e)}")
return redirect(url_for('index'))
@app.route('/spotify_callback')
def spotify_callback():
try:
print("Received Spotify callback") # Debug log
# Get authorization code
code = request.args.get("code")
print(f"Received auth code: {code[:10]}...") # 新增日誌
if not code:
print("No authorization code received") # Debug log
return redirect(url_for('index'))
# Get access token
print(f"Got authorization code: {code[:10]}...")
access_token = get_spotify_token(code)
print("Successfully got access token") # Debug log
session['spotify_token'] = access_token
# Create playlist and add songs
filtered_songs = session.get('filtered_songs', [])
playlist_link = create_spotify_playlist_with_songs(access_token,filtered_songs)
# Render template with playlist
return display_playlist_page(playlist_link)
except Exception as e:
print(f"Error in Spotify callback: {str(e)}")
return redirect(url_for('index'))
def get_spotify_token(code):
print("Requesting access token") # Debug log
try:
response = requests.post(
"https://accounts.spotify.com/api/token",
data={
"grant_type": "authorization_code",
"code": code,
"redirect_uri": REDIRECT_URI,
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET
}
)
response_data = response.json()
if 'error' in response_data:
raise Exception(f"Spotify API Error: {response_data['error']}")
if 'access_token' not in response_data:
raise Exception("No access token in response")
return response_data["access_token"]
except Exception as e:
print(f"Error in get_spotify_token: {str(e)}")
raise
def create_spotify_playlist_with_songs(access_token,filtered_songs):
PLAYLIST_NAME = "My Filtered Playlist"
PLAYLIST_DESC = "Filtered playlist"
headers = {"Authorization": f"Bearer {access_token}"}
# Get user ID
user_response = requests.get("https://api.spotify.com/v1/me", headers=headers)
user_id = user_response.json()["id"]
# Create playlist
playlist = requests.post(
f"https://api.spotify.com/v1/users/{user_id}/playlists",
headers=headers,
json={
"name": PLAYLIST_NAME,
"description": PLAYLIST_DESC,
"public": True
}
).json()
# Add songs
playlist_id = playlist["id"]
playlist_data = fetch_playlist_data()
filtered_playlist_data = [song for song in playlist_data if song['song_name'] in filtered_songs]
for song in filtered_playlist_data:
search_query = f"track:{song['song_name']} artist:{song['singer_name']}"
search_response = requests.get(
"https://api.spotify.com/v1/search",
headers=headers,
params={
"q": search_query,
"type": "track",
"limit": 1
}
)
print(search_response)
if search_response.json()["tracks"]["items"]:
track_uri = search_response.json()["tracks"]["items"][0]["uri"]
requests.post(
f"https://api.spotify.com/v1/playlists/{playlist_id}/tracks",
headers=headers,
json={"uris": [track_uri]}
)
return playlist["external_urls"]["spotify"]
def display_playlist_page(playlist_link):
playlist_data = fetch_playlist_data()
return render_template('interface.html',
playlist=playlist_data,
genre_count=Counter(row['song_genre'] for row in playlist_data),
language_count=Counter(row['song_language'] for row in playlist_data),
singer_count=Counter(row['singer_name'] for row in playlist_data),
timing_count=Counter(row['song_timing'] for row in playlist_data),
playlist_link=playlist_link
)
@app.route('/show_playlist')
def show_playlist():
playlist_data = fetch_playlist_data()
return render_template('interface.html',
playlist=playlist_data,
genre_count=Counter(row['song_genre'] for row in playlist_data),
language_count=Counter(row['song_language'] for row in playlist_data),
singer_count=Counter(row['singer_name'] for row in playlist_data),
timing_count=Counter(row['song_timing'] for row in playlist_data)
)
if __name__ == '__main__':
app.run(debug=True, port=8888)