Page MenuHomePhorge

sort_hot100.py
No OneTemporary

sort_hot100.py

#!/usr/bin/env python3
import csv
import json
import operator
import pprint
import sys
import tekore as tk
import pyml_config as config
import libmusicleague as lml
import pprint
def play_list(pid):
tracks = spotify.playlist_items(pid)
tracks = spotify.all_items(tracks)
items = []
for t in tracks:
items.append(t)
return items
def uri_list(pid):
tracks = spotify.playlist_items(pid)
tracks = spotify.all_items(tracks)
items = []
for t in tracks:
pprint.pp(t)
items.append(t.dict().get('uri'))
return items
def sort_hot(playlist):
l = []
along = []
pid = playlist.id
items = play_list(pid)
for idx, i in enumerate(items):
l.append({"popularity": f'{i.track.popularity:03d}', "current_position": idx, "uri": i.track.uri, "title": i.track.name})
along.append(i.track.uri)
# l.append([f'{i.track.popularity:03d}')+" | "+trackname(i.track)+" by "+primary_artist(i.track)])
l.sort(key=operator.itemgetter('popularity'))
for rank, line in enumerate(l):
print(rank, line)
# We'll have to get this item's current location, annoyingly, each time. You'd think we could just use the URIs but maybe the same song can be on a playlist multiple times? To avoid repeated API calls, we'll reorder an in-memory local list as well and use that to query positions
current_position = along.index(line['uri'])
spotify.playlist_reorder(playlist_id=pid, range_start=current_position, range_length=1, insert_before=rank)
a = along.pop(current_position)
along.insert(rank, a)
# Set up Spotify connection using client token
conf = (config.client_id, config.client_secret)
#token = tk.request_client_token(*conf)
token = tk.prompt_for_user_token(*conf, redirect_uri="https://example.com/callback", open_browser=True, scope=tk.scope.every)
spotify = tk.Spotify(token)
# Get URI or URL from first argument
ur = str(sys.argv[1])
urt = lml.ur_type(ur)
st = lml.spotify_type(ur)
if st == "playlist":
if urt == "url":
uri = tk.from_url(ur)[1]
pl = spotify.playlist(uri)
sort_hot(pl)
elif urt == "uri":
pl = lml.track_from_id(ur)
sort_hot(pl)

File Metadata

Mime Type
text/x-python
Expires
Sun, Jun 29, 4:29 PM (1 d, 9 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
168810
Default Alt Text
sort_hot100.py (2 KB)

Event Timeline