-
-
Save gshimansky/1df35d5d2cf05dc9d8f5 to your computer and use it in GitHub Desktop.
Get loved tracks from last.fm, convert into Google Music playlist, print not found tracks in the end
This file contains 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
#!/usr/bin/env python | |
# Today is the 9th of March 2015. | |
# | |
# Not really tested! | |
# | |
# 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`: `g`. | |
# Today it is necessary to download development version of | |
# gmusicapi from GIT: https://github.com/simon-weber/Unofficial-Google-Music-API | |
# and install it using included setup script because something changed | |
# in Google search response since last stable verison was released. | |
# 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 collections | |
from collections import defaultdict | |
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): | |
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 = [] | |
not_found = defaultdict(list) | |
# 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"]) | |
print target[0] + " - " + target[1] + ": " + res["song_hits"][0]["track"]["nid"] | |
except: | |
print "NOT FOUND: " + target[0] + " - " + target[1] | |
not_found[target[0]].append(target[1]) | |
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 "NOT FOUND:" | |
for artist, songs in collections.OrderedDict(sorted(not_found.items())).iteritems(): | |
for s in songs: | |
print artist + ", " + s | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment