Child: [ef0988] (diff)

Download this file

tidal.py    159 lines (134 with data), 5.3 kB

  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
#!/usr/bin/env python
# Copyright (C) 2016 J.F.Dockes
# 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 2 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, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from __future__ import print_function
import sys
import os
import json
import re
import conftree
import cmdtalkplugin
import tidalapi
from tidalapi.models import Album, Artist
from tidalapi import Quality
routes = cmdtalkplugin.Routes()
processor = cmdtalkplugin.Processor(routes)
is_logged_in = False
def maybelogin():
global session
global quality
if is_logged_in:
return True
if "UPMPD_CONFIG" not in os.environ:
raise Exception("No UPMPD_CONFIG in environment")
upconfig = conftree.ConfSimple(os.environ["UPMPD_CONFIG"])
username = upconfig.get('tidaluser')
password = upconfig.get('tidalpass')
qalstr = upconfig.get('tidalquality')
if not username or not password:
raise Exception("tidaluser and/or tidalpass not set in configuration")
if qalstr == 'lossless':
quality = Quality.lossless
elif qalstr == 'high':
quality = Quality.high
elif qalstr == 'low':
quality = Quality.low
elif not qalstr:
quality = Quality.high
else:
raise Exception("Bad tidal quality string in config. "+
"Must be lossless/high/low")
tidalconf = tidalapi.Config(quality)
session = tidalapi.Session(config=tidalconf)
session.login(username, password)
def trackentries(objid, tracks):
entries = []
for track in tracks:
if not track.available:
continue
li = {}
li['pid'] = objid
li['id'] = objid + '$' + "%s"%track.id
li['tt'] = track.name
li['uri'] = 'http://s.com/upmpcli-tidal/track?version=1&trackId=%s' % track.id
li['tp'] = 'it'
if track.album:
li['upnp:album'] = track.album.name
if track.album.image:
li['upnp:albumArtUri'] = track.album.image
li['upnp:originalTrackNumber'] = str(track.track_num)
li['upnp:artist'] = track.artist.name
li['dc:creator'] = track.artist.name
li['dc:title'] = track.name
li['discnumber'] = str(track.disc_num)
li['duration'] = track.duration
# track.album.release_date.year if track.album.release_date else None,
entries.append(li)
return entries
def direntry(id, pid, title):
return {'id': id, 'pid' : pid, 'tt':title, 'tp':'ct'}
def direntries(objid, ttidlist):
content = []
for tt,id in ttidlist:
content.append(direntry(objid+'$'+id, objid, tt))
return content
@routes.route('browse')
def browse(a):
processor.rclog("browse: [%s]" % a)
if 'objid' not in a:
raise Exception("No objid in args")
objid = a['objid']
if re.match('0\$tidal', objid) is None:
raise Exception("bad objid [%s]" % objid)
maybelogin()
if objid == '0$tidal':
# Root
content = direntries(objid,
(('My Music', 'my_music'),
('Featured Playlists', 'featured'),
("What's New", 'new'),
('Genres', 'genres'),
('Moods', 'moods'),
('Search', 'search')))
elif objid == '0$tidal$my_music':
content = direntries(objid,
(('My Playlists', 'my_pl'),
('Favourite Playlists', 'fav_pl'),
('Favourite Artists', 'fav_art'),
('Favourite Albums', 'fav_alb'),
('Favourite Tracks', 'fav_trk')))
elif objid == '0$tidal$my_music$fav_trk':
content = trackentries(objid, session.user.favorites.tracks())
elif objid == '0$tidal$my_music$my_pl':
items = session.user.playlists()
view(items, urls_from_id(playlist_view, items))
elif objid == '0$tidal$my_music$fav_pl':
items = session.user.favorites.playlists()
view(items, urls_from_id(playlist_view, items))
elif objid == '0$tidal$my_music$fav_art':
xbmcplugin.setContent(plugin.handle, 'artists')
items = session.user.favorites.artists()
view(items, urls_from_id(artist_view, items))
elif objid == '0$tidal$my_music$fav_alb':
xbmcplugin.setContent(plugin.handle, 'albums')
items = session.user.favorites.albums()
view(items, urls_from_id(album_view, items))
else:
raise Exception("unhandled path")
encoded = json.dumps(content)
return {"entries":encoded}
processor.rclog("Tidal running")
processor.mainloop()