Switch to unified view

a b/src/mediaserver/cdplugins/uprcl/uprcl-app.py
1
#!/usr/bin/env python
2
#
3
# Copyright (C) 2016 J.F.Dockes
4
#
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
from __future__ import print_function
19
import sys
20
import os
21
import json
22
import posixpath
23
import re
24
import conftree
25
import cmdtalkplugin
26
import folders
27
from uprclutils import *
28
29
# Func name to method mapper
30
dispatcher = cmdtalkplugin.Dispatch()
31
# Pipe message handler
32
msgproc = cmdtalkplugin.Processor(dispatcher)
33
34
is_logged_in = False
35
36
def maybelogin():
37
    global httphp
38
    global pathprefix
39
    global is_logged_in
40
    
41
    if is_logged_in:
42
        return True
43
44
    if "UPMPD_HTTPHOSTPORT" not in os.environ:
45
        raise Exception("No UPMPD_HTTPHOSTPORT in environment")
46
    httphp = os.environ["UPMPD_HTTPHOSTPORT"]
47
    if "UPMPD_PATHPREFIX" not in os.environ:
48
        raise Exception("No UPMPD_PATHPREFIX in environment")
49
    pathprefix = os.environ["UPMPD_PATHPREFIX"]
50
    if "UPMPD_CONFIG" not in os.environ:
51
        raise Exception("No UPMPD_CONFIG in environment")
52
    upconfig = conftree.ConfSimple(os.environ["UPMPD_CONFIG"])
53
    
54
55
@dispatcher.record('trackuri')
56
def trackuri(a):
57
    msgproc.log("trackuri: [%s]" % a)
58
    trackid = trackid_from_urlpath(pathprefix, a)
59
    maybelogin()
60
    mime, kbs = get_mimeandkbs()
61
    return {'media_url' : media_url, 'mimetype' : mime, 'kbs' : kbs}
62
63
64
@dispatcher.record('browse')
65
def browse(a):
66
    msgproc.log("browse: [%s]" % a)
67
    if 'objid' not in a:
68
        raise Exception("No objid in args")
69
    objid = a['objid']
70
    bflg = a['flag'] if 'flag' in a else 'children'
71
    
72
    if re.match('0\$uprcl\$', objid) is None:
73
        raise Exception("bad objid [%s]" % objid)
74
    maybelogin()
75
76
    idpath = objid.replace('0$uprcl$', '', 1)
77
    msgproc.log("browse: idpath: %s" % idpath)
78
79
    entries = []
80
81
    if bflg == 'meta':
82
        m = re.match('.*\$(.+)$', idpath)
83
        if m:
84
            trackid = m.group(1)
85
            # get doc from trackid or whatever
86
            doc = {}
87
            entries += trackentries(httphp, pathprefix,
88
                                    objid, [])
89
    else:
90
        if not idpath:
91
            # Root dir
92
            entries.append(rcldirentry('0$uprcl$' + 'folders', '0$uprcl$',
93
                                        '[folders]'))
94
        if idpath.startswith("folders"):
95
            entries = folders.browse(objid, bflg)
96
        else:
97
            pass
98
99
100
    #msgproc.log("%s" % entries)
101
    encoded = json.dumps(entries)
102
    return {"entries" : encoded}
103
104
@dispatcher.record('search')
105
def search(a):
106
    msgproc.log("search: [%s]" % a)
107
    objid = a['objid']
108
    field = a['field'] if 'field' in a else None
109
    value = a['value']
110
    objkind = a['objkind'] if 'objkind' in a else None
111
112
    if re.match('0\$uprcl\$', objid) is None:
113
        raise Exception("bad objid [%s]" % objid)
114
    maybelogin()
115
    
116
    searchresults = session.search(value)
117
118
    if objkind and objkind not in ['artist', 'album', 'playlist', 'track']:
119
        msgproc.log('Unknown objkind \'%s\'' % objkind)
120
        objkind = None
121
    if objkind is None or objkind == 'artist':
122
        view(searchresults.artists,
123
             urls_from_id(artist_view, searchresults.artists), end=False)
124
    if objkind is None or objkind == 'album':
125
        view(searchresults.albums,
126
             urls_from_id(album_view, searchresults.albums), end=False)
127
    if objkind is None or objkind == 'playlist':
128
        view(searchresults.playlists,
129
             urls_from_id(playlist_view, searchresults.playlists), end=False)
130
    if objkind is None or objkind == 'track':
131
        track_list(searchresults.tracks)
132
133
    #msgproc.log("%s" % xbmcplugin.entries)
134
    encoded = json.dumps(xbmcplugin.entries)
135
    return {"entries" : encoded}
136
137
msgproc.log("Uprcl running")
138
msgproc.mainloop()