def populate(files): if len(files) <= 0: raise ValueError("Invalid list") manager = OneServerManager() tcsRoot = Entry("/tcs", manager.CONTAINER_MIME, None, [], "TCS", "tcs", -1, None) idlna = DLNAInterface() dlna = manager.dlna for f in files: profile = idlna.dlna_guess_media_profile(dlna, f) manager.log.debug('Profile for %s: %s', f, str(profile)) if profile is None: raise ValueError("Invalid media type on {0}".format(f)) try: profile.contents except ValueError: OneServerManager().log.debug("Invalid profile object, skipping "+f) break size = os.path.getsize(f) child = Entry(f, profile, tcsRoot, None, f, "", size, createLocalFileHandle) tcsRoot.children.append(child) return tcsRoot
def matchesSearch(searchCriteria, entry): dlna = DLNAInterface() protocol = dlna.dlna_write_protocol_info( dlna.dlna_protocol_info_type_t['DLNA_PROTOCOL_INFO_TYPE_HTTP'], dlna.dlna_org_play_speed_t['DLNA_ORG_PLAY_SPEED_NORMAL'], dlna.dlna_org_conversion_t['DLNA_ORG_CONVERSION_NONE'], dlna.dlna_org_operation_t['DLNA_ORG_OPERATION_RANGE'], dlna.flags, entry.dlnaProfile) keyword = CDSService.SEARCH_OBJECT_KEYWORD #Defaults derivedFrom = False protocolContains = False result = False if searchCriteria is CDSService.SEARCH_CLASS_MATCH_KEYWORD: keyword = CDSService.SEARCH_CLASS_MATCH_KEYWORD elif searchCriteria is CDSService.SEARCH_CLASS_DERIVED_KEYWORD: derivedFrom = True keyword = CDSService.SEARCH_CLASS_DERIVED_KEYWORD elif searchCriteria is CDSService.SEARCH_PROTOCOL_CONTAINS_KEYWORD: protocolContains = True keyword = CDSService.SEARCH_PROTOCOL_CONTAINS_KEYWORD if (protocolContains) and (protocol.find(keyword) >= 0): result = True andClause = searchCriteria.find(CDSService.SEARCH_AND) if andClause >= 0: result = result and CDSService.matchesSearch( searchCriteria[andClause], entry) return result
def cdsSearchDirectChildrenRecursive(buf, count, entry, _filter, searchCriteria): if entry.children is None: return -1 resultCount = 0 for child in entry.children: if (count is 0) or (resultCount < count): if child.children is not None: #Container newCount, buf = CDSService.cdsSearchDirectChildrenRecursive( buf, 0 if count is 0 else count - resultCount, child, _filter, searchCriteria) resultCount += newCount else: #File if CDSService.matchesSearch(searchCriteria, child): dlna = DLNAInterface() protocol = dlna.dlna_write_protocol_info( dlna.dlna_protocol_info_type_t[ 'DLNA_PROTOCOL_INFO_TYPE_HTTP'], dlna.dlna_org_play_speed_t[ 'DLNA_ORG_PLAY_SPEED_NORMAL'], dlna. dlna_org_conversion_t['DLNA_ORG_CONVERSION_NONE'], dlna. dlna_org_operation_t['DLNA_ORG_OPERATION_RANGE'], dlna.flags, entry.dlnaProfile) buf = CDSService.didlAddItem( buf, child._id, child.parent._id if child.parent is not None else -1, "true", dlna.dlna_profile_upnp_object_item( child.dlnaProfile), child.title, protocol, child.size, child.url, _filter) resultCount += 1 return resultCount, buf
def __init__(self, config): if config is None: raise ValueError("No ConfigManager was given!") self.config = config self.upnp = UPnP() self.idlna = DLNAInterface() self.dlnaDescription = None self.dlna = self.idlna.dlna_init() self.idlna.dlna_set_verbosity(self.dlna, 1) self.idlna.dlna_set_extension_check(self.dlna, 1) self.idlna.dlna_register_all_media_profiles(self.dlna) self.rootDev = UpnpDevice_Handle() self.manager = OneServerManager() self.manager.idlna = self.idlna self.manager.dlna = self.dlna self.manager.webRoot = '/web/' self.manager.hostIp = config.getCoreConfigWithDefault('ip', None) self.manager.port = config.getCoreConfigWithDefault('port', 0) self.manager.log.debug("hostIp=%s, port=%s" % (self.manager.hostIp, self.manager.port)) self.vdc = VirtualDirCallbackHandler(self.manager) self.virtualDirCallback = UpnpVirtualDirCallbacks() self.virtualDirCallback.get_info = get_info_prototype( self.vdc.httpGetInfo) self.virtualDirCallback.open = open_prototype(self.vdc.httpOpen) self.virtualDirCallback.read = read_prototype(self.vdc.httpRead) self.virtualDirCallback.write = write_prototype(self.vdc.httpWrite) self.virtualDirCallback.seek = seek_prototype(self.vdc.httpSeek) self.virtualDirCallback.close = close_prototype(self.vdc.httpClose) cds = CDSService() DLNAService.services[0] = { "id_t": cds.id_t, "type_t": cds.type_t, "actions": cds.actions } cms = CMSService() DLNAService.services[1] = { "id_t": cms.id_t, "type_t": cms.type_t, "actions": cms.actions } msr = MSRService() DLNAService.services[2] = { "id_t": msr.id_t, "type_t": msr.type_t, "actions": msr.actions }
def cdsBrowseDirectChildren(event, buf, index, count, entry, _filter): resultCount = 0 if entry.children is None: #Its a file return -1, buf buf = CDSService.didlAddHeader(buf) #If index = 0 and count = 0 then all children must be returned if (index is 0) and (count is 0): count = len(entry.children) for child in entry.children: if (count is 0) or (resultCount < count): if child.children is not None: #Container buf = CDSService.didlAddContainer( buf, child._id, child.parent._id if child.parent is not None else -1, len(child.children), "true", None, child.title, CDSService.CONTAINER_MIME) else: #Item manager = OneServerManager() manager.log.debug("child=%s, child.children=%s" % (child, child.children)) manager.log.debug("child.fullpath=%s" % child.fullPath) manager.log.debug("child.dlnaProfile=%s" % child.dlnaProfile.contents.mime) dlna = DLNAInterface() protocol = dlna.dlna_write_protocol_info( dlna.dlna_protocol_info_type_t[ 'DLNA_PROTOCOL_INFO_TYPE_HTTP'], dlna. dlna_org_play_speed_t['DLNA_ORG_PLAY_SPEED_NORMAL'], dlna.dlna_org_conversion_t['DLNA_ORG_CONVERSION_NONE'], dlna.dlna_org_operation_t['DLNA_ORG_OPERATION_RANGE'], dlna.flags, child.dlnaProfile) buf = CDSService.didlAddItem( buf, child._id, child.parent._id if child.parent is not None else -1, "true", dlna.dlna_profile_upnp_object_item(child.dlnaProfile), child.title, protocol, child.size, child.url, _filter) resultCount = resultCount + 1 buf = CDSService.didlAddFooter(buf) Service.upnpAddResponse(event, CDSService.SERVICE_CDS_DIDL_RESULT, buf) Service.upnpAddResponse(event, CDSService.SERVICE_CDS_DIDL_NUM_RETURNED, str(resultCount)) Service.upnpAddResponse(event, CDSService.SERVICE_CDS_DIDL_TOTAL_MATCH, str(len(entry.children))) return resultCount, buf
def cdsBrowseMetadata(event, buf, index, count, entry, _filter): if entry is None: return 0, buf resultCount = 0 if entry.children is None: # File protocol = "" dlna = DLNAInterface() protocol = dlna.dlna_write_protocol_info( dlna.dlna_protocol_info_type_t['DLNA_PROTOCOL_INFO_TYPE_HTTP'], dlna.dlna_org_play_speed_t['DLNA_ORG_PLAY_SPEED_NORMAL'], dlna.dlna_org_conversion_t['DLNA_ORG_CONVERSION_NONE'], dlna.dlna_org_operation_t['DLNA_ORG_OPERATION_RANGE'], dlna.flags, entry.dlnaProfile) buf = CDSService.didlAddHeader(buf) buf = CDSService.didlAddItem( buf, entry._id, entry.parent._id if entry.parent is not None else -1, "false", dlna.dlna_profile_upnp_object_item(entry.dlnaProfile), entry.title, protocol, entry.size, entry.url, _filter) buf = CDSService.didlAddFooter(buf) for a in range(index, min(index + count, -1)): resultCount += 1 else: #Directory buf = CDSService.didlAddHeader(buf) buf = CDSService.didlAddContainer( buf, entry._id, entry.parent._id if entry.parent is not None else -1, len(entry.children), "true", "true", entry.title, CDSService.CONTAINER_MIME) buf = CDSService.didlAddFooter(buf) resultCount = 1 Service.upnpAddResponse(event, CDSService.SERVICE_CDS_DIDL_RESULT, buf) Service.upnpAddResponse(event, CDSService.SERVICE_CDS_DIDL_NUM_RETURNED, "1") Service.upnpAddResponse(event, CDSService.SERVICE_CDS_DIDL_TOTAL_MATCH, "1") return resultCount, buf
def isValidExtension(filename): if not filename: return False return DLNAInterface().dlna_guess_media_profile(OneServerManager().dlna, filename)