def parse_device(self, d): self.info(f'parse_device {d}') self.device_type = d.findtext(f'./{{{ns}}}deviceType') ( self.friendly_device_type, self.device_type_version, ) = self.device_type.split(':')[-2:] self.friendly_name = d.findtext(f'./{{{ns}}}friendlyName') self.udn = d.findtext(f'./{{{ns}}}UDN') self.info(f'found udn {self.udn} {self.friendly_name}') try: self.manufacturer = d.findtext(f'./{{{ns}}}manufacturer') except Exception: pass try: self.manufacturer_url = d.findtext(f'./{{{ns}}}manufacturerURL') except Exception: pass try: self.model_name = d.findtext(f'./{{{ns}}}modelName') except Exception: pass try: self.model_description = d.findtext(f'./{{{ns}}}modelDescription') except Exception: pass try: self.model_number = d.findtext(f'./{{{ns}}}modelNumber') except Exception: pass try: self.model_url = d.findtext(f'./{{{ns}}}modelURL') except Exception: pass try: self.serial_number = d.findtext(f'./{{{ns}}}serialNumber') except Exception: pass try: self.upc = d.findtext(f'./{{{ns}}}UPC') except Exception: pass try: self.presentation_url = d.findtext(f'./{{{ns}}}presentationURL') except Exception: pass try: for dlna_doc in d.findall( './{urn:schemas-dlna-org:device-1-0}X_DLNADOC' ): try: self.dlna_dc.append(dlna_doc.text) except AttributeError: self.dlna_dc = [] self.dlna_dc.append(dlna_doc.text) except Exception: pass try: for dlna_cap in d.findall( './{urn:schemas-dlna-org:device-1-0}X_DLNACAP' ): for cap in dlna_cap.text.split(','): try: self.dlna_cap.append(cap) except AttributeError: self.dlna_cap = [] self.dlna_cap.append(cap) except Exception: pass icon_list = d.find(f'./{{{ns}}}iconList') if icon_list is not None: from urllib.parse import urlparse url_base = '%s://%s' % urlparse(self.get_location())[:2] for icon in icon_list.findall(f'./{{{ns}}}icon'): try: i = {} i['mimetype'] = icon.find(f'./{{{ns}}}mimetype').text i['width'] = icon.find(f'./{{{ns}}}width').text i['height'] = icon.find(f'./{{{ns}}}height').text i['depth'] = icon.find(f'./{{{ns}}}depth').text i['realurl'] = icon.find(f'./{{{ns}}}url').text i['url'] = self.make_fullyqualified(i['realurl']).decode( 'utf-8' ) self.icons.append(i) self.debug(f'adding icon {i} for {self.friendly_name}') except Exception as e: import traceback self.debug(traceback.format_exc()) self.warning( f'device {self.friendly_name} seems to have an invalid' + f' icon description, ignoring that icon [error: {e}]' ) serviceList = d.find(f'./{{{ns}}}serviceList') if serviceList is not None: for service in serviceList.findall(f'./{{{ns}}}service'): serviceType = service.findtext(f'{{{ns}}}serviceType') serviceId = service.findtext(f'{{{ns}}}serviceId') controlUrl = service.findtext(f'{{{ns}}}controlURL') eventSubUrl = service.findtext(f'{{{ns}}}eventSubURL') presentationUrl = service.findtext(f'{{{ns}}}presentationURL') scpdUrl = service.findtext(f'{{{ns}}}SCPDURL') # check if values are somehow reasonable if len(scpdUrl) == 0: self.warning('service has no uri for its description') continue if len(eventSubUrl) == 0: self.warning('service has no uri for eventing') continue if len(controlUrl) == 0: self.warning('service has no uri for controling') continue try: self.add_service( Service( serviceType, serviceId, self.get_location(), controlUrl, eventSubUrl, presentationUrl, scpdUrl, self, ) ) except Exception as e: self.error( f'Error on adding service: {service} [ERROR: {e}]' ) # now look for all sub devices embedded_devices = d.find(f'./{{{ns}}}deviceList') if embedded_devices is not None: for d in embedded_devices.findall(f'./{{{ns}}}device'): embedded_device = Device(self) self.add_device(embedded_device) embedded_device.parse_device(d) self.receiver()
def parse_device(self, d): self.info("parse_device %r", d) self.device_type = str(d.findtext('./{%s}deviceType' % ns)) self.friendly_device_type, self.device_type_version = \ self.device_type.split(':')[-2:] self.friendly_name = str(d.findtext('./{%s}friendlyName' % ns)) self.udn = d.findtext('./{%s}UDN' % ns) self.info("found udn %r %r", self.udn, self.friendly_name) try: self.manufacturer = d.findtext('./{%s}manufacturer' % ns) except: pass try: self.manufacturer_url = d.findtext('./{%s}manufacturerURL' % ns) except: pass try: self.model_name = d.findtext('./{%s}modelName' % ns) except: pass try: self.model_description = d.findtext('./{%s}modelDescription' % ns) except: pass try: self.model_number = d.findtext('./{%s}modelNumber' % ns) except: pass try: self.model_url = d.findtext('./{%s}modelURL' % ns) except: pass try: self.serial_number = d.findtext('./{%s}serialNumber' % ns) except: pass try: self.upc = d.findtext('./{%s}UPC' % ns) except: pass try: self.presentation_url = d.findtext('./{%s}presentationURL' % ns) except: pass try: for dlna_doc in d.findall('./{urn:schemas-dlna-org:device-1-0}X_DLNADOC'): try: self.dlna_dc.append(dlna_doc.text) except AttributeError: self.dlna_dc = [] self.dlna_dc.append(dlna_doc.text) except: pass try: for dlna_cap in d.findall('./{urn:schemas-dlna-org:device-1-0}X_DLNACAP'): for cap in dlna_cap.text.split(','): try: self.dlna_cap.append(cap) except AttributeError: self.dlna_cap = [] self.dlna_cap.append(cap) except: pass icon_list = d.find('./{%s}iconList' % ns) if icon_list is not None: import urllib.request, urllib.error, urllib.parse url_base = "%s://%s" % urllib2.urlparse.urlparse(self.get_location())[:2] for icon in icon_list.findall('./{%s}icon' % ns): try: i = {} i['mimetype'] = icon.find('./{%s}mimetype' % ns).text i['width'] = icon.find('./{%s}width' % ns).text i['height'] = icon.find('./{%s}height' % ns).text i['depth'] = icon.find('./{%s}depth' % ns).text i['realurl'] = icon.find('./{%s}url' % ns).text i['url'] = self.make_fullyqualified(i['realurl']) self.icons.append(i) self.debug("adding icon %r for %r", i, self.friendly_name) except: import traceback self.debug(traceback.format_exc()) self.warning("device %r seems to have an invalid icon description, ignoring that icon", self.friendly_name) serviceList = d.find('./{%s}serviceList' % ns) if serviceList is not None: for service in serviceList.findall('./{%s}service' % ns): serviceType = service.findtext('{%s}serviceType' % ns) serviceId = service.findtext('{%s}serviceId' % ns) controlUrl = service.findtext('{%s}controlURL' % ns) eventSubUrl = service.findtext('{%s}eventSubURL' % ns) presentationUrl = service.findtext('{%s}presentationURL' % ns) scpdUrl = service.findtext('{%s}SCPDURL' % ns) """ check if values are somehow reasonable """ if len(scpdUrl) == 0: self.warning("service has no uri for its description") continue if len(eventSubUrl) == 0: self.warning("service has no uri for eventing") continue if len(controlUrl) == 0: self.warning("service has no uri for controling") continue self.add_service(Service(serviceType, serviceId, self.get_location(), controlUrl, eventSubUrl, presentationUrl, scpdUrl, self)) # now look for all sub devices embedded_devices = d.find('./{%s}deviceList' % ns) if embedded_devices is not None: for d in embedded_devices.findall('./{%s}device' % ns): embedded_device = Device(self) self.add_device(embedded_device) embedded_device.parse_device(d) self.receiver()