Page MenuHomePhorge

libmusicleague.py
No OneTemporary

libmusicleague.py

#!/usr/bin/env python3
import tekore as tk
import pyml_config as config
conf = (config.client_id, config.client_secret)
token = tk.request_client_token(*conf)
spotify = tk.Spotify(token)
## Takes a trackid URI and turns it into a track object
# \param uri A Spotify trackid, in the form of a URI (ie. spotify:track:SOMEUUID)
def track_from_id(uri):
track = spotify.track(trackid)
return track
def trackname(track):
name = track.asbuiltin().get('name')
return name
## Takes a track and tries to determine the primary artist on that track
# \param track A Spotify track
# \return artist A string that's hopefully the primary artist's name
def primary_artist(track):
if track is None:
#print(bcolors.FAIL+"Uhh, nothing here, skipping, hope that's cool!"+bcolors.ENDC)
return
#print(bcolors.HEADER+"Now checking this track: "+track.asbuiltin().get("name")+bcolors.ENDC)
albuminfo = track.asbuiltin().get("album")
albumartist = albuminfo.get("artists")[0]["name"] #TODO: Handle when an album has more than one artist!
#print("Album artist is "+albumartist)
#print(track.asbuiltin())
#print(albuminfo)
artistlist = []
for a in track.artists:
artistlist += [a.asbuiltin().get("name")]
#print("Found artist "+a.asbuiltin().get("name"))
if albumartist in artistlist:
#print("One of the track's artists is also the album artist, "+albumartist)
if len(track.artists) == 1:
artist = albumartist
else:
#print("Well, there's other artists, but going with the one whose name is on the album...")
artist = track.artists[0].asbuiltin().get("name")
else:
#print(bcolors.WARNING+"Danger, Will Robinson! No track artist matches the album artist "+albumartist+"!"+bcolors.ENDC)
if "Various Artists" in albumartist:
#print("Taking 'Various Artists' as evidence that this is some sort of compilation, and defaulting to the first track artist listed")
artist = track.artists[0].asbuiltin().get("name")
elif "Cast" in albumartist:
#print("Album artist has 'Cast' in its name, presuming this is a musical and that'll make more sense than crediting the individual performer as is weirdly common")
artist = albumartist
elif "Cast" in albuminfo["name"]:
#print("Album title has 'Cast' in its name, presuming this is a musical and that'll make more sense than crediting the individual performer as is weirdly common")
artist = albumartist
else:
# If we can't match anything else, default to track artist
artist = track.artists[0].asbuiltin().get("name")
#print(bcolors.FAIL+"Oh no! Seems like I didn't catch a potential scenario, couldn't determine artist for "+track.asbuiltin().get("name")+bcolors.ENDC)
#print(" Going with "+bcolors.OKCYAN+artist+bcolors.ENDC)
return artist
# From Blender by way of https://stackoverflow.com/a/287944/2808933, should do something better
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
## Takes a URL or URI and figures out if it's a URL or a URI
# \param ur A Spotify URL or URI
def ur_type(ur):
spotify_url_prefix = "https://open.spotify.com/"
spotify_uri_prefix = "spotify:"
u = "unknown"
if (ur.startswith(spotify_url_prefix)):
u = "url"
elif (ur.startswith(spotify_uri_prefix)):
u = "uri"
return u
## Takes a URL or URI and figures out if it's a URL or a URI (using `urtype()`), then figures out and returns what type of entity (track, album, playlist, artist, whatever) it is
def spotify_type(ur):
types = ["playlist", "album", "track", "artist"]
this_is = "dunno"
for t in types:
if (("spotify:"+t+":" in ur) or ("spotify.com/"+t+"/" in ur)):
this_is = t
return this_is
def play_list(uri):
info = spotify.playlist(uri)
playlist_id = uri
title = info.asbuiltin().get("name")
tracks = spotify.playlist_items(playlist_id)
tracks = spotify.all_items(tracks)
items = []
for t in tracks:
#print(t.track.asbuiltin().get('name'))
artist = primary_artist(t.track)
items.append(t)
return title, items
def playlist_as_textlist(playlist):
l = []
distinction = " - "
for i in playlist[1]:
name = trackname(i.track)
artist = primary_artist(i.track)
l.append(artist+distinction+name)
return l
def print_playlist(playlist):
l = []
title = playlist[0]
distinction = " - "
titleprefix = "###"
itemprefix = "####"
for i in playlist[1]:
name = trackname(i.track)
artist = primary_artist(i.track)
l.append(artist+distinction+name)
print(titleprefix+' '+title, end = '')
print('', *l, sep='\n'+itemprefix+' ')

File Metadata

Mime Type
text/x-python
Expires
Fri, Jan 24, 1:56 AM (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
131343
Default Alt Text
libmusicleague.py (4 KB)

Event Timeline