-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefault.py
339 lines (289 loc) · 13.4 KB
/
default.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# -*- coding: utf-8 -*-
#
# Copyright (C) 2019 L2501
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import os
import sys
from routing import Plugin
from kodi_six import xbmc, xbmcgui, xbmcaddon, xbmcplugin
from resources.lib.musicmp3 import musicMp3, gnr_ids
from future.backports.urllib.parse import quote_from_bytes as orig_quote
from future.backports.urllib.parse import unquote_to_bytes as orig_unquote
try:
from xbmcvfs import translatePath
except ImportError:
from kodi_six.xbmc import translatePath
def quote(s, safe=""):
return orig_quote(s.encode("utf-8"), safe.encode("utf-8"))
def unquote(s):
return orig_unquote(s).decode("utf-8")
addon = xbmcaddon.Addon()
plugin = Plugin()
plugin.name = addon.getAddonInfo("name")
USER_DATA_DIR = translatePath(addon.getAddonInfo("profile"))
MEDIA_DIR = os.path.join(translatePath(xbmcaddon.Addon().getAddonInfo("path")), "resources", "media")
FANART = os.path.join(MEDIA_DIR, "fanart.jpg")
MUSICMP3_DIR = os.path.join(USER_DATA_DIR, "musicmp3")
if not os.path.exists(MUSICMP3_DIR):
os.makedirs(MUSICMP3_DIR)
fixed_view_mode = addon.getSetting("fixed_view_mode") == "true"
albums_view_mode = addon.getSetting("albums_view_mode")
songs_view_mode = addon.getSetting("songs_view_mode")
@plugin.route("/")
def index():
_all_artists = xbmcgui.ListItem("Artists")
_all_artists.setArt({"fanart": FANART, "icon": os.path.join(MEDIA_DIR, "artists.jpg")})
xbmcplugin.addDirectoryItem(plugin.handle, plugin.url_for(musicmp3_artist_main), _all_artists, True)
_top_albums = xbmcgui.ListItem("Top Albums")
_top_albums.setArt({"fanart": FANART, "icon": os.path.join(MEDIA_DIR, "topalbums.jpg")})
xbmcplugin.addDirectoryItem(plugin.handle, plugin.url_for(musicmp3_albums_main, "top"), _top_albums, True)
_new_albums = xbmcgui.ListItem("New Albums")
_new_albums.setArt({"fanart": FANART, "icon": os.path.join(MEDIA_DIR, "newalbums.jpg")})
xbmcplugin.addDirectoryItem(plugin.handle, plugin.url_for(musicmp3_albums_main, "new"), _new_albums, True)
_search_artists = xbmcgui.ListItem("Search Artists")
_search_artists.setArt({"fanart": FANART, "icon": os.path.join(MEDIA_DIR, "searchartists.jpg")})
xbmcplugin.addDirectoryItem(plugin.handle, plugin.url_for(musicmp3_search, "artists"), _search_artists, True)
_search_albums = xbmcgui.ListItem("Search Albums")
_search_albums.setArt({"fanart": FANART, "icon": os.path.join(MEDIA_DIR, "searchalbums.jpg")})
xbmcplugin.addDirectoryItem(plugin.handle, plugin.url_for(musicmp3_search, "albums"), _search_albums, True)
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route("/musicmp3/albums_main/<sort>")
def musicmp3_albums_main(sort):
_directory_items = []
for i, gnr in enumerate(gnr_ids):
li = xbmcgui.ListItem("{0} {1} Albums".format(sort.title(), gnr[0]))
li.setArt(
{
"fanart": FANART,
"icon": os.path.join(
MEDIA_DIR, "genre", "{0}.jpg".format(gnr[0].lower().replace(" ", "").replace("&", "_"))
),
}
)
_directory_items.append((plugin.url_for(musicmp3_albums_gnr, sort, i), li, True))
xbmcplugin.addDirectoryItems(plugin.handle, _directory_items, len(_directory_items))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route("/musicmp3/albums_gnr/<sort>/<gnr>")
def musicmp3_albums_gnr(sort, gnr):
_directory_items = []
for sub_gnr in gnr_ids[int(gnr)][1]:
if sub_gnr[0] == "Compilations":
section = "compilations"
elif sub_gnr[0] == "Soundtracks":
section = "soundtracks"
else:
section = "main"
li = xbmcgui.ListItem("{0} {1} Albums".format(sort.title(), sub_gnr[0]))
li.setArt(
{
"fanart": FANART,
"icon": os.path.join(
MEDIA_DIR,
"genre",
gnr_ids[int(gnr)][0].lower(),
"{0}.jpg".format(sub_gnr[0].lower().replace(" ", "").replace("&", "_")),
),
}
)
_directory_items.append((plugin.url_for(musicmp3_main_albums, section, sub_gnr[1], sort, "0"), li, True))
xbmcplugin.addDirectoryItems(plugin.handle, _directory_items, len(_directory_items))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route("/musicmp3/artists_main")
def musicmp3_artist_main():
_directory_items = []
for i, gnr in enumerate(gnr_ids):
li = xbmcgui.ListItem("{0} Artists".format(gnr[0]))
li.setArt(
{
"fanart": FANART,
"icon": os.path.join(
MEDIA_DIR, "genre", "{0}.jpg".format(gnr[0].lower().replace(" ", "").replace("&", "_"))
),
}
)
_directory_items.append((plugin.url_for(musicmp3_artists_gnr, i), li, True))
xbmcplugin.addDirectoryItems(plugin.handle, _directory_items, len(_directory_items))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route("/musicmp3/artists_gnr/<gnr>")
def musicmp3_artists_gnr(gnr):
_directory_items = []
for sub_gnr in gnr_ids[int(gnr)][1]:
if sub_gnr[0] == "Compilations":
continue
elif sub_gnr[0] == "Soundtracks":
continue
li = xbmcgui.ListItem("{0} Artists".format(sub_gnr[0]))
li.setArt(
{
"fanart": FANART,
"icon": os.path.join(
MEDIA_DIR,
"genre",
gnr_ids[int(gnr)][0].lower(),
"{0}.jpg".format(sub_gnr[0].lower().replace(" ", "").replace("&", "_")),
),
}
)
_directory_items.append((plugin.url_for(musicmp3_main_artists, sub_gnr[1], "0"), li, True))
xbmcplugin.addDirectoryItems(plugin.handle, _directory_items, len(_directory_items))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route("/musicmp3/search/<cat>")
def musicmp3_search(cat):
musicmp3_api = musicMp3(MUSICMP3_DIR)
keyboard = xbmc.Keyboard("", "Search")
keyboard.doModal()
if keyboard.isConfirmed():
keyboardinput = keyboard.getText()
if keyboardinput:
_directory_items = []
if cat == "artists":
artists = musicmp3_api.search(keyboardinput, cat)
for a in artists:
li = xbmcgui.ListItem(a.get("artist"))
li.setInfo("music", {"title": a.get("artist"), "artist": a.get("artist")})
_directory_items.append((plugin.url_for(artists_albums, quote(a.get("link"))), li, True))
elif cat == "albums":
albums = musicmp3_api.search(keyboardinput, cat)
for a in albums:
li = xbmcgui.ListItem("{0}[CR][COLOR=darkmagenta]{1}[/COLOR]".format(a.get("title"), a.get("artist")))
li.setArt({"thumb": a.get("image"), "icon": a.get("image")})
li.setInfo(
"music",
{
"title": a.get("title"),
"artist": a.get("artist"),
"album": a.get("title"),
"year": a.get("date"),
},
)
li.setProperty("Album_Description", a.get("details"))
_directory_items.append((plugin.url_for(musicmp3_album, quote(a.get("link"))), li, True))
xbmcplugin.addDirectoryItems(plugin.handle, _directory_items, len(_directory_items))
xbmcplugin.setContent(plugin.handle, "albums")
if fixed_view_mode:
xbmc.executebuiltin("Container.SetViewMode({0})".format(albums_view_mode))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route("/musicmp3/main_albums/<section>/<gnr_id>/<sort>/<index>/dir")
def musicmp3_main_albums(section, gnr_id, sort, index):
musicmp3_api = musicMp3(MUSICMP3_DIR)
dir_items = 40
index = int(index)
if section == "main":
_section = ""
else:
_section = section
albums = musicmp3_api.main_albums(_section, gnr_id, sort, index, dir_items)
_directory_items = []
for a in albums:
li = xbmcgui.ListItem("{0}[CR][COLOR=darkmagenta]{1}[/COLOR]".format(a.get("title"), a.get("artist")))
li.setArt({"thumb": a.get("image"), "icon": a.get("image")})
li.setInfo(
"music",
{"title": a.get("title"), "artist": a.get("artist"), "album": a.get("title"), "year": a.get("date")},
)
_directory_items.append((plugin.url_for(musicmp3_album, quote(a.get("link"))), li, True))
""" Next Page """
if len(albums) >= dir_items:
next_index = str(index + dir_items)
li = xbmcgui.ListItem("More {0}+".format(next_index))
l = plugin.url_for(musicmp3_main_albums, section, gnr_id, sort, next_index)
_directory_items.append((l, li, True))
xbmcplugin.addDirectoryItems(plugin.handle, _directory_items, len(_directory_items))
xbmcplugin.setContent(plugin.handle, "albums")
if fixed_view_mode:
xbmc.executebuiltin("Container.SetViewMode({0})".format(albums_view_mode))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route("/musicmp3/main_artists/<gnr_id>/<index>/dir")
def musicmp3_main_artists(gnr_id, index):
musicmp3_api = musicMp3(MUSICMP3_DIR)
dir_items = 40
index = int(index)
artists = musicmp3_api.main_artists(gnr_id, index, dir_items)
_directory_items = []
for a in artists:
li = xbmcgui.ListItem(a.get("artist"))
li.setInfo("music", {"title": a.get("artist"), "artist": a.get("artist")})
li.setProperty("Album_Artist", a.get("artist"))
_directory_items.append((plugin.url_for(artists_albums, quote(a.get("link"))), li, True))
""" Next Page """
if len(artists) >= dir_items:
next_index = str(index + dir_items)
li = xbmcgui.ListItem("More {0}+".format(next_index))
l = plugin.url_for(musicmp3_main_artists, gnr_id, next_index)
_directory_items.append((l, li, True))
xbmcplugin.addDirectoryItems(plugin.handle, _directory_items, len(_directory_items))
xbmcplugin.setContent(plugin.handle, "albums")
if fixed_view_mode:
xbmc.executebuiltin("Container.SetViewMode({0})".format(albums_view_mode))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route("/musicmp3/artists_albums/<link>")
def artists_albums(link):
musicmp3_api = musicMp3(MUSICMP3_DIR)
url = unquote(link)
albums = musicmp3_api.artist_albums(url)
_directory_items = []
for a in albums:
li = xbmcgui.ListItem("{0}[CR][COLOR=darkmagenta]{1}[/COLOR]".format(a.get("title"), a.get("artist")))
li.setArt({"thumb": a.get("image"), "icon": a.get("image")})
li.setInfo(
"music",
{"title": a.get("title"), "artist": a.get("artist"), "album": a.get("title"), "year": a.get("date")},
)
li.setProperty("Album_Description", a.get("details"))
_directory_items.append((plugin.url_for(musicmp3_album, quote(a.get("link"))), li, True))
xbmcplugin.addDirectoryItems(plugin.handle, _directory_items, len(_directory_items))
xbmcplugin.setContent(plugin.handle, "albums")
if fixed_view_mode:
xbmc.executebuiltin("Container.SetViewMode({0})".format(albums_view_mode))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route("/musicmp3/album/<link>")
def musicmp3_album(link):
musicmp3_api = musicMp3(MUSICMP3_DIR)
url = unquote(link)
tracks = musicmp3_api.album_tracks(url)
_directory_items = []
for t in tracks:
_infolabels = {
"title": t.get("title"),
"artist": t.get("artist"),
"album": t.get("album"),
"duration": t.get("duration"),
}
li = xbmcgui.ListItem(t.get("title"))
li.setProperty("IsPlayable", "true")
li.setArt({"thumb": t.get("image"), "icon": t.get("image")})
li.setInfo("music", _infolabels)
_directory_items.append(
(plugin.url_for(musicmp3_play, track_id=t.get("track_id"), rel=t.get("rel")), li, False)
)
xbmcplugin.addDirectoryItems(plugin.handle, _directory_items, len(_directory_items))
xbmcplugin.setContent(plugin.handle, "songs")
if fixed_view_mode:
xbmc.executebuiltin("Container.SetViewMode({0})".format(songs_view_mode))
xbmcplugin.endOfDirectory(plugin.handle)
@plugin.route("/musicmp3/play/<track_id>/<rel>")
def musicmp3_play(track_id, rel):
musicmp3_api = musicMp3(MUSICMP3_DIR)
_track = musicmp3_api.get_track(rel)
_infolabels = {"title": _track.title, "artist": _track.artist, "album": _track.album, "duration": _track.duration}
li = xbmcgui.ListItem(_track.title, path=musicmp3_api.play_url(track_id, rel))
li.setInfo("music", _infolabels)
li.setArt({"thumb": _track.image, "icon": _track.image})
li.setMimeType("audio/mpeg")
li.setContentLookup(False)
xbmcplugin.setResolvedUrl(plugin.handle, True, li)
if __name__ == "__main__":
plugin.run(sys.argv)