示例#1
0
        def build_upnp_item(elisa_item):
            UPnPClass = classChooser(elisa_item['mimetype'])
            upnp_item = None
            if UPnPClass:
                upnp_item = UPnPClass(elisa_item['id'],
                                      elisa_item['parent_id'],
                                      elisa_item['name'])
                if isinstance(upnp_item, Container):
                    upnp_item.childCount = len(elisa_item.get('children', []))
                    if len(Filter) > 0:
                        upnp_item.searchable = True
                        upnp_item.searchClass = ('object', )
                else:
                    internal_url = elisa_item['location'].get('internal')
                    external_url = elisa_item['location'].get('external')
                    try:
                        size = elisa_item['size']
                    except:
                        size = None
                    try:
                        cover = elisa_item['cover']
                        if cover != '':
                            upnp_item.albumArtURI = cover
                    except:
                        pass

                    res = Resource(internal_url, 'internal:%s:*:*' % self.host)
                    res.size = size
                    upnp_item.res.append(res)
                    res = Resource(external_url,
                                   'http-get:*:%s:*' % elisa_item['mimetype'])
                    res.size = size
                    upnp_item.res.append(res)

            return upnp_item
示例#2
0
    def rebuild(self, urlbase):
        # print('rebuild', self.mimetype)
        if self.mimetype != 'item':
            return
        # print('rebuild for', self.get_path())
        mimetype, _ = mimetypes.guess_type(self.get_path(), strict=False)
        if mimetype is None:
            return
        self.mimetype = mimetype
        # print('rebuild', self.mimetype)
        UPnPClass = classChooser(self.mimetype)
        self.item = UPnPClass(self.id, self.parent.id, self.get_name())
        if getattr(self.parent, 'cover', None):
            _, ext = os.path.splitext(self.parent.cover)
            # add the cover image extension to help
            # clients not reacting on the mimetype
            self.item.albumArtURI = ''.join(
                (urlbase, str(self.id), '?cover', ext))

        _, host_port, _, _, _ = urlsplit(urlbase)
        if host_port.find(':') != -1:
            host, port = tuple(host_port.split(':'))
        else:
            host = host_port

        res = Resource(
            'file://' + quote(self.get_path()),
            f'internal:{host}:{self.mimetype}:*',
        )
        try:
            res.size = self.location.getsize()
        except Exception:
            res.size = 0
        self.item.res.append(res)
        res = Resource(self.url, f'http-get:*:{self.mimetype}:*')

        try:
            res.size = self.location.getsize()
        except Exception:
            res.size = 0
        self.item.res.append(res)

        try:
            # FIXME: getmtime is deprecated in Twisted 2.6
            self.item.date = datetime.fromtimestamp(self.location.getmtime())
        except Exception:
            self.item.date = None

        self.parent.update_id += 1
示例#3
0
    def __init__(self, id, name, parent, mimetype,
                 urlbase, host, update=False):
        log.LogAble.__init__(self)
        self.id = id
        self.name = name
        self.mimetype = mimetype

        self.parent = parent
        if parent:
            parent.add_child(self, update=update)

        if parent is None:
            parent_id = -1
        else:
            parent_id = parent.get_id()

        UPnPClass = classChooser(
            mimetype, sub='music')  # FIXME: this is stupid
        self.item = UPnPClass(id, parent_id, self.name)
        self.child_count = 0
        self.children = []

        if len(urlbase) and urlbase[-1] != '/':
            urlbase += '/'

        # self.url = urlbase + str(self.id)
        self.url = self.name

        if self.mimetype == 'directory':
            self.update_id = 0
        else:
            res = Resource(self.url, f'internal:{host}:{self.mimetype}:*')
            res.size = None
            self.item.res.append(res)
            self.item.artist = self.parent.name
示例#4
0
    def __init__(self,
                 id,
                 obj,
                 parent,
                 mimetype,
                 urlbase,
                 UPnPClass,
                 update=False):
        log.LogAble.__init__(self)
        self.id = id

        self.name = obj.get('name')
        self.title = obj.get('title')
        self.artist = obj.get('artist')
        self.creator = obj.get('creator')
        self.album = obj.get('album')
        self.duration = obj.get('duration')
        self.mimetype = mimetype

        self.parent = parent
        if parent:
            parent.add_child(self, update=update)

        if parent is None:
            parent_id = -1
        else:
            parent_id = parent.get_id()

        self.item = UPnPClass(id, parent_id, self.title, False, self.creator)
        if isinstance(self.item, Container):
            self.item.childCount = 0
        self.child_count = 0
        self.children = []

        if len(urlbase) and urlbase[-1] != '/':
            urlbase += '/'

        if self.mimetype == 'directory':
            self.url = urlbase + str(self.id)
        else:
            self.url = urlbase + str(self.id)
            self.location = LFMProxyStream(obj.get('url'), self)
            # self.url = obj.get('url')

        if self.mimetype == 'directory':
            self.update_id = 0
        else:
            protocols = (
                'DLNA.ORG_PN=MP3',
                'DLNA.ORG_CI=0',
                'DLNA.ORG_OP=01',
                'DLNA.ORG_FLAGS=01700000000000000000000000000000',
            )
            res = Resource(
                self.url,
                f'http-get:*:{obj.get("mimetype")}:{";".join(protocols)}',
            )
            res.size = -1  # None
            self.item.res.append(res)
示例#5
0
 def __init__(self, parent_id, item_id, urlbase, **kwargs):
     super(IRadioItem, self).__init__(parent_id, item_id, urlbase, **kwargs)
     protocols = ('DLNA.ORG_PN=MP3', 'DLNA.ORG_CI=0', 'DLNA.ORG_OP=01',
                  'DLNA.ORG_FLAGS=01700000000000000000000000000000')
     res = Resource(self.url,
                    f'http-get:*:{self.mimetype}:{";".join(protocols)}')
     res.size = 0  # None
     self.item.res.append(res)
示例#6
0
    def __init__(self,
                 id,
                 obj,
                 parent,
                 mimetype,
                 urlbase,
                 UPnPClass,
                 update=False):
        BackendItem.__init__(self)
        self.id = id

        self.name = obj.get('title')  # .encode('utf-8')
        if self.name == None:
            self.name = obj.get('name')
        if self.name == None:
            self.name = id

        self.mimetype = mimetype

        self.gallery2_id = obj.get('gallery2_id')

        self.parent = parent
        if parent:
            parent.add_child(self, update=update)

        if parent == None:
            parent_id = -1
        else:
            parent_id = parent.get_id()

        self.item = UPnPClass(id, parent_id, self.name)
        if isinstance(self.item, Container):
            self.item.childCount = 0
        self.child_count = 0
        self.children = None

        if (len(urlbase) and urlbase[-1] != '/'):
            urlbase += '/'

        if parent == None:
            self.gallery2_url = None
            self.url = urlbase + str(self.id)
        elif self.mimetype == 'directory':
            #self.gallery2_url = parent.store.get_url_for_album(self.gallery2_id)
            self.url = urlbase + str(self.id)
        else:
            self.gallery2_url = parent.store.get_url_for_image(
                self.gallery2_id)
            self.url = urlbase + str(self.id)
            self.location = ProxyGallery2Image(self.gallery2_url)

        if self.mimetype == 'directory':
            self.update_id = 0
        else:
            res = Resource(self.gallery2_url,
                           'http-get:*:%s:*' % self.mimetype)
            res.size = None
            self.item.res.append(res)
示例#7
0
 def get_item(self):
     if self.item == None:
         upnp_id = self.get_id()
         upnp_parent_id = self.parent.get_id()
         self.item = DIDLLite.AudioBroadcast(upnp_id, upnp_parent_id,
                                             self.name)
         res = Resource(
             self.url, 'http-get:*:%s:%s' % (self.mimetype, ';'.join(
                 ('DLNA.ORG_PN=MP3', 'DLNA.ORG_CI=0', 'DLNA.ORG_OP=01',
                  'DLNA.ORG_FLAGS=01700000000000000000000000000000'))))
         res.size = 0  #None
         self.item.res.append(res)
     return self.item
示例#8
0
 def get_item(self):
     if self.item is None:
         upnp_id = self.get_id()
         upnp_parent_id = self.parent.get_id()
         self.item = DIDLLite.AudioBroadcast(upnp_id, upnp_parent_id,
                                             self.name)
         self.item.albumArtURI = self.image
         protocols = ';'.join(
             ('DLNA.ORG_PN=MP3', 'DLNA.ORG_CI=0', 'DLNA.ORG_OP=01',
              'DLNA.ORG_FLAGS=01700000000000000000000000000000'))
         res = Resource(self.stream_url,
                        f'http-get:*:{self.mimetype}:{protocols}')
         res.size = 0  # None
         self.item.res.append(res)
     return self.item
示例#9
0
    def __init__(self,
                 id,
                 obj,
                 parent,
                 mimetype,
                 urlbase,
                 UPnPClass,
                 update=False):
        self.id = id

        self.name = obj.get('name')
        self.mimetype = mimetype

        self.parent = parent
        if parent:
            parent.add_child(self, update=update)

        if parent == None:
            parent_id = -1
        else:
            parent_id = parent.get_id()

        self.item = UPnPClass(id, parent_id, self.name)
        if isinstance(self.item, Container):
            self.item.childCount = 0
        self.child_count = 0
        self.children = None

        if (len(urlbase) and urlbase[-1] != '/'):
            urlbase += '/'

        if self.mimetype == 'directory':
            self.url = urlbase + str(self.id)
        else:
            self.url = urlbase + str(self.id)
            self.location = ProxyStream(obj.get('url'))
            #self.url = obj.get('url')

        if self.mimetype == 'directory':
            self.update_id = 0
        else:
            res = Resource(
                self.url, 'http-get:*:%s:%s' % (obj.get('mimetype'), ';'.join(
                    ('DLNA.ORG_PN=MP3', 'DLNA.ORG_CI=0', 'DLNA.ORG_OP=01',
                     'DLNA.ORG_FLAGS=01700000000000000000000000000000'))))
            res.size = 0  #None
            self.item.res.append(res)
示例#10
0
    def __init__(self,
                 id,
                 obj,
                 parent,
                 mimetype,
                 urlbase,
                 UPnPClass,
                 update=False):
        BackendItem.__init__(self)
        self.id = id
        if mimetype == 'directory':
            self.name = obj
            self.mimetype = mimetype
        else:
            self.name = obj.get('name')
            self.mimetype = mimetype

        self.parent = parent
        if parent:
            parent.add_child(self, update=update)

        if parent == None:
            parent_id = -1
        else:
            parent_id = parent.get_id()

        self.item = UPnPClass(id, parent_id, self.name)
        if isinstance(self.item, Container):
            self.item.childCount = 0
        self.children = []

        if (len(urlbase) and urlbase[-1] != '/'):
            urlbase += '/'

        if self.mimetype == 'directory':
            self.url = urlbase + str(self.id)
        else:
            self.url = obj.get('url')

        if self.mimetype == 'directory':
            self.update_id = 0
        else:
            res = Resource(self.url, obj.get('protocol'))
            res.size = None
            self.item.res.append(res)
示例#11
0
    def get_item(self):
        if self.item is None:
            upnp_id = self.get_id()
            upnp_parent_id = self.parent.get_id()
            if self.mimetype.startswith('video/'):
                item = DIDLLite.VideoItem(upnp_id, upnp_parent_id, self.name)
            else:
                item = DIDLLite.AudioItem(upnp_id, upnp_parent_id, self.name)

            # what to do with MMS:// feeds?
            protocol = 'http-get'
            if self.stream_url.startswith('rtsp://'):
                protocol = 'rtsp-rtp-udp'

            res = Resource(self.stream_url, f'{protocol}:*:{self.mimetype}:*')
            res.size = None
            item.res.append(res)

            self.item = item
        return self.item
示例#12
0
    def get_item(self):
        if self.item == None:
            upnp_id = self.get_id()
            upnp_parent_id = self.parent.get_id()
            if (self.mimetype.startswith('video/')):
                item = DIDLLite.VideoItem(upnp_id, upnp_parent_id, self.name)
            else:
                item = DIDLLite.AudioItem(upnp_id, upnp_parent_id, self.name)

            # what to do with MMS:// feeds?
            protocol = "http-get"
            if self.stream_url.startswith("rtsp://"):
                protocol = "rtsp-rtp-udp"

            res = Resource(self.stream_url, '%s:*:%s:*' % (protocol, self.mimetype))
            res.size = None
            item.res.append(res)

            self.item = item
        return self.item
示例#13
0
        def process(result):
            for size in result.getiterator('size'):
                #print size.get('label'), size.get('source')
                if size.get('label') == 'Original':
                    self.original_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                    if self.store.proxy == False:
                        self.url = self.original_url[0]
                    else:
                        self.location = ProxyImage(self.original_url[0])
                elif size.get('label') == 'Large':
                    self.large_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                    if self.store.proxy == False:
                        self.url = self.large_url[0]
                    else:
                        self.location = ProxyImage(self.large_url[0])
                elif size.get('label') == 'Medium':
                    self.medium_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                elif size.get('label') == 'Small':
                    self.small_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                elif size.get('label') == 'Thumbnail':
                    self.thumb_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))

            self.item = Photo(self.id, self.parent.get_id(), self.get_name())
            #print self.id, self.store.proxy, self.url
            self.item.date = self.date
            self.item.attachments = {}
            dlna_tags = simple_dlna_tags[:]
            dlna_tags[3] = 'DLNA.ORG_FLAGS=00f00000000000000000000000000000'
            if hasattr(self, 'original_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_LRG'
                if self.store.proxy == False:
                    res = Resource(self.original_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=original', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['original'] = ProxyImage(self.original_url[0])
                res.resolution = self.original_url[1]
                self.item.res.append(res)
            elif hasattr(self, 'large_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_LRG'
                if self.store.proxy == False:
                    res = Resource(self.large_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=large', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['large'] = ProxyImage(self.large_url[0])
                res.resolution = self.large_url[1]
                self.item.res.append(res)
            if hasattr(self, 'medium_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_MED'
                if self.store.proxy == False:
                    res = Resource(self.medium_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=medium', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['medium'] = ProxyImage(self.medium_url[0])
                res.resolution = self.medium_url[1]
                self.item.res.append(res)
            if hasattr(self, 'small_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_SM'
                if self.store.proxy == False:
                    res = Resource(self.small_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=small', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['small'] = ProxyImage(self.small_url[0])
                res.resolution = self.small_url[1]
                self.item.res.append(res)
            if hasattr(self, 'thumb_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_TN'
                if self.store.proxy == False:
                    res = Resource(self.thumb_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=thumb', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['thumb'] = ProxyImage(self.thumb_url[0])
                res.resolution = self.thumb_url[1]
                self.item.res.append(res)

            return self.item
示例#14
0
    def __init__(
        self,
        object_id,
        parent,
        path,
        mimetype,
        urlbase,
        UPnPClass,
        update=False,
        store=None,
    ):
        BackendItem.__init__(self)
        self.id = object_id
        self.parent = parent
        if parent:
            parent.add_child(self, update=update)
        if mimetype == 'root':
            self.location = str(path)
        else:
            if mimetype == 'item' and path is None:
                path = os.path.join(parent.get_realpath(), str(self.id))
            # self.location = FilePath(unicode(path))
            self.location = FilePath(path)
        self.mimetype = mimetype
        if urlbase[-1] != '/':
            urlbase += '/'
        self.url = urlbase + str(self.id)

        self.store = store

        if parent is None:
            parent_id = -1
        else:
            parent_id = parent.get_id()

        self.item = UPnPClass(object_id, parent_id, self.get_name())
        if isinstance(self.item, Container):
            self.item.childCount = 0
        self.child_count = 0
        self.children = []
        self.sorted = False
        self.caption = None

        if mimetype in ['directory', 'root']:
            self.update_id = 0
            self.get_url = lambda: self.url
            # self.item.searchable = True
            # self.item.searchClass = 'object'
            if (isinstance(self.location, FilePath)
                    and self.location.isdir() is True):
                self.check_for_cover_art()
                if getattr(self, 'cover', None):
                    _, ext = os.path.splitext(self.cover)
                    ''' add the cover image extension to help clients
                        not reacting on the mimetype '''
                    self.item.albumArtURI = ''.join(
                        (urlbase, str(self.id), '?cover', str(ext)))
        else:
            self.get_url = lambda: self.url

            if self.mimetype.startswith('audio/'):
                if getattr(parent, 'cover', None):
                    _, ext = os.path.splitext(parent.cover)
                    ''' add the cover image extension to help clients
                        not reacting on the mimetype '''
                    self.item.albumArtURI = ''.join(
                        (urlbase, str(self.id), '?cover', ext))

            _, host_port, _, _, _ = urlsplit(urlbase)
            if host_port.find(':') != -1:
                host, port = tuple(host_port.split(':'))
            else:
                host = host_port

            try:
                size = self.location.getsize()
            except Exception:
                size = 0

            if (self.store.server and self.store.server.coherence.config.get(
                    'transcoding', 'no') == 'yes'):
                if self.mimetype in (
                        'application/ogg',
                        'audio/ogg',
                        'audio/x-wav',
                        'audio/x-m4a',
                        'application/x-flac',
                ):
                    new_res = Resource(
                        self.url + '/transcoded.mp3',
                        f'http-get:*:{"audio/mpeg"}:*',
                    )
                    new_res.size = None
                    # self.item.res.append(new_res)

            if mimetype != 'item':
                res = Resource(
                    'file://' + quote(self.get_path(), encoding='utf-8'),
                    f'internal:{host}:{self.mimetype}:*',
                )
                res.size = size
                self.item.res.append(res)

            if mimetype != 'item':
                res = Resource(self.url, f'http-get:*:{self.mimetype}:*')
            else:
                res = Resource(self.url, 'http-get:*:*:*')

            res.size = size
            self.item.res.append(res)
            ''' if this item is of type audio and we want to add a transcoding
                rule for it, this is the way to do it:

                create a new Resource object, at least a 'http-get'
                and maybe an 'internal' one too

                for transcoding to wav this looks like that

                res = Resource(
                    url_for_transcoded audio,
                    'http-get:*:audio/x-wav:%s'% ';'.join(
                        ['DLNA.ORG_PN=JPEG_TN']+simple_dlna_tags))
                res.size = None
                self.item.res.append(res)
            '''

            if (self.store.server and self.store.server.coherence.config.get(
                    'transcoding', 'no') == 'yes'):
                if self.mimetype in (
                        'audio/mpeg',
                        'application/ogg',
                        'audio/ogg',
                        'audio/x-wav',
                        'audio/x-m4a',
                        'audio/flac',
                        'application/x-flac',
                ):
                    dlna_pn = 'DLNA.ORG_PN=LPCM'
                    dlna_tags = simple_dlna_tags[:]
                    # dlna_tags[1] = 'DLNA.ORG_OP=00'
                    dlna_tags[2] = 'DLNA.ORG_CI=1'
                    new_res = Resource(
                        self.url + '?transcoded=lpcm',
                        f'http-get:*:{"audio/L16;rate=44100;channels=2"}:'
                        f'{";".join([dlna_pn] + dlna_tags)}',
                    )
                    new_res.size = None
                    # self.item.res.append(new_res)

                    if self.mimetype != 'audio/mpeg':
                        new_res = Resource(
                            self.url + '?transcoded=mp3',
                            f'http-get:*:{"audio/mpeg"}:*',
                        )
                        new_res.size = None
                        # self.item.res.append(new_res)
            ''' if this item is an image and we want to add a thumbnail for it
                we have to follow these rules:

                create a new Resource object, at least a 'http-get'
                and maybe an 'internal' one too

                for an JPG this looks like that

                res = Resource(url_for_thumbnail,
                        'http-get:*:image/jpg:%s'% ';'.join(
                        ['DLNA.ORG_PN=JPEG_TN']+simple_dlna_tags))
                res.size = size_of_thumbnail
                self.item.res.append(res)

                and for a PNG the Resource creation is like that

                res = Resource(url_for_thumbnail,
                        'http-get:*:image/png:%s'% ';'.join(
                        simple_dlna_tags+['DLNA.ORG_PN=PNG_TN']))

                if not hasattr(self.item, 'attachments'):
                    self.item.attachments = {}
                self.item.attachments[key] = utils.StaticFile(
                filename_of_thumbnail)
            '''

            if (self.mimetype in ('image/jpeg', 'image/png')
                    or self.mimetype.startswith('video/')):
                try:
                    filename, mimetype, dlna_pn = _find_thumbnail(
                        self.get_path())
                except NoThumbnailFound:
                    pass
                except Exception:
                    self.warning(traceback.format_exc())
                else:
                    dlna_tags = simple_dlna_tags[:]
                    dlna_tags[
                        3] = 'DLNA.ORG_FLAGS=00f00000000000000000000000000000'

                    hash_from_path = str(id(filename))
                    new_res = Resource(
                        self.url + '?attachment=' + hash_from_path,
                        f'http-get:*:{mimetype}:'
                        f'{";".join([dlna_pn] + dlna_tags)}',
                    )
                    new_res.size = os.path.getsize(filename)
                    self.item.res.append(new_res)
                    if not hasattr(self.item, 'attachments'):
                        self.item.attachments = {}
                    self.item.attachments[hash_from_path] = utils.StaticFile(
                        filename)

            if self.mimetype.startswith('video/'):
                # check for a subtitles file
                caption, _ = os.path.splitext(self.get_path())
                caption = caption + '.srt'
                if os.path.exists(caption):
                    hash_from_path = str(id(caption))
                    mimetype = 'smi/caption'
                    new_res = Resource(
                        self.url + '?attachment=' + hash_from_path,
                        f'http-get:*:{mimetype}:{"*"}',
                    )
                    new_res.size = os.path.getsize(caption)
                    self.caption = new_res.data
                    self.item.res.append(new_res)
                    if not hasattr(self.item, 'attachments'):
                        self.item.attachments = {}
                    self.item.attachments[hash_from_path] = utils.StaticFile(
                        caption,
                        defaultType=mimetype,
                    )

            try:
                # FIXME: getmtime is deprecated in Twisted 2.6
                self.item.date = datetime.fromtimestamp(
                    self.location.getmtime())
            except Exception:
                self.item.date = None