Created
October 10, 2021 02:21
-
-
Save moniquelive/ecbc6bfe98c8df7416f5b465657ed542 to your computer and use it in GitHub Desktop.
Snippet para fazer o ranking de contribuidores da nossa playlist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import spotipy | |
from spotipy.oauth2 import SpotifyClientCredentials | |
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials()) | |
offset, limit = 0, 100 | |
playlist_id = '6tPcPBeTEoPqaZcR03IzwN' | |
ranking = defaultdict(int) | |
while True: | |
results = sp.playlist_items(playlist_id, offset=offset, limit=limit) | |
if not len(results['items']): | |
break | |
offset += limit | |
for item in results['items']: | |
user_id = item['added_by']['id'] | |
ranking[user_id] += 1 | |
total = sum(ranking.values()) | |
sorted_ranking = dict( | |
reversed(sorted(ranking.items(), key=lambda item: item[1]))) | |
for user, cnt in sorted_ranking.items(): | |
info = sp.user(user) | |
print("%5d (%5.2f %%)\t%-30s" % | |
(cnt, (100 * cnt / total), info['display_name'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Só lembra de definir as variáveis
SPOTIPY_CLIENT_ID
eSPOTIPY_CLIENT_SECRET
Para criar os
CLIENT_ID
eSECRET
: https://developer.spotify.com/dashboard/applications