Я новичок в программировании.Я хочу сделать анализ настроений в сочетании с анализом аудиофункций моих плейлистов Spotify с использованием SpotiPy и PyLyrics.
Я пытаюсь заставить этот код работать, но мне удалось получить только одну песню для моего 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?