Last active
July 5, 2018 15:18
-
-
Save umrysh/678ffedc041012133c777a9082a17c03 to your computer and use it in GitHub Desktop.
Listen to Google Play Music playlists from the terminal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####################################################################### | |
# You will need gmusicapi (https://github.com/simon-weber/gmusicapi) | |
# You can install it with: | |
# pip install gmusicapi | |
####################################################################### | |
import sys,warnings,subprocess,os,time | |
from gmusicapi import Mobileclient | |
from random import shuffle | |
import curses | |
warnings.filterwarnings("ignore") | |
# Clear screen | |
subprocess.call('clear',shell=True) | |
def sendmessage(message): | |
subprocess.Popen(['notify-send', "Now Playing...%s" % message]) | |
return | |
def play(song): | |
with open(os.devnull, 'w') as temp: | |
proc = subprocess.Popen(["mplayer", "-noconfig", "all","%s" % song], stdin=subprocess.PIPE, stdout=temp, stderr=temp) | |
return proc | |
try: | |
playing = None | |
songQueue = [] | |
api = Mobileclient() | |
#################################################################################################### | |
# Use this next command to find a device ID to use. # | |
# Uncomment the next api.login line, run the script and it will print out your valid device ids. # | |
# Fill in the deviceID variable and then comment it out again. # | |
#api.login('<email address>', '<password>', '1234567890abcdef') # | |
#################################################################################################### | |
deviceID = "" | |
api.login('<email address>', '<password>', deviceID) | |
if not api.is_authenticated(): | |
print "Credentials were not accepted. Exiting..." | |
sys.exit(1) | |
# Was there a command line argument for a playlist? | |
if len(sys.argv) >= 2: | |
playlistWanted = sys.argv[1] | |
playlists = api.get_all_user_playlist_contents() | |
allSongs = api.get_all_songs() | |
found = False | |
for playlist in playlists: | |
if playlist["name"].lower() == playlistWanted.lower() : | |
found = True | |
songQueue = playlist["tracks"] | |
break | |
if found: | |
stdscr = curses.initscr() | |
curses.cbreak() | |
stdscr.timeout(1) | |
stdscr.keypad(1) | |
count = 0 | |
# Get all playlist songs into a queue and shuffle | |
shuffle(songQueue) | |
# Play the first song | |
song = songQueue[count] | |
playing = play(api.get_stream_url(song['trackId'],deviceID)) | |
# Print out track information to screen | |
if "track" in song: | |
stdscr.addstr(0,2,"%s - %s" % (song["track"]['title'],song["track"]['artist'])) | |
stdscr.clrtoeol() | |
stdscr.refresh() | |
sendmessage("%s - %s" % (song["track"]['title'],song["track"]['artist'])) | |
else: | |
for currentSong in allSongs: | |
if currentSong["id"] == song["trackId"]: | |
stdscr.addstr(0,2,"%s - %s" % (currentSong['title'],currentSong['artist'])) | |
stdscr.clrtoeol() | |
stdscr.refresh() | |
sendmessage("%s - %s" % (currentSong['title'],currentSong['artist'])) | |
break | |
key = '' | |
while True: | |
key = stdscr.getch() | |
if key == curses.KEY_RIGHT: | |
# Skipping song | |
stdscr.addstr(0,2,"<Next Song>") | |
stdscr.clrtoeol() | |
stdscr.refresh() | |
playing.terminate() | |
time.sleep(2) | |
if isinstance(playing, subprocess.Popen) and playing.poll() != None: | |
# Song has stopped playing, play the next | |
count = count + 1 | |
if count >= len(songQueue): | |
count = 0 | |
song = songQueue[count] | |
playing = play(api.get_stream_url(song['trackId'],deviceID)) | |
if "track" in song: | |
stdscr.addstr(0,2,"%s - %s" % (song["track"]['title'],song["track"]['artist'])) | |
stdscr.clrtoeol() | |
stdscr.refresh() | |
sendmessage("%s - %s" % (song["track"]['title'],song["track"]['artist'])) | |
else: | |
for currentSong in allSongs: | |
if currentSong["id"] == song["trackId"]: | |
stdscr.addstr(0,2,"%s - %s" % (currentSong['title'],currentSong['artist'])) | |
stdscr.clrtoeol() | |
stdscr.refresh() | |
sendmessage("%s - %s" % (currentSong['title'],currentSong['artist'])) | |
break | |
else: | |
print "Could not locate playlist" | |
else: | |
print "You must specify a playlist" | |
except Exception as e: | |
print str(e) | |
finally: | |
if isinstance(playing, subprocess.Popen): | |
playing.terminate() | |
if api: | |
api.logout() | |
curses.endwin() | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment