Parent: [af4850] (diff)

Child: [827592] (diff)

Download this file

tidal.py    210 lines (180 with data), 7.2 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
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
#!/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 posixpath
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
global httphp
global pathprefix
global is_logged_in
if is_logged_in:
return True
if "UPMPD_HTTPHOSTPORT" not in os.environ:
raise Exception("No UPMPD_HTTPHOSTPORT in environment")
httphp = os.environ["UPMPD_HTTPHOSTPORT"]
if "UPMPD_PATHPREFIX" not in os.environ:
raise Exception("No UPMPD_PATHPREFIX in environment")
pathprefix = os.environ["UPMPD_PATHPREFIX"]
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)
is_logged_in = True
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' % httphp + \
posixpath.join(pathprefix,
'track?version=1&trackId=%s' % \
track.id)
processor.rclog("URI: [%s]" % li['uri'])
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: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
def trackid_from_path(a):
if 'path' not in a:
raise Exception("No path in args")
path = a['path']
# pathprefix + 'track?version=1&trackId=trackid
exp = posixpath.join(pathprefix, '''track\?version=1&trackId=(.+)$''')
m = re.match(exp, path)
if m is None:
raise Exception("trackuri: path [%s] does not match [%s]" % (path, exp))
trackid = m.group(1)
processor.rclog("trackid: [%s]" % trackid)
return trackid
@routes.route('trackuri')
def trackuri(a):
processor.rclog("trackuri: [%s]" % a)
trackid = trackid_from_path(a)
maybelogin()
media_url = session.get_media_url(trackid)
processor.rclog("%s" % media_url)
if not media_url.startswith('http://') and not \
media_url.startswith('https://'):
host, tail = media_url.split('/', 1)
app, playpath = tail.split('/mp4:', 1)
media_url = 'rtmp://%s app=%s playpath=mp4:%s' % (host, app, playpath)
mimetype = 'audio/flac' if quality == Quality.lossless else 'audio/mpeg'
return {'media_url' : media_url, 'mimetype' : mimetype}
@routes.route('mimetype')
def mimetype(a):
processor.rclog("mimetype: [%s]" % a)
maybelogin()
mimetype = 'audio/flac' if quality == Quality.lossless else 'audio/mpeg'
return {'mimetype' : mimetype}
@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()