ArchiveBox/archivebox/parsers/generic_rss.py

47 lines
1.1 KiB
Python
Raw Normal View History

2019-04-27 21:26:24 +00:00
__package__ = 'archivebox.parsers'
from typing import IO, Iterable
from time import mktime
from feedparser import parse as feedparser
2019-04-27 21:26:24 +00:00
from ..index.schema import Link
from ..util import (
htmldecode,
enforce_types
2019-04-27 21:26:24 +00:00
)
@enforce_types
def parse_generic_rss_export(rss_file: IO[str], **_kwargs) -> Iterable[Link]:
2019-04-27 21:26:24 +00:00
"""Parse RSS XML-format files into links"""
rss_file.seek(0)
feed = feedparser(rss_file.read())
for item in feed.entries:
url = item.link
title = item.title
time = mktime(item.updated_parsed)
try:
tags = ','.join(map(lambda tag: tag.term, item.tags))
except AttributeError:
tags = ''
if url is None:
# Yielding a Link with no URL will
# crash on a URL validation assertion
continue
2019-04-27 21:26:24 +00:00
yield Link(
url=htmldecode(url),
timestamp=str(time),
2019-04-27 21:26:24 +00:00
title=htmldecode(title) or None,
tags=tags,
2019-04-27 21:26:24 +00:00
sources=[rss_file.name],
)
KEY = 'rss'
NAME = 'Generic RSS'
PARSER = parse_generic_rss_export