-
Notifications
You must be signed in to change notification settings - Fork 1
/
lastfm-loved2grooveshark.py
113 lines (82 loc) · 3.61 KB
/
lastfm-loved2grooveshark.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
import urllib, urllib2
from xml.etree import ElementTree
import json
import time
import logging
log = logging.getLogger('lastfm2grooveshark')
log.setLevel(logging.DEBUG)
log.addHandler(logging.StreamHandler())
# constants
lastfm_username = 'LASTFM_USERNAME'
lastfm_key = 'LASTFM_KEY' # you can get one here http://www.last.fm/api/account
grooveshark_key = 'GROOVESHARK_KEY' # you can get key here http://apishark.com/#gettingStarted
grooveshark_gsauth = 'GSAuth' # you can get your GSAuth here https://ssl.apishark.com/generateGSAuth
playlist_name = 'lastfm-loved'
# urls
lastfm_url = "http://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user=%(username)s&api_key=%(lastfm_key)s&limit=100000" % \
dict(username=lastfm_username, lastfm_key=lastfm_key)
grooveshark_url = 'http://1.apishark.com/p:%s/' % grooveshark_key
grooveshark_search_url = grooveshark_url + 'searchSongs/'
grooveshark_create_playlist = 'http://1.apishark.com/createPlaylist/'
lastfm_loved_xml = urllib.urlopen(lastfm_url).read()
parsed_lastfm = ElementTree.fromstring(lastfm_loved_xml)
songs = []
skipped = []
counter = 0
def add_song(count, result):
id = result['SongID']
found_title = result['SongName']
found_artist = result['ArtistName']
found_album = result['AlbumName']
songs.append(id)
log.debug('%d: found: ID=%s "%s" by "%s", album "%s"' % (count, id, found_title, found_artist, found_album))
def process_search_response(response):
found = False
results = response['Result']
if type(results) == list and len(results) > 0:
found = True
add_song(counter, results[0])
calls_left = response['RateLimit']['CallsRemaining']
reset_time = response['RateLimit']['ResetTime']
if calls_left <= 0:
secs = abs(reset_time - int(time.time()))
log.info("Going to sleep for %d" % secs)
time.sleep(secs)
log.info("Morning!")
return found
for track in parsed_lastfm.findall('*/track'):
title = track.find('name').text
artist = track.find('artist/name').text
counter += 1
log.debug('%d: looking for "%s" by "%s"' % (counter, title, artist))
# 1st search attempt
search_data = urllib.urlencode(dict(query=title.encode('utf-8'), artist=artist.encode('utf-8')))
response = json.load(urllib.urlopen(grooveshark_search_url, data=search_data))
if not process_search_response(response):
# 2nd search attempt
search_data = urllib.urlencode(dict(query=('%s %s' % (title, artist)).encode('utf-8')))
response = json.load(urllib.urlopen(grooveshark_search_url, data=search_data))
if not process_search_response(response):
skipped.append((title, artist))
#f = open('/tmp/songs-ok', 'w')
#f.writelines(json.dumps(songs))
#f.close()
#
#f = open('/tmp/songs-skipped', 'w')
#f.writelines(json.dumps(skipped))
#f.close()
log.info("Processed: %s" % counter)
log.info("OK: %s" % len(songs))
log.info("Skipped: %s" % len(skipped))
playlist_data = dict(gsAuth=grooveshark_gsauth, name=playlist_name, songIDs=json.dumps(songs))
playlist = json.load(urllib.urlopen(grooveshark_create_playlist, data=urllib.urlencode(playlist_data)))
if playlist['Success']:
googl_request = urllib2.Request("https://www.googleapis.com/urlshortener/v1/url",
headers = { "Content-Type": "application/json" },
data = "{ 'longUrl' : '%s' }" % playlist['Result']['Url'])
shorten = json.load(urllib2.urlopen(googl_request))
# log.info("Playlist created: %s" % playlist['Result']['Url'])
log.info("Playlist created: %s" % shorten['id'])
else:
log.error("Playlist not created: %s" % playlist['Result']['string'])