forked from vially/googlemusic-xbmc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoogleMusicNavigation.py
195 lines (155 loc) · 7.52 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
import os
import sys
import urllib
import GoogleMusicApi
class GoogleMusicNavigation():
def __init__(self):
self.xbmc = sys.modules["__main__"].xbmc
self.xbmcgui = sys.modules["__main__"].xbmcgui
self.xbmcplugin = sys.modules["__main__"].xbmcplugin
self.xbmcvfs = sys.modules["__main__"].xbmcvfs
self.settings = sys.modules["__main__"].settings
self.language = sys.modules["__main__"].language
self.dbg = sys.modules["__main__"].dbg
self.common = sys.modules["__main__"].common
self.api = GoogleMusicApi.GoogleMusicApi()
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(30203), 'params':{'path':"playlists", 'playlist_type':"instant"}},
#{'title':self.language(30204), 'params':{'path':"playlists", 'playlist_type':"auto"}}
)
def listMenu(self, params={}):
get = params.get
path = get("path", "root")
if path == "root":
''' Show the plugin root menu. '''
for menu_item in self.main_menu:
params = menu_item['params']
cm = []
if 'playlist_id' in params:
cm = self.getPlayAllContextMenuItems(params['playlist_id'])
else:
cm = self.getPlaylistsContextMenuItems(params['playlist_type'])
self.addFolderListItem(menu_item['title'], params, cm)
elif path == "playlist":
self.listPlaylistSongs(get("playlist_id"))
elif path == "playlists":
playlist_type = get('playlist_type')
if playlist_type in ('auto', 'instant', 'user'):
self.getPlaylists(playlist_type)
else:
self.common.log("Invalid playlist type: " + playlist_type)
else:
self.common.log("Invalid path: " + get("path"))
self.xbmcplugin.endOfDirectory(handle=int(sys.argv[1]), succeeded=True)
def executeAction(self, params={}):
get = params.get
if (get("action") == "play_all"):
self.playAll(params)
elif (get("action") == "play_song"):
self.playSong(params)
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.clearCache()
elif (get("action") == "clear_cookie"):
self.clearCookie()
else:
self.common.log("Invalid action: " + get("action"))
def addFolderListItem(self, name, params={}, contextMenu=[]):
li = self.xbmcgui.ListItem(name)
li.setProperty("Folder", "true")
url = sys.argv[0] + '?' + urllib.urlencode(params)
if len(contextMenu) > 0:
li.addContextMenuItems(contextMenu, replaceItems=True)
return self.xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url, listitem=li, isFolder=True)
def createSongListItem(self, song):
li = self.xbmcgui.ListItem(song[23])
li.setProperty('IsPlayable', 'true')
li.setProperty('Music', 'true')
li.setInfo(type='music', infoLabels=self.getInfoLabels(song))
return li
def addSongItem(self, song, playlist_id):
song_id = song[0].encode('utf-8')
li = self.createSongListItem(song)
#li.addContextMenuItems(self.getPlayAllContextMenuItems(playlist_id))
url = '%s?action=play_song&song_id=%s' % (sys.argv[0], song_id)
return self.xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url, listitem=li)
def listPlaylistSongs(self, playlist_id):
self.common.log("Loading playlist: " + playlist_id)
songs = self.api.getPlaylistSongs(playlist_id)
self.addSongsFromLibrary(songs, playlist_id)
def addSongsFromLibrary(self, library, playlist_id):
for song in library:
self.addSongItem(song, playlist_id)
def getPlaylists(self, playlist_type):
self.common.log("Getting playlists of type: " + playlist_type)
playlists = self.api.getPlaylistsByType(playlist_type)
self.common.log(str(playlists))
self.addPlaylistsItems(playlists)
def addPlaylistsItems(self, playlists):
for playlist_id, playlist_name, playlist_type, fetched in playlists:
cm = self.getPlayAllContextMenuItems(playlist_id)
self.addFolderListItem(playlist_name, {'path':"playlist", 'playlist_id':playlist_id}, cm)
def playAll(self, params={}):
get = params.get
playlist_id = get('playlist_id')
self.common.log("Loading playlist: " + playlist_id)
songs = self.api.getPlaylistSongs(playlist_id)
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&playlist_id=" + playlist_id
for song in songs:
song_id = song[0].encode('utf-8')
li = self.createSongListItem(song)
playlist.add(song_url % (sys.argv[0], song_id), li)
if (get("shuffle")):
playlist.shuffle()
self.xbmc.executebuiltin('playlist.playoffset(music , 0)')
def playSong(self, params={}):
get = params.get
song = self.api.getSong(get("song_id"))
url = self.api.getSongStreamUrl(get("song_id"))
li = self.createSongListItem(song)
li.setPath(url)
self.xbmcplugin.setResolvedUrl(handle=int(sys.argv[1]), succeeded=True, listitem=li)
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 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 getInfoLabels(self, song):
infoLabels = {
'tracknumber': song[11],
'duration': song[21] / 1000,
'year': song[6],
'genre': song[14].encode('utf-8'),
'album': song[7].encode('utf-8'),
'artist': song[18].encode('utf-8'),
'title': song[8].encode('utf-8'),
'playcount': song[15]
}
return infoLabels
def clearCache(self):
sqlite_db = os.path.join(self.xbmc.translatePath("special://database"), self.settings.getSetting('sqlite_db'))
if self.xbmcvfs.exists(sqlite_db):
self.xbmcvfs.delete(sqlite_db)
self.settings.setSetting("fetched_all_songs", "")
self.settings.setSetting('firstrun', "")
self.clearCookie()
def clearCookie(self):
cookie_file = os.path.join(self.settings.getAddonInfo('path'), self.settings.getSetting('cookie_file'))
if self.xbmcvfs.exists(cookie_file):
self.xbmcvfs.delete(cookie_file)
self.settings.setSetting('logged_in', "")