forked from vially/googlemusic-xbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleMusicNavigation.py
248 lines (208 loc) · 10.2 KB
/
GoogleMusicNavigation.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import os
import sys
import urllib
import CommonFunctions as common
import GoogleMusicApi
from gmusicapi.utils.utils import id_or_nid
class GoogleMusicNavigation():
def __init__(self):
self.main = sys.modules["__main__"]
self.xbmc = self.main.xbmc
self.xbmcgui = self.main.xbmcgui
self.xbmcplugin = self.main.xbmcplugin
self.settings = self.main.settings
self.language = self.settings.getLocalizedString
self.api = GoogleMusicApi.GoogleMusicApi()
self.song = self.main.song
self.main_menu = (
{'title':self.language(30201), 'params':{'path':"playlist", 'playlist_id':"all_songs"}},
{'title':self.language(30202), 'params':{'path':"playlists", 'playlist_type':"user"}},
{'title':self.language(30204), 'params':{'path':"playlists", 'playlist_type':"auto"}},
{'title':self.language(30205), 'params':{'path':"filter", 'criteria':"artist"}},
{'title':self.language(30206), 'params':{'path':"filter", 'criteria':"album"}},
{'title':self.language(30207), 'params':{'path':"filter", 'criteria':"genre"}},
{'title':self.language(30208), 'params':{'path':"search"}}
)
def listMenu(self, params={}):
get = params.get
self.path = get("path", "root")
listItems = []
updateListing = False
if self.path == "root":
listItems = self.getMainMenuItems()
elif self.path == "playlist":
listItems = self.listPlaylistSongs(get("playlist_id"))
elif self.path == "station":
listItems = self.getStationTracks(get('id'))
elif self.path == "playlists":
playlist_type = get('playlist_type')
if playlist_type in ('auto', 'user'):
listItems = self.getPlaylists(playlist_type)
elif playlist_type == 'radio':
listItems = self.getStations()
else:
self.main.log("Invalid playlist type: " + playlist_type)
elif self.path == "filter":
criteria = get('criteria')
listItems = self.getCriteria(criteria)
elif self.path == "artist":
listItems = self.getCriteria("album",urllib.unquote(get('name')))
listItems.insert(0,self.addFolderListItem(self.language(30201),{'path':"artist_allsongs", 'name':urllib.unquote(get('name'))}))
elif self.path == "artist_allsongs":
listItems = self.listFilterSongs("artist",get('name'))
elif self.path in ["genre","artist","album"]:
filter_criteria = get('name')
listItems = self.listFilterSongs(self.path,filter_criteria)
elif self.path == "search":
query = common.getUserInput(self.language(30208), '')
if query:
listItems = self.getSearch(query)
else:
self.main.log("No query specified. Showing main menu")
listItems = self.getMainMenuItems()
updateListing = True
else:
self.main.log("Invalid path: " + get("path"))
self.xbmcplugin.addDirectoryItems(int(sys.argv[1]), listItems)
self.xbmcplugin.endOfDirectory(int(sys.argv[1]), succeeded=True, updateListing=updateListing)
def getMainMenuItems(self):
''' Build the plugin root menu. '''
menuItems = []
for menu_item in self.main_menu:
params = menu_item['params']
cm = []
if 'playlist_id' in params:
cm = self.getPlayAllContextMenuItems(params['playlist_id'])
elif 'playlist_type' in params:
cm = self.getPlaylistsContextMenuItems(params['playlist_type'])
menuItems.append(self.addFolderListItem(menu_item['title'], params, cm))
if self.getStations():
menuItems.append(self.addFolderListItem('Stations', {'path':"playlists", 'playlist_type':"radio"}))
return menuItems
def executeAction(self, params={}):
get = params.get
if (get("action") == "play_all"):
self.playAll(params)
elif (get("action") == "play_song"):
self.song.play(get("song_id"))
elif (get("action") == "update_playlist"):
self.api.getPlaylistSongs(params["playlist_id"], True)
elif (get("action") == "update_playlists"):
self.api.getPlaylistsByType(params["playlist_type"], True)
elif (get("action") == "clear_cache"):
self.api.clearCache()
elif (get("action") == "clear_cookie"):
self.api.clearCookie()
else:
self.main.log("Invalid action: " + get("action"))
def addFolderListItem(self, name, params={}, contextMenu=[], album_art_url=""):
if not name:
name = 'Unknown'
li = self.xbmcgui.ListItem(label=name, iconImage=album_art_url, thumbnailImage=album_art_url)
li.setProperty("Folder", "true")
url = sys.argv[0] + '?' + urllib.urlencode(params)
if len(contextMenu) > 0:
li.addContextMenuItems(contextMenu, replaceItems=True)
return url, li, "true"
def addSongItem(self, song):
song_id = song[0].encode('utf-8')
if self.path == 'artist_allsongs' and song[7]:
# add album name when showing all artist songs
li = self.song.createItem(song, ('['+song[7]+'] '+song[8]).encode('utf-8'))
else:
li = self.song.createItem(song)
url = '%s?action=play_song&song_id=%s' % (sys.argv[0], song_id)
return url,li
def listPlaylistSongs(self, playlist_id):
self.main.log("Loading playlist: " + playlist_id)
songs = self.api.getPlaylistSongs(playlist_id)
return self.addSongsFromLibrary(songs)
def addSongsFromLibrary(self, library):
listItems = []
for song in library:
listItems.append(self.addSongItem(song))
return listItems
def getPlaylists(self, playlist_type):
self.main.log("Getting playlists of type: " + playlist_type)
playlists = self.api.getPlaylistsByType(playlist_type)
return self.addPlaylistsItems(playlists)
def listFilterSongs(self, filter_type, filter_criteria):
if filter_criteria:
filter_criteria = urllib.unquote_plus(filter_criteria)
songs = self.api.getFilterSongs(filter_type, filter_criteria)
return self.addSongsFromLibrary(songs)
def getCriteria(self, criteria, artist=''):
listItems = []
genres = self.api.getCriteria(criteria,artist)
for genre in genres:
if genre[1]:
art = genre[1]
else:
art = self.main.__icon__
cm = self.getFilterContextMenuItems(criteria,genre[0])
listItems.append(self.addFolderListItem(genre[0], {'path':criteria, 'name':genre[0].encode('utf8')}, cm, art))
return listItems
def addPlaylistsItems(self, playlists):
listItems = []
for playlist_id, playlist_name in playlists:
cm = self.getPlayAllContextMenuItems(playlist_id)
listItems.append(self.addFolderListItem(playlist_name, {'path':"playlist", 'playlist_id':playlist_id}, cm))
return listItems
def playAll(self, params={}):
get = params.get
playlist_id = get('playlist_id')
if playlist_id:
self.main.log("Loading playlist: " + playlist_id)
songs = self.api.getPlaylistSongs(playlist_id)
else:
songs = self.api.getFilterSongs(get('filter_type'), get('filter_criteria'))
player = self.xbmc.Player()
if (player.isPlaying()):
player.stop()
playlist = self.xbmc.PlayList(self.xbmc.PLAYLIST_MUSIC)
playlist.clear()
song_url = "%s?action=play_song&song_id=%s"
for song in songs:
song_id = song[0].encode('utf-8')
li = self.song.createItem(song)
playlist.add(song_url % (sys.argv[0], song_id), li)
if (get("shuffle")):
playlist.shuffle()
self.xbmc.executebuiltin('playlist.playoffset(music , 0)')
def getPlayAllContextMenuItems(self, playlist):
cm = []
cm.append((self.language(30301), "XBMC.RunPlugin(%s?action=play_all&playlist_id=%s)" % (sys.argv[0], playlist)))
cm.append((self.language(30302), "XBMC.RunPlugin(%s?action=play_all&playlist_id=%s&shuffle=true)" % (sys.argv[0], playlist)))
cm.append((self.language(30303), "XBMC.RunPlugin(%s?action=update_playlist&playlist_id=%s)" % (sys.argv[0], playlist)))
return cm
def getFilterContextMenuItems(self, filter_type, filter_criteria):
cm = []
cm.append((self.language(30301), "XBMC.RunPlugin(%s?action=play_all&filter_type=%s&filter_criteria=%s)" % (sys.argv[0], filter_type, filter_criteria)))
cm.append((self.language(30302), "XBMC.RunPlugin(%s?action=play_all&filter_type=%s&filter_criteria=%s&shuffle=true)" % (sys.argv[0], filter_type, filter_criteria)))
return cm
def getPlaylistsContextMenuItems(self, playlist_type):
cm = []
cm.append((self.language(30304), "XBMC.RunPlugin(%s?action=update_playlists&playlist_type=%s)" % (sys.argv[0], playlist_type)))
return cm
def getSearch(self, query):
results = self.api.getSearch(query)
return self.addSongsFromLibrary(results)
def getStations(self):
listItems = []
stations = self.api.getStations()
#stations = [{'name':'Teste','id':'1287yed82d'}]
for rs in stations:
listItems.append(self.addFolderListItem(rs['name'], {'path':"station",'id':rs['id']}))
return listItems
def getStationTracks(self,station_id):
listItems = []
tracks = self.api.getStationTracks(station_id)
#tracks = [{'title':'Teste1','id':'f5d1db5a-77d6-3b89-993b-b6ee45b3abce'},{'title':'Teste2','id':'kf7fi765'}]
for track in tracks:
li = self.xbmcgui.ListItem(track['title'])
li.setProperty('IsPlayable', 'true')
li.setProperty('Music', 'true')
url = '%s?action=play_song&song_id=%s&title=%s' % (sys.argv[0],id_or_nid(track).encode('utf8'),track['title'].encode('utf8'))
li.setPath(url)
listItems.append([url,li])
return listItems