-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_by_track_popularity.py
101 lines (89 loc) · 4.27 KB
/
sort_by_track_popularity.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
#%%
import spotipy
import os
from spotipy.oauth2 import SpotifyClientCredentials
from spotipy.oauth2 import SpotifyOAuth
from pprint import pprint
import pandas as pd
# Checking that your environment variables are correctly set (if you run this on your OS):
print(f'Client ID:\n{os.getenv("SPOTIPY_CLIENT_ID")}') # Get it here: https://developer.spotify.com/
print(f'Client secret:\n{os.getenv("SPOTIPY_CLIENT_SECRET")}') # Get it here: https://developer.spotify.com/
print(f'Redirect URI:\n{os.getenv("SPOTIPY_REDIRECT_URI")}') # 'http://localhost:8888/callback' (this works fine. Make sure to update it on https://developer.spotify.com/)
# YouTube tutorial for setting up the app and environment variables:
# https://www.youtube.com/watch?v=3RGm4jALukM
# Function
def sort_playlist_by_track_popularity(client, playlist_url):
print("Starting sort_playlist_by_track_popularity")
playlist_id = playlist_url.split("/")[-1].split("?")[0]
print(f"Playlist ID: {playlist_id}")
tracks_data = []
try:
results = client.playlist_items(playlist_id, limit=100)
tracks = []
while results:
tracks.extend(results['items'])
if results['next']:
results = client.next(results)
else:
results = None
print(f"Tracks fetched: {len(tracks)}")
except Exception as e:
print(f"Error fetching playlist items: {e}")
return
for i, track_item in enumerate(tracks):
try:
track = track_item['track']
uri = track['uri']
artist_id = track['artists'][0]['id']
artist = client.artist(artist_id)
artist_name = artist['name']
track_popularity = track['popularity']
genres = artist['genres']
followers = artist['followers']['total']
track_name = track['name']
tracks_data.append({
"Artist name": artist_name,
"Artist id": artist_id,
"Track name": track_name,
"Track popularity": track_popularity,
"Genres": genres,
"Followers": followers,
"uri": uri
})
print(f"Processed track {i+1}/{len(tracks)} by {artist_name} - Popularity: {track_popularity}")
except Exception as e:
print(f"Error processing track {i+1}: {e}")
# Sort tracks_data by artist popularity and followers before creating DataFrame
sorted_tracks_data = sorted(tracks_data, key=lambda x: x["Track popularity"], reverse=True)
df_tracks_sorted = pd.DataFrame(sorted_tracks_data)
print("Tracks sorted by artist popularity and followers")
try:
user_id = client.current_user()["id"]
print(f"User ID: {user_id}")
original_playlist = client.playlist(playlist_id)
original_name = original_playlist['name']
new_playlist_name = original_name + " - sorted by track popularity"
new_playlist = client.user_playlist_create(user_id, new_playlist_name, public=False)
new_playlist_id = new_playlist["id"]
track_uris = df_tracks_sorted["uri"].tolist()
print(f"Creating new playlist with ID: {new_playlist_id}")
for i in range(0, len(track_uris), 100):
client.playlist_add_items(new_playlist_id, track_uris[i:i+100])
print(f"Added tracks {i+1}-{min(i+100, len(track_uris))} to new playlist")
except Exception as e:
print(f"Error during playlist creation or track addition: {e}")
return df_tracks_sorted
# %%
#sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())
# This will authenticate you on your web browser:
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=os.getenv("SPOTIPY_CLIENT_ID"),
client_secret=os.getenv("SPOTIPY_CLIENT_SECRET"),
redirect_uri=os.getenv("SPOTIPY_REDIRECT_URI"),
scope="playlist-modify-public playlist-modify-private"))
# Italo-Disco TEST
# playlist_url = "https://open.spotify.com/playlist/2qajcUJ7x242tTuBpEKUKx"
# Enter your playlist URL
playlist_url = input("Please enter the playlist URL: ")
df_sorted = sort_playlist_by_track_popularity(sp, playlist_url)
df_sorted.head(10)
# %%