Skip to content

Instantly share code, notes, and snippets.

@moniquelive
Created October 10, 2021 02:21
Show Gist options
  • Save moniquelive/ecbc6bfe98c8df7416f5b465657ed542 to your computer and use it in GitHub Desktop.
Save moniquelive/ecbc6bfe98c8df7416f5b465657ed542 to your computer and use it in GitHub Desktop.
Snippet para fazer o ranking de contribuidores da nossa playlist
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']))
@moniquelive
Copy link
Author

moniquelive commented Oct 10, 2021

Só lembra de definir as variáveis SPOTIPY_CLIENT_ID e SPOTIPY_CLIENT_SECRET
Para criar os CLIENT_ID e SECRET: https://developer.spotify.com/dashboard/applications

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment