Skip to content

Commit d34542e

Browse files
committed
Make RSS enclosure type and length optional
1 parent 4425bcc commit d34542e

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

atoma/rss.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class RSSImage:
2323
@attr.s
2424
class RSSEnclosure:
2525
url: str = attr.ib()
26-
length: int = attr.ib()
27-
type: str = attr.ib()
26+
length: Optional[int] = attr.ib()
27+
type: Optional[str] = attr.ib()
2828

2929

3030
@attr.s
@@ -102,10 +102,14 @@ def _get_source(element: Element, name,
102102

103103

104104
def _get_enclosure(element: Element) -> RSSEnclosure:
105+
length = element.attrib.get('length')
106+
if length is not None:
107+
length = int(length)
108+
105109
return RSSEnclosure(
106110
element.attrib['url'],
107-
int(element.attrib['length']),
108-
element.attrib['type'],
111+
length,
112+
element.attrib.get('type'),
109113
)
110114

111115

tests/rss/broken-enclosure.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<rss version="2.0">
3+
<channel>
4+
<title>Foo</title>
5+
<link>http://foo.bar</link>
6+
<description>Foo bar.</description>
7+
<item>
8+
<title>Baz item</title>
9+
<enclosure url="https://foo.com/test.mp3" />
10+
</item>
11+
</channel>
12+
</rss>

tests/rss/test_rss.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ def test_broken_missing_source_url():
5858
assert p.items[0].source.url is None
5959

6060

61+
def test_broken_enclosure():
62+
# The length and type of an enclosure are mandatory by specs,
63+
# but some feeds in the wild do not provide them
64+
p = parse_rss_file('tests/rss/broken-enclosure.xml')
65+
assert p.items[0].enclosures[0].url == 'https://foo.com/test.mp3'
66+
assert p.items[0].enclosures[0].length is None
67+
assert p.items[0].enclosures[0].type is None
68+
69+
6170
def test_broken_version():
6271
with pytest.raises(FeedParseError):
6372
parse_rss_file('tests/rss/broken-version.xml')

0 commit comments

Comments
 (0)