Skip to content

Instantly share code, notes, and snippets.

@Timmmm
Last active June 1, 2018 12:43
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save Timmmm/6572592 to your computer and use it in GitHub Desktop.
Save Timmmm/6572592 to your computer and use it in GitHub Desktop.
Convert Last.fm loved tracks to your Google Play Store Music Play All Access Subscription Service by Google™. It creates a new playlist rather than adding the tracks to your library willy-nilly. See code for more details.
#!/usr/bin/env python
# Lastfm loved tracks to Google Music All Access playlist. As noted in the comments you do need the All Access subscription thing otherwise it will always find 0 songs.
#
# Written by Tim Hutt, tdhutt@gmail.com, based on this script:
#
# https://gist.github.com/oquno/3664731
#
# Today is the 15th of September 2013.
#
# Not really tested!
#
# Update on 20th of September 2016:
#
# * Changed login API to match gmusicapi (not tested at all)
#
# Instructions:
#
# 0. Install python and pip.
# 1. Download this to a file `lastfm_to_gmusic.py`
# 2. Make it executable: `chmod +x lastfm_to_gmusic.py`
# 3. Install `gmusicapi` using `pip`: `pip install gmusicapi`
# 4. Get a last.fm API key here: http://www.last.fm/api/account/create
# 5. Run it! `./lastfm_to_gmusic.py`.
#
# Troubleshooting:
#
# 1. It says "Login error": Go to your gmail and check that it didn't block any "suspicious logins".
# 2. It doesn't find any tracks: Update gmusicapi.
# 3. Something else: Email me. There's a small chance I'll reply.
#
#
import urllib, urllib2
import gmusicapi
from xml.etree.ElementTree import *
def main():
# Gather required info.
google_username = raw_input("Google username: ").strip()
google_password = raw_input("Google password: ")
lastfm_username = raw_input("Lastfm username: ").strip()
lastfm_key = raw_input("Lastfm API key: ").strip()
# Log in.
api = gmusicapi.Mobileclient()
if not api.login(google_username, google_password, gmusicapi.Mobileclient.FROM_MAC_ADDRESS):
print "Login error"
return
# Get loved tracks.
loved = []
page = 1
while True:
url = "http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user=%s&api_key=%s&page=%d" % \
(lastfm_username, lastfm_key, page)
print("Fetching: " + url)
tree = parse(urllib2.urlopen(url)).getroot()
tracks = tree.findall('lovedtracks/track')
for track in tracks:
title = track.find('name').text
artist = track.find('artist/name').text
loved.append((artist,title))
if len(tracks) < 50:
break
page += 1
print("Got " + str(len(loved)) + " loved tracks")
if len(loved) == 0:
print "Exiting"
return
# Creating new playlist
playlist_id = api.create_playlist("Loved tracks")
to_add = []
# Search for each song in all access.
# This is quite a dirty way to do it, and the gmusicapi seems to be a little out of date
# hence the catch-all. This found 529 out of the 787 loved songs I have which is not too bad.
for target in loved:
try:
res = api.search_all_access(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(loved)))
print("Adding " + str(len(to_add)) + " songs to playlist")
api.add_songs_to_playlist(playlist_id, to_add)
print("Done! I hope.")
if __name__ == '__main__':
main()
@andras-tim
Copy link

I know the problem. I have an idea, this script uses search_all_access() call, but I have not Google Play Music subscriptions.

@dep-deprecated
Copy link

Worked for me. Thanks a million! Any such script for Spotify?

@wieczorek1990
Copy link

Glad it still works.
Had to disable security option in https://www.google.com/settings/security/lesssecureapps

@gshimansky
Copy link

In addition to enabling less secure apps it is necessary to have a full subscription to google music, otherwise this scripts always produces "Got 0 songs so far" like andras-tim had. This is a description of search_all_access method: "Using this method without an All Access subscription will always result in CallFailure being raised.".
Currently it is possible to sign up for 30 days free trial. It did this, and instantly this script worked for me and produced 264 songs out of 337. Thank you very much!

@one-dash
Copy link

Traceback (most recent call last):
  File "./lastfm_to_gmusic.py", line 95, in <module>
    main()
  File "./lastfm_to_gmusic.py", line 44, in main
    if not api.login(google_username, google_password):
TypeError: login() takes exactly 4 arguments (3 given)

:-(

@Timmmm
Copy link
Author

Timmmm commented Sep 20, 2016

@one-dash: Looks like the gmusicapi has changed. I altered the script slightly which might work. Not tested at all.

@kagel
Copy link

kagel commented Jan 31, 2017

Thanks a lot!
@everyone: Please keep in mind that Google playlist has a limit of 1000 items

@vnl
Copy link

vnl commented Jun 7, 2017

Not sure what went wrong, but I get 0 songs again. I have all access (I subscribe). Any way to diagnose it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment