Skip to content

Instantly share code, notes, and snippets.

@luads
Forked from Timmmm/lastfm_to_gmusic.py
Last active November 28, 2016 02:30
Show Gist options
  • Save luads/e55f7e394d490006eda6 to your computer and use it in GitHub Desktop.
Save luads/e55f7e394d490006eda6 to your computer and use it in GitHub Desktop.
Imports any playlist into gmusic
#!/usr/bin/env python
# Import any playlist (within the script) to Google Music All Access playlist.
#
# Based on Tim Hutt's script:
# https://gist.github.com/Timmmm/6572592
#
# Instructions:
# 1. Write the playlist down to the file
# 2. Install `gmusicapi` using `pip`: `pip install gmusicapi`
# 3. Run it! `python playlist-gmusic.py`.
#
# Sample JS code to rip off a Grooveshark playlist using the console,
# just run the 3 lines with the playlist screen open.
# Or get a Grooveshark API key and make it smart :)
#
# musics = [];
# $('.song.grid-item').each(function(){ musics.push("('"+$(this).find('.song').text()+"', '"+$(this).find('.artist').text()+"')") });
# musics
#
# Specify the playlist name
PLAYLIST_NAME = '90s'
# Write the song title and the artist's name
tracks = [
('Interstate Love Song ', 'Stone Temple Pilots'),
('Love Rears Its Ugly Head ', 'Living Colour'),
('Walkin On The Sun ', 'Smash Mouth'),
('Closing Time ', 'Semi Sonic'),
]
import urllib, urllib2
import gmusicapi
import getpass
from xml.etree.ElementTree import *
def main():
# Gather required info.
google_username = raw_input('Google username: ').strip()
google_password = getpass.getpass('Google password: ')
# Log in.
api = gmusicapi.Mobileclient()
if not api.login(google_username, google_password, gmusicapi.Mobileclient.FROM_MAC_ADDRESS):
print 'Login error'
return
playlist_id = api.create_playlist(PLAYLIST_NAME)
to_add = []
for target in tracks:
try:
res = api.search(target[0] + ' ' + target[1], max_results=1)
to_add.append(res['song_hits'][0]['track']['nid'])
except:
pass
print('Got ' + str(len(to_add)) + ' songs so far out of ' + str(len(tracks)))
print('Adding ' + str(len(to_add)) + ' songs to playlist')
api.add_songs_to_playlist(playlist_id, to_add)
print('Done!')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment