Fix handling JSON feeds with extra stuff in content-type header

This commit is contained in:
Tulir Asokan 2020-08-03 03:11:17 +03:00
parent 44927f2cf5
commit ad39e34ae2

View File

@ -144,7 +144,9 @@ class RSSBot(Plugin):
elif url is not None:
raise ValueError("Only one of feed or url must be set")
resp = await self.http.get(feed.url)
if resp.headers["Content-Type"] == "application/json":
# No need to check for vendor prefixes and such, the JSON feed spec requires
# the mime type to be application/json
if resp.headers["Content-Type"].startswith("application/json"):
return await self._parse_json(feed, resp)
else:
return await self._parse_rss(feed, resp)
@ -153,6 +155,8 @@ class RSSBot(Plugin):
async def _parse_json(cls, feed: Feed, resp: aiohttp.ClientResponse
) -> Tuple[Feed, Iterable[Entry]]:
content = await resp.json()
if content["version"] != "https://jsonfeed.org/version/1":
raise ValueError("Unsupported JSON feed version")
if not isinstance(content["items"], list):
raise ValueError("Feed is not a valid JSON feed (items is not a list)")
feed = Feed(id=feed.id, title=content["title"], subtitle=content.get("subtitle", ""),