forked from vially/googlemusic-xbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleMusicApi.py
140 lines (114 loc) · 5.13 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
import sys
class GoogleMusicApi():
def __init__(self):
self.main = sys.modules["__main__"]
self.storage = self.main.storage
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'):
return self.storage.getAutoPlaylistSongs(playlist_id)
if not self.storage.isPlaylistFetched(playlist_id) or forceRenew:
self.updatePlaylistSongs(playlist_id)
songs = self.storage.getPlaylistSongs(playlist_id)
return songs
def getPlaylistsByType(self, playlist_type, forceRenew=False):
if playlist_type == 'auto':
return [['thumbsup','Highly Rated'],['lastadded','Last Added'],
['freepurchased','Free and Purchased'],['mostplayed','Most Played']]
if forceRenew:
self.updatePlaylists(playlist_type)
playlists = self.storage.getPlaylists()
if len(playlists) == 0 and not forceRenew:
self.updatePlaylists(playlist_type)
playlists = self.storage.getPlaylists()
return playlists
def getSong(self, song_id):
return self.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()
self.main.log("Library Size: "+repr(len(api_songs)))
#self.main.log("First Song: "+repr(api_songs[0]))
self.storage.storeApiSongs(api_songs, 'all_songs')
def updatePlaylistSongs(self, playlist_id):
self.storage.storePlaylistSongs(self.getApi().get_all_user_playlist_contents())
def updatePlaylists(self, playlist_type):
self.storage.storePlaylistSongs(self.getApi().get_all_user_playlist_contents())
def getSongStreamUrl(self, song_id):
# using cached cookies fails with all access tracks
self.getApi()
stream_url = self.login.getStreamUrl(song_id)
self.storage.updateSongStreamUrl(song_id, stream_url)
self.main.log("getSongStreamUrl: "+stream_url)
return stream_url
def incrementSongPlayCount(self, song_id):
try:
self.getApi().increment_song_playcount(song_id)
except Exception as ex:
self.main.log("ERROR trying to increment playcount: "+repr(ex))
pass
self.storage.incrementSongPlayCount(song_id)
def getFilterSongs(self, filter_type, filter_criteria, artist):
return self.storage.getFilterSongs(filter_type, filter_criteria, artist)
def getCriteria(self, criteria, artist=''):
return self.storage.getCriteria(criteria,artist)
def getSearch(self, query):
result = self.storage.getSearch(query)
try:
aaresult = self.getApi().search_all_access(query)
for song in aaresult['song_hits']:
track = song['track']
self.main.log("RESULT: "+track['artist']+" - "+track['title'])
result.append([track['nid'],'',0,0,track['discNumber'],'',0,track['album'],
track['title'],track['albumArtist'],track['trackType'],
track['trackNumber'],0,0,'',track.get('playCount', 0),0,track['title'],
track['artist'],'',0,int(track['durationMillis'])/1000,
track['albumArtRef'][0]['url'],track['artist']+" - "+track['title']+" **",''])
except Exception as e:
self.main.log("*** NO ALL ACCESS RESULT IN SEARCH *** "+repr(e))
return result
def clearCache(self):
self.storage.clearCache()
self.clearCookie()
def clearCookie(self):
self.getLogin().clearCookie()
def getStations(self):
stations = {}
try:
stations = self.getApi().get_all_stations()
#self.main.log("STATIONS: "+repr(stations))
except Exception as e:
self.main.log("*** NO STATIONS *** "+repr(e))
return stations
def getStationTracks(self, station_id):
tracks = {}
try:
tracks = self.getApi().get_station_tracks(station_id)
#self.main.log("TRACKS *** "+repr(tracks))
except Exception as e:
self.main.log("*** NO TRACKS *** "+repr(e))
return tracks