Skip to content
This repository
Simon Weber July 23, 2013
file 92 lines (67 sloc) 2.632 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from getpass import getpass

from gmusicapi import Mobileclient


def ask_for_credentials():
    """Make an instance of the api and attempts to login with it.
Return the authenticated api.
"""

    # We're not going to upload anything, so the Mobileclient is what we want.
    api = Mobileclient()

    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 demonstrate():
    """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['title'].encode('utf-8'),
        first_song['artist'].encode('utf-8'))

    # 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 "I'm going to make a new playlist and add that song to it."
    print "I'll delete it when we're finished."
    print
    playlist_name = raw_input('Enter a name for the playlist: ')

    # Like songs, playlists have unique ids.
    # Google Music allows more than one playlist of the same name;
    # these ids are necessary.
    playlist_id = api.create_playlist(playlist_name)
    print 'Made the playlist.'
    print

    # Now let's add the song to the playlist, using their ids:
    api.add_songs_to_playlist(playlist_id, song_id)
    print 'Added the song to the playlist.'
    print

    # We're all done! The user can now go and see that the playlist is there.
    # The web client syncs our changes in real time.
    raw_input('You can now check on Google Music that the playlist exists.\n'
              'When done, press enter to delete the playlist:')
    api.delete_playlist(playlist_id)
    print 'Deleted the playlist.'

    # It's good practice to logout when finished.
    api.logout()
    print 'All done!'

if __name__ == '__main__':
    demonstrate()
Something went wrong with that request. Please try again.