Skip to content

Commit

Permalink
Improve handling of XML parsing and device register
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmagno committed May 12, 2022
1 parent a3ea124 commit e8c80d3
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions nanodlna/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@
"MAN: \"ssdp:discover\"", "MX: 10", "ST: ssdp:all", "", ""]
SSDP_BROADCAST_MSG = "\r\n".join(SSDP_BROADCAST_PARAMS)

UPNP_DEFAULT_SERVICE_TYPE = "urn:schemas-upnp-org:service:AVTransport:1"
DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaRenderer:1"
UPNP_DEVICE_TYPE = "urn:schemas-upnp-org:device:MediaRenderer:1"
UPNP_SERVICE_TYPE = "urn:schemas-upnp-org:service:AVTransport:1"


def register_device(location_url):
def get_xml_field_text(xml_root, query):
result = None
if xml_root:
node = xml_root.find(query)
result = node.text if node is not None else None
return result

xml = urllibreq.urlopen(location_url).read().decode("UTF-8")
xml = re.sub(" xmlns=\"[^\"]+\"", "", xml, count=1)
info = ET.fromstring(xml)

def register_device(location_url):

Expand All @@ -58,38 +60,32 @@ def register_device(location_url):
device_root = info.find(
"./device/deviceList/device/"
"[deviceType='{0}']".format(
DEVICE_TYPE
UPNP_DEVICE_TYPE
)
)

try:
friendly_name = device_root.find("./friendlyName").text
except:
friendly_name = None

try:
manufacturer_name = device_root.find("./manufacturer").text
except:
manufacturer_name = None

try:
path = info.find(
"./serviceList/service/"
"[serviceType='{0}']/controlURL".format(
UPNP_DEFAULT_SERVICE_TYPE
)
).text
action_url = urllibparse.urljoin(location_url, path)
except:
friendly_name = get_xml_field_text(device_root, "./friendlyName")
manufacturer = get_xml_field_text(device_root, "./manufacturer")
action_url_path = get_xml_field_text(
device_root,
"./serviceList/service/"
"[serviceType='{0}']/controlURL".format(
UPNP_SERVICE_TYPE
)
)

if action_url_path is not None:
action_url = urllibparse.urljoin(location_url, action_url_path)
else:
action_url = None

device = {
"location": location_url,
"hostname": hostname,
"manufacturer_name": manufacturer_name,
"manufacturer": manufacturer,
"friendly_name": friendly_name,
"action_url": action_url,
"st": UPNP_DEFAULT_SERVICE_TYPE
"st": UPNP_SERVICE_TYPE
}

logging.debug(
Expand Down

0 comments on commit e8c80d3

Please sign in to comment.