#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:"
spotify_new_prefix = "https://spotify.link"
u = "unknown"
if (ur.startswith(spotify_url_prefix)):
u = "url"
elif (ur.startswith(spotify_uri_prefix)):
u = "uri"
elif (ur.startswith(spotify_new_prefix)):
u = "new"
return u
## Translate new Spotify URL format into full verbose ID
# \param newurl A Spotify URL of the new type
def newurl_expand(newurl):
req = requests.get(newurl)
# If Spotify just actually used HTTP like God intended, we could just use
# return(r.url)
# Intead, we're gonna have to parse the page text for the URL to the track that their JavaScript will do the reload for (kindof insane, on a desktop you don't even see the page)
pagetext = req.text
r = re.search('validateProtocol\("https://open.spotify.com/(.*)\?', pagetext)
url = "https://open.spotify.com/"+r[1]
return(url)
## Takes a URL or URI and figures out if it's a URL or a URI, 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"
# Test UR* type and use tk and lml functions accordingly
ut = ur_type(ur)
if ut == "uri":
f = tk.from_uri(ur)
this_is = f[0]
elif ut == "url":
f = tk.from_url(ur)
this_is = f[0]
elif ut == "new":
ur = newurl_expand(ur)
f = tk.from_url(ur)
this_is = f[0]
# for t in types:
# if (("spotify:"+t+":" in ur) or ("spotify.com/"+t+"/" in ur)):
# this_is = t
return this_is
+## Takes a URL or URI and figures out if it's a URL or a URI, then figures out and returns what type of entity (track, album, playlist, artist, whatever) it is alongside the ID (there's probably a Tekore function I'm missing that does this)
+<p><strong>Enter a Spotify track or album URL:</strong> <input name="URL" id="URL"></p>
+<p><button>Submit</button></p>
+</form>
+
+<?php
+
+if($_POST['URL']) {
+ $url = $_POST['URL'];
+ if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
+ die("Sorry, only valid URLs are currently supported. Take it up with Keith! He probably just forgot to make URIs work too. Or maybe what you pasted in isn't valid as either...");
+ }
+
+ # Run Python script (should rewrite in PHP sometime...)