Parent: [e7698b] (diff)

Child: [893fd1] (diff)

Download this file

uprclutils.py    246 lines (212 with data), 6.9 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
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
from __future__ import print_function
import sys
import posixpath
import urllib
import os
audiomtypes = frozenset([
'audio/mpeg',
'application/x-flac',
'application/ogg',
'audio/aac',
'audio/mp4',
'audio/x-aiff',
'audio/x-wav',
'inode/directory'
])
upnp2rclfields = {'upnp:album': 'album',
'releasedate' : 'date',
'upnp:originalTrackNumber' : 'tracknumber',
'upnp:artist' : 'artist',
'upnp:genre' : 'genre',
'res:mime' : 'mtype',
'duration' : 'duration',
'res:samplefreq' : 'sample_rate'
}
def rcldoctoentry(id, pid, httphp, pathprefix, doc):
"""
Transform a Doc objects into the format expected by the parent
Args:
id (str): objid for the entry
pid (str): objid for the browsed object (the parent container)
httphp: the hostport part of the generated track urls
pathprefix: is provided by our parent process (it's used to
what plugin an url belongs too when needed for
translating the internal into the real url (for plugins
based on external-services)
doc is the Doc object to be translated
Returns:
A dict representing an UPnP item, with the
keys as expected in the plgwithslave.cxx resultToEntries() function.
The permanent URIs, are of the following form, based on the
configured host:port and pathprefix arguments and track Id:
TBD
http://host:port/pathprefix/track?version=1&trackId=<trackid>
"""
uplog("rcldoctoentry: pid %s id %s mtype %s" %
(pid, id, doc.mtype))
li = {}
if doc.mtype not in audiomtypes:
return li
li['pid'] = pid
li['id'] = id
li['tp'] = 'ct' if doc.mtype == 'inode/directory' else 'it'
# Why no dc.title??
li['tt'] = doc.title
# TBD
li['upnp:class'] = 'object.item.audioItem.musicTrack'
# TBD Date format ?
# !! Albumart will have to come from somewhere else !
# li['upnp:class'] = doc.upnpclass
#li['res:channels'] =
#li['res:size'] =
#li['res:bitrate'] =
### #if doc.albumarturi:
### #li['upnp:albumArtURI'] = track.album.image
### li['discnumber'] = str(track.disc_num)
#albumartist=
#comment=
#composer=
#conductor=
#discnumber=
#genre=
#lyricist=
#lyrics=
for oname,dname in upnp2rclfields.iteritems():
val = getattr(doc, dname)
if val:
li[oname] = val
try:
val = li['upnp:originalTrackNumber']
l = val.split('/')
li['upnp:originalTrackNumber'] = l[0]
except:
pass
# Compute the url. We use the URL from recoll, stripped of file://
# and with the pathprefix prepended (the pathprefix is used by our
# parent process to match urls to plugins)
path = doc.getbinurl()
path = path[7:]
path = pathprefix + path
li['uri'] = "http://%s%s" % (httphp, urllib.quote(path))
uplog("rcldoctoentry: uri: %s" % li['uri'])
return li
def cmpentries(e1, e2):
tp1 = e1['tp']
tp2 = e2['tp']
isct1 = tp1 == 'ct'
isct2 = tp2 == 'ct'
# Containers come before items, and are sorted in alphabetic order
if isct1 and not isct2:
return 1
elif not isct1 and isct2:
return -1
elif isct1 and isct2:
tt1 = e1['tt']
tt2 = e2['tt']
if tt1 < tt2:
return -1
elif tt1 > tt2:
return 1
else:
return 0
# Tracks. Sort by album then directory then track number
k = 'upnp:album'
a1 = e1[k] if k in e1 else ""
a2 = e2[k] if k in e2 else ""
if a1 < a2:
return -1
elif a1 > a2:
return 1
d1 = os.path.dirname(e1['uri'])
d2 = os.path.dirname(e2['uri'])
if d1 < d2:
return -1
elif d1 > d2:
return 1
k = 'upnp:originalTrackNumber'
a1 = e1[k] if k in e1 else "0"
a2 = e2[k] if k in e2 else "0"
return int(a1) - int(a2)
def rclpathtoreal(path, pathprefix, httphp, pathmap):
path = path.replace(pathprefix, '', 1)
found = False
for fsp,htp in pathmap.iteritems():
if path.startswith(fsp):
path = path.replace(fsp, htp, 1)
found = True
if not found:
return None
return "http://" + httphp + path
def rcldirentry(id, pid, title, arturi=None, artist=None, upnpclass=None,
searchable='1'):
""" Create container entry in format expected by parent """
ret = {'id':id, 'pid':pid, 'tt':title, 'tp':'ct', 'searchable':searchable}
if arturi:
ret['upnp:albumArtURI'] = arturi
if artist:
ret['upnp:artist'] = artist
if upnpclass:
ret['upnp:class'] = upnpclass
else:
ret['upnp:class'] = 'object.container'
return ret
def uplog(s):
print(("%s: %s" % ('uprcl', s)).encode('utf-8'), file=sys.stderr)
# Parse string into (possibly multiword) tokens
# 'a b "one phrase" c' -> [a, b, 'one phrase', c]
def stringToStrings(str):
# States. Note that ESCAPE can only occur inside INQUOTE
SPACE, TOKEN, INQUOTE, ESCAPE = range(4)
tokens = []
curtok = ""
state = SPACE;
for c in str:
if c == '"':
if state == SPACE:
state = INQUOTE
elif state == TOKEN:
curtok += '"'
elif state == INQUOTE:
if curtok:
tokens.append(curtok);
curtok = ""
state = SPACE
elif state == ESCAPE:
curtok += '"'
state = INQUOTE
continue;
elif c == '\\':
if state == SPACE or state == TOKEN:
curtok += '\\'
state = TOKEN
elif state == INQUOTE:
state = ESCAPE
elif state == ESCAPE:
curtok += '\\'
state = INQUOTE
continue
elif c == ' ' or c == '\t' or c == '\n' or c == '\r':
if state == SPACE or state == TOKEN:
if curtok:
tokens.append(curtok)
curtok = ""
state = SPACE
elif state == INQUOTE or state == ESCAPE:
curtok += c
continue;
else:
if state == ESCAPE:
state = INQUOTE
elif state == SPACE:
state = TOKEN
elif state == TOKEN or state == INQUOTE:
pass
curtok += c
if state == SPACE:
pass
elif state == TOKEN:
if curtok:
tokens.append(curtok)
elif state == INQUOTE or state == ESCAPE:
raise Exception("Bad string: <" + str + ">")
return tokens