I’ve been thinking about building a plugin for MPD that would allow it to talk with Google Music for a while now. Currently, when I purchase music from Google Music, I have to download it and push the new music up to my local music share on my network. Once my music is there, MPD can stream it without a problem… I love MPD!

I started to investigate the idea this weekend. I found these two projects that I wanted to share with you guys. They helped me prove out that the plugin I want to write is possible without TOO much work.

Unofficial Google Music API - An unofficial, opensource API written in Python for Google Music.
MPC - A linux terminal based MPD client.

First, by modifying the example.py in the Google Music API a little I was able to create a script that would sign into Google Music, find the first song in my library and grab the streaming URL of that song.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from getpass import getpass
from gmusicapi import Api
def ask_for_credentials():
    """Make an instance of the api and attempts to login with it.
    Return the authenticated api.
    """
    api = Api()
    logged_in = False
    attempts = 0
    while not logged_in and attempts < 3:
        email = raw_input("Email: ")
        password = getpass()
        logged_in = api.login(email, password)
        attempts += 1
    return api
def demonstration():
    """Demonstrate some api features."""
    api = ask_for_credentials()
    if not api.is_authenticated():
        print "Sorry, those credentials weren't accepted."
        return
    print "Successfully logged in."
    print
    #Get all of the users songs.
    #library is a big list of dictionaries, each of which contains a single song.
    print "Loading library...",
    library = api.get_all_songs()
    print "done."
    print len(library), "tracks detected."
    print
    #Show some info about a song. There is no guaranteed order;
    # this is essentially a random song.
    first_song = library[0]
    print "The first song I see is '{}' by '{}'.".format(
        first_song["name"],
        first_song["artist"])
    #We're going to create a new playlist and add a song to it.
    #Songs are uniquely identified by 'song ids', so let's get the id:
    song_id = first_song["id"]
    print (api.get_stream_url(song_id))
    #It's good practice to logout when finished.
    api.logout()
    print "All done!"
if __name__ == '__main__':
    demonstration()

When running the script above, it’ll ask for a Google account and password. Once they are entered the script will login and load the library of the account. It will grab the first song in the library and output the URL to stream the song. Now that I had the streaming URL, I needed a way to add that to my MPD queue.

mpc clear
mpc add "URL FROM SCRIPT ABOVE"
mpc play

Low and behold! When I hit play the song started playing! So now we know that it can work the next step is figuring out if there’s a way to tie the Google Music library in with MPD’s library.


Gregory Strike

Husband, father, IT dude & blogger wrapped up into one good looking package.