Skip to content

Commit

Permalink
Cater for malformed XML from WiiM pro in non-strict mode (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
pp81381 authored Nov 3, 2024
1 parent df88db6 commit f746e64
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion didl_lite/didl_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,13 @@ def from_xml_el(
# construct item
upnp_class = child_el.find("./upnp:class", NAMESPACES)
if upnp_class is None or not upnp_class.text:
continue
if strict:
continue
# WiiM Pro and possibly other Linkplay devices emit
# upnp_class above the item element instead of inside it
upnp_class = xml_el.find("./upnp:class", NAMESPACES)
if upnp_class is None or not upnp_class.text:
continue
didl_object_type = type_by_upnp_class(upnp_class.text, strict)
if didl_object_type is None:
if strict:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_didl_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,29 @@ def test_property_case(self) -> None:
assert hasattr(item, "otherItem")
assert not hasattr(item, "other_item")
assert item.otherItem == "otherItem"

def test_item_improper_class_nesting(self) -> None:
"""
Test item from XML that has upnp_class element above item.
Cater for WiiM Pro and possibly other Linkplay devices that
emit upnp_class above the item element instead of inside it
"""
didl_string = """
<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/"
xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">
<upnp:class>object.item.audioItem.musicTrack</upnp:class>
<item id="0">
<res protocolInfo="protocol_info"></res>
<dc:title>Music Track Title</dc:title>
<dc:creator>Music Track Creator</dc:creator>
<upnp:artist>Artist</upnp:artist>
<upnp:album>Album</upnp:album>
</item>
</DIDL-Lite>"""
items = didl_lite.from_xml_string(didl_string, strict=False)
assert len(items) == 1

item = items[0]
assert isinstance(item, didl_lite.MusicTrack)

0 comments on commit f746e64

Please sign in to comment.