Switch to side-by-side view

--- a/src/cdplugins/tidal/tidal.py
+++ b/src/cdplugins/tidal/tidal.py
@@ -19,6 +19,7 @@
 
 import sys
 import os
+import posixpath
 import json
 import re
 import conftree
@@ -35,10 +36,19 @@
 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"])
@@ -63,7 +73,7 @@
     tidalconf = tidalapi.Config(quality)
     session = tidalapi.Session(config=tidalconf)
     session.login(username, password)
-
+    is_logged_in = True
 
 
 def trackentries(objid, tracks):
@@ -77,15 +87,18 @@
         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['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: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
@@ -103,6 +116,44 @@
     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):