spotify_playlist.py
generate_credentials()
¶
Generate the token. Please respect these credentials :)
Source code in mps_youtube/commands/spotify_playlist.py
def generate_credentials():
"""Generate the token. Please respect these credentials :)"""
credentials = oauth2.SpotifyClientCredentials(
client_id='6451e12933bb49ed8543d41e3296a88d',
client_secret='40ef54678fe441bd9acd66f5d5c34e69')
return credentials
search_playlist(term, spotify=None)
¶
Search for Spotify playlist.
Source code in mps_youtube/commands/spotify_playlist.py
@command(r'splaylist\s*(.*[-_a-zA-Z0-9].*)?', 'splaylist')
def search_playlist(term, spotify=None):
"""Search for Spotify playlist. """
# pylint: disable=R0914,R0912
if has_spotipy:
if not term:
show_message("Enter playlist url:", c.g, update=True)
term = input("> ")
if not term or len(term) < 2:
g.message = c.r + "Not enough input!" + c.w
g.content = None#content.generate_songlist_display()
return
if not spotify:
credentials = generate_credentials()
token = credentials.get_access_token()
spotify = spotipy.Spotify(auth=token)
try:
playlist, tracks = grab_playlist(spotify, term)
except TypeError:
tracks = None
if not tracks:
show_message("Playlist '%s' not found!" % term)
return
if not playlist['tracks']['total']:
show_message("Playlist '%s' by '%s' has 0 tracks!" % (playlist['name'], playlist['owner']['id']))
return
msg = "%s%s%s by %s%s%s\n\n" % (c.g, playlist['name'], c.w, c.g, playlist['owner']['id'], c.w)
msg += "Enter to begin matching or [q] to abort"
g.message = msg
g.content = "Tracks:\n"
for n, track in enumerate(tracks, 1):
trackname = '{0:<20} - {1}'.format(track['artists'][0]['name'], track['name'])
g.content += "%03s %s" % (n, trackname)
g.content += "\n"
screen.update()
entry = input("Continue? [Enter] > ")
if entry == "":
pass
else:
show_message("Playlist search abandoned!")
return
songs = []
screen.clear()
itt = _match_tracks(tracks)
stash = config.SEARCH_MUSIC.get, config.ORDER.get
config.SEARCH_MUSIC.value = True
config.ORDER.value = "relevance"
try:
songs.extend(itt)
except KeyboardInterrupt:
util.xprint("%sHalted!%s" % (c.r, c.w))
finally:
config.SEARCH_MUSIC.value, config.ORDER.value = stash
if songs:
util.xprint("\n%s / %s songs matched" % (len(songs), len(tracks)))
input("Press Enter to continue")
msg = "Contents of playlist %s%s - %s%s %s(%d/%d)%s:" % (
c.y, playlist['owner']['id'], playlist['name'], c.w, c.b, len(songs), len(tracks), c.w)
failmsg = "Found no playlist tracks for %s%s%s" % (c.y, playlist['name'], c.w)
paginatesongs(songs, msg=msg, failmsg=failmsg)
else:
g.message = "spotipy module must be installed for Spotify support\n"
g.message += "see https://pypi.python.org/pypi/spotipy/"
search_user(term)
¶
Search for Spotify user playlists.
Source code in mps_youtube/commands/spotify_playlist.py
@command(r'suser\s*(.*[-_a-zA-Z0-9].*)?', 'suser')
def search_user(term):
"""Search for Spotify user playlists. """
# pylint: disable=R0914,R0912
if has_spotipy:
if not term:
show_message("Enter username:", c.g, update=True)
term = input("> ")
if not term or len(term) < 2:
g.message = c.r + "Not enough input!" + c.w
g.content = None#content.generate_songlist_display()
return
credentials = generate_credentials()
token = credentials.get_access_token()
spotify = spotipy.Spotify(auth=token)
playlists = spotify.user_playlists(term)
links = []
check = 1
g.content = "Playlists:\n"
while True:
for playlist in playlists['items']:
if playlist['name'] is not None:
g.content += (u'{0:>2}. {1:<30} ({2} tracks)'.format(
check, playlist['name'],
playlist['tracks']['total']))
g.content += "\n"
links.append(playlist)
check += 1
if playlists['next']:
playlists = spotify.next(playlists)
else:
break
g.message = c.g + "Choose your playlist:" + c.w
screen.update()
choice = int(input("> "))
playlist = links[choice-1]
search_playlist(playlist['external_urls']['spotify'], spotify=spotify)
else:
g.message = "spotipy module must be installed for Spotify support\n"
g.message += "see https://pypi.python.org/pypi/spotipy/"
show_message(message, col='', update=False)
¶
Show message using col, update screen if required.
Source code in mps_youtube/commands/spotify_playlist.py
def show_message(message, col=c.r, update=False):
""" Show message using col, update screen if required. """
g.content = content.generate_songlist_display()
g.message = col + message + c.w
if update:
screen.update()