我是编程新手。我想使用SpotiPy和PyLyrics对我的Spotify播放列表进行情感分析和音频功能分析。
我正在尝试使此代码正常工作,但是即使播放列表很大,我也只能将一首歌曲添加到CSV中。
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import json
import csv
import pylyrics3
from nltk.sentiment.vader import SentimentIntensityAnalyzer as SIA
client_credentials_manager = SpotifyClientCredentials(client_id='#########', client_secret='#########')
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
sentiment_analyzer = SIA()
def get_features(track_id):
features_results = sp.audio_features(track_id)
json_features = json.dumps(features_results)
features_data = json.loads(json_features)
features_list = list(features_data[0].values())
return features_list
playlist_ids = [
"6itzpx6OEo76NWSpy0f3fc",
"3itylhqRqKPS4nOgWmU7lU",
"63UBtWxXAtUIR6djuNhGj9",
"6tZo11hLgEslYdMP5wu4q8",
"3XXl0JH2uLXpmO5EbH6XPb"
]
# Audio features
feature_names = [
"danceability",
"energy",
"key",
"loudness",
"mode",
"speechiness",
"acousticness",
"instrumentalness",
"liveness",
"valence",
"tempo",
"type",
"id",
"uri",
"track_href",
"analysis_url",
"duration_ms",
"time_signature"
]
username = '#########'
# Write data to CSV file
data_file = open('data.csv','w')
writer = csv.writer(data_file)
# Write header
writer.writerow(['track_id', 'playlist_id', 'date_added', 'track_name', 'first_artist'] + feature_names + ['lyrics', 'neg', 'neu', 'pos', 'compound'])
for playlist_id in playlist_ids:
print('Querying playlist: ' + str(playlist_id))
repeat_query = True
offset_n = 0
for i in range(2):
# Query Spotify API
if i > 0:
print('Repeating query')
offset_n += 50
results = sp.user_playlist_tracks(username, playlist_id, offset=offset_n)
json_results = json.dumps(results)
data = json.loads(json_results)
# Write rows
for track in data['items']:
track_id = track['track']['id']
date_added = track['added_at']
track_name = track['track']['name']
first_artist = track['track']['artists'][0]['name']
# Track features
features = get_features(track_id)
# Try to get lyrics, if available
lyrics = ''
try:
lyrics = pylyrics3.get_song_lyrics(first_artist, track_name)
except:
pass
# Sentiment Analysis
neg = None
neu = None
pos = None
compound = None
if lyrics:
snt = sentiment_analyzer.polarity_scores(lyrics)
neg = snt['neg']
neu = snt['neu']
pos = snt['pos']
compound = snt['compound']
writer.writerow([track_id, playlist_id, date_added, track_name, first_artist] + features + [lyrics] + [neg, neu, pos, compound])
#if data['total'] < 50:
# break ????
print('Done querying')
data_file.close()
有人知道这里出了什么问题以及如何将所有曲目从我的播放列表复制到csv吗?
答案 0 :(得分:2)
import random
number = random.randint(1,10)
lives = 3
Success = False
while lives > 0:
guess = int(input("What is your guess between 1 and 10? \r\n"))
if guess > number:
print("Too high! Go lower. \r\n")
lives -= 1
elif guess < number:
print("Too low! Go higher. \r\n")
lives -= 1
elif guess == number:
print("Congratulations, you win!")
global Success = True
break
if Success != True:
print("Sorry. Try again! The number was ", number, ".")