forked from vially/googlemusic-xbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleMusicApi.py
189 lines (159 loc) · 7.45 KB
/
GoogleMusicApi.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import utils
from GoogleMusicStorage import storage
class GoogleMusicApi():
def __init__(self):
self.api = None
self.device = None
self.login = None
def getApi(self,nocache=False):
if self.api == None :
import GoogleMusicLogin
self.login = GoogleMusicLogin.GoogleMusicLogin()
self.login.login(nocache)
self.api = self.login.getApi()
self.device = self.login.getDevice()
return self.api
def getDevice(self):
if self.device == None:
self.getApi()
return self.device
def getLogin(self):
if self.login == None:
self.getApi()
return self.login
def getPlaylistSongs(self, playlist_id, forceRenew=False):
if playlist_id in ('thumbsup','lastadded','mostplayed','freepurchased','feellucky'):
songs = storage.getAutoPlaylistSongs(playlist_id)
if playlist_id == 'thumbsup':
""" Try to fetch all access thumbs up songs """
for track in self.getApi().get_promoted_songs():
songs.append(self._convertAATrack(track))
else:
if forceRenew:
self.updatePlaylistSongs()
songs = storage.getPlaylistSongs(playlist_id)
return songs
def getPlaylistsByType(self, playlist_type, forceRenew=False):
if forceRenew:
self.updatePlaylistSongs()
return storage.getPlaylists()
def getSong(self, song_id):
return storage.getSong(song_id)
def loadLibrary(self):
#gen = self.gmusicapi.get_all_songs(incremental=True)
#for chunk in gen:
# for song in chunk:
#print song
# api_songs.append(song)
# break
#api_songs = [song for chunk in api_songs for song in chunk]
api_songs = self.getApi().get_all_songs()
utils.log("Library Size: "+repr(len(api_songs)))
#utils.log("First Song: "+repr(api_songs[0]))
storage.storeApiSongs(api_songs, 'all_songs')
self.updatePlaylistSongs()
def updatePlaylistSongs(self):
storage.storePlaylistSongs(self.getApi().get_all_user_playlist_contents())
def getSongStreamUrl(self, song_id):
stream_url = self.getLogin().getStreamUrl(song_id)
return stream_url
def incrementSongPlayCount(self, song_id):
try:
self.getApi().increment_song_playcount(song_id)
except Exception as ex:
utils.log("ERROR trying to increment playcount: "+repr(ex))
storage.incrementSongPlayCount(song_id)
def getFilterSongs(self, filter_type, filter_criteria, albums):
return storage.getFilterSongs(filter_type, filter_criteria, albums)
def getCriteria(self, criteria, artist=''):
return storage.getCriteria(criteria,artist)
def getSearch(self, query):
utils.log("API getsearch: "+query)
result = storage.getSearch(query)
tracks = result['tracks']
albums = result['albums']
artists = result['artists']
try:
aaresult = self.getApi().search_all_access(query)
utils.log("API getsearch aa: "+repr(aaresult))
for song in aaresult['song_hits']:
track = song['track']
#utils.log("RESULT SONGS: "+repr(track['artist'])+" - "+repr(track['title'])+" "+track['nid'])
tracks.append(self._convertAATrack(track))
for album in aaresult['album_hits']:
#utils.log("RESULT ALBUMS: "+repr(album['album']['name'])+" - "+repr(album['album']['artist'])+" "+album['album']['albumId'])
albumDict = album['album']
albums.append([albumDict['name'],albumDict['artist'],albumDict.get('albumArtRef',''),albumDict['albumId']])
for artist in aaresult['artist_hits']:
artistDict = artist['artist']
artists.append([artistDict['name'],artistDict.get('artistArtRef',''),artistDict['artistId']])
utils.log("API search results: tracks "+repr(len(tracks))+" albums "+repr(len(albums))+" artists "+repr(len(artists)))
except Exception as e:
utils.log("*** NO ALL ACCESS RESULT IN SEARCH *** "+repr(e))
#tracksAA = storage.getAutoPlaylistSongs('thumbsup')
return result
def getAlbum(self, albumid):
result = []
try:
for track in self.getApi().get_album_info(albumid, include_tracks=True)['tracks']:
result.append(self._convertAATrack(track))
except Exception as e:
utils.log("*** NO ALL ACCESS ALBUM *** "+albumid+' '+repr(e))
return result
def getArtist(self, artistid):
result = []
try:
for track in self.getApi().get_artist_info(artistid, include_albums=False, max_top_tracks=50, max_rel_artist=0)['topTracks']:
result.append(self._convertAATrack(track))
except Exception as e:
utils.log("*** NO ALL ACCESS ARTIST *** "+artistid+' '+repr(e))
return result
def getTrack(self, trackid):
return self._convertAATrack(self.getApi().get_track_info(trackid))
def getSharedPlaylist(self, sharetoken):
result = []
try:
for track in self.getApi().get_shared_playlist_contents(sharetoken):
result.append(self._convertAATrack(track['track']))
except Exception as e:
utils.log("*** NO ALL ACCESS SHARED PLAYLIST *** "+sharetoken+' '+repr(e))
return result
def clearCache(self):
storage.clearCache()
self.clearCookie()
def clearCookie(self):
self.getLogin().clearCookie()
def getStations(self):
stations = {}
try:
stations = self.getApi().get_all_stations()
#utils.log("STATIONS: "+repr(stations))
except Exception as e:
utils.log("*** NO STATIONS *** "+repr(e))
return stations
def getStationTracks(self, station_id):
songs = []
try:
for track in self.getApi().get_station_tracks(station_id, num_tracks=100):
songs.append(self._convertAATrack(track))
except Exception as e:
utils.log("*** NO TRACKS *** "+repr(e))
return songs
def startRadio(self, name, song_id):
return self.getApi().create_station(name, track_id=song_id)
def addAAtrack(self, song_id):
self.getApi().add_aa_track(song_id)
def addToPlaylist(self, playlist_id, song_id):
entry_id = self.getApi().add_songs_to_playlist(playlist_id, song_id)
storage.addToPlaylist(playlist_id, song_id, entry_id[0])
def delFromPlaylist(self, playlist_id, song_id):
entry_id = storage.delFromPlaylist(playlist_id, song_id)
self.getApi().remove_entries_from_playlist(entry_id)
def _convertAATrack(self, aaTrack):
return [aaTrack.get('id') or aaTrack['storeId'],'',0,0,0,'',0,aaTrack.get('album'),
aaTrack['artist']+" - "+aaTrack['title'],aaTrack['albumArtist'],0,
aaTrack['trackNumber'],0,0,'',aaTrack.get('playCount', 0),0,aaTrack['title'],
aaTrack['artist'],'',0,int(aaTrack['durationMillis'])/1000,
aaTrack['albumArtRef'][0]['url'] if aaTrack.get('albumArtRef') else utils.addon.getAddonInfo('icon'),
aaTrack['artist']+" - "+aaTrack['title'],'',
aaTrack['artistArtRef'][0]['url'] if aaTrack.get('artistArtRef') else utils.addon.getAddonInfo('fanart')]