Commit a22c924d authored by Jean-Francois Dockes's avatar Jean-Francois Dockes
Browse files

uprcl: separated the code for reading the radio config

parent 4fa24a83
......@@ -183,7 +183,8 @@ dist_cdpluginspycom_DATA = \
src/mediaserver/cdplugins/pycommon/conftree.py \
src/mediaserver/cdplugins/pycommon/routing.py \
src/mediaserver/cdplugins/pycommon/upmplgmodels.py \
src/mediaserver/cdplugins/pycommon/upmplgutils.py
src/mediaserver/cdplugins/pycommon/upmplgutils.py \
src/mediaserver/cdplugins/pycommon/upradioconf.py
cdpluginstidaldir = $(pkgdatadir)/cdplugins/tidal
dist_cdpluginstidal_DATA = \
src/mediaserver/cdplugins/tidal/tidal-app.py
......
# Copyright (C) 2016 J.F.Dockes
# Copyright (C) 2016-2021 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
......
# Copyright (C) 2021 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.
import os
import subprocess
from upmplgutils import uplog, direntry
import conftree
class UpmpdcliRadios(object):
def __init__(self, upconfig):
datadir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
if not os.path.isabs(datadir):
datadir = "/usr/share/upmpdcli"
self.fetchstream = os.path.join(datadir, "rdpl2stream", "fetchStream.py")
self._radios = []
self._readRadios(upconfig)
#uplog("Radios: %s" % self._radios)
def _readRadiosFromConf(self, conf):
'''Read radio definitions from a config file (either main file or radiolist)'''
keys = conf.getSubKeys_unsorted()
for k in keys:
if k.startswith("radio"):
title = k[6:]
uri = conf.get("url", k)
artUri = conf.get("artUrl", k)
streamUri = None
try:
streamUri = subprocess.check_output([self.fetchstream, uri])
streamUri = streamUri.decode('utf-8').strip("\r\n")
except Exception as ex:
uplog("fetchStream.py failed for %s: %s" % (title, ex))
if streamUri:
self._radios.append((title, streamUri, uri, artUri))
def _readRadios(self, upconfig):
'''Read radio definitions from main config file, then possible radiolist'''
self._radios = []
self._readRadiosFromConf(upconfig)
radiolist = upconfig.get("radiolist")
if radiolist:
radioconf = conftree.ConfSimple(radiolist)
self._readRadiosFromConf(radioconf)
def __iter__(self):
return RadioIterator(self)
class RadioIterator:
def __init__(self, radios):
self._radios = radios
self._index = 0
def __next__(self):
if self._index < (len(self._radios._radios)):
radio = self._radios._radios[self._index]
result = {"title" : radio[0], "streamUri" : radio[1],
"uri" : radio[2], "artUri" : radio[3]}
self._index +=1
return result
raise StopIteration
if __name__ == "__main__":
conf = conftree.ConfSimple("/etc/upmpdcli.conf")
radios = UpmpdcliRadios(conf)
for radio in radios:
print("%s" % radio)
......@@ -29,18 +29,15 @@ import uprclutils
import uprclinit
from recoll import recoll
import conftree
import upradioconf
class Playlists(object):
def __init__(self, rcldocs, httphp, pathprefix):
self._idprefix = '0$uprcl$playlists'
self._httphp = httphp
self._pprefix = pathprefix
self._radios = []
datadir = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
self.fetchstream = os.path.join(datadir, "rdpl2stream", "fetchStream.py")
self._radios = upradioconf.UpmpdcliRadios(uprclinit.g_upconfig)
self.recoll2playlists(rcldocs)
self.upradios2playlist()
# Create the untagged entries static vector by filtering the global
# doc vector, storing the indexes of the playlists.
......@@ -54,33 +51,7 @@ class Playlists(object):
if doc["mtype"] == 'audio/x-mpegurl':
self.utidx.append(docidx)
def readRadiosFromConf(self, conf):
keys = conf.getSubKeys_unsorted()
for k in keys:
if k.startswith("radio"):
title = k[6:]
uri = conf.get("url", k)
artUri = conf.get("artUrl", k)
realluri = None
try:
realluri = subprocess.check_output([self.fetchstream, uri])
realluri = realluri.decode('utf-8').strip("\r\n")
except Exception as ex:
uplog("fetchStream.py failed for %s: %s" % (title, ex))
pass
if realluri:
self._radios.append((title, realluri, uri, artUri))
def upradios2playlist(self):
self._radios = []
self.readRadiosFromConf(uprclinit.g_upconfig)
radiolist = uprclinit.g_upconfig.get("radiolist")
if radiolist:
radioconf = conftree.ConfSimple(radiolist)
self.readRadiosFromConf(radioconf)
#uplog("Radios: %s" % self._radios)
# Compute index into our entries vector by 'parsing' the objid.
# Entry 0 is not attributed (browsing 0 -> our root)
# idx == len(ourlist) is valid too and does not point to an rcldoc
......@@ -111,19 +82,16 @@ class Playlists(object):
str(len(self.utidx)) + ' playlists'),]
def radioToEntry(self, pid, idx, radio):
title = radio[0]
uri = radio[1]
arturi = radio[3]
id = pid + '$e' + str(idx)
return {
'pid': pid,
'id': id,
'uri': uri,
'uri': radio["streamUri"],
'tp': 'it',
'res:mime': "audio/mpeg",
'upnp:class': 'object.item.audioItem.musicTrack',
'upnp:albumArtURI': arturi,
'tt': title
'upnp:albumArtURI': radio["artUri"],
'tt': radio["title"]
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment