2019-04-27 21:26:24 +00:00
|
|
|
__package__ = 'archivebox.parsers'
|
|
|
|
|
|
|
|
|
|
|
|
from typing import IO, Iterable
|
2021-04-10 08:19:30 +00:00
|
|
|
from datetime import datetime, timezone
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
|
|
|
|
from ..index.schema import Link
|
|
|
|
from ..util import (
|
|
|
|
htmldecode,
|
|
|
|
enforce_types,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
2020-08-18 12:27:47 +00:00
|
|
|
def parse_pinboard_rss_export(rss_file: IO[str], **_kwargs) -> Iterable[Link]:
|
2019-04-27 21:26:24 +00:00
|
|
|
"""Parse Pinboard RSS feed files into links"""
|
|
|
|
|
|
|
|
rss_file.seek(0)
|
|
|
|
root = ElementTree.parse(rss_file).getroot()
|
|
|
|
items = root.findall("{http://purl.org/rss/1.0/}item")
|
|
|
|
for item in items:
|
2021-08-04 13:26:51 +00:00
|
|
|
find = lambda p: item.find(p).text.strip() if item.find(p) is not None else None # type: ignore
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
url = find("{http://purl.org/rss/1.0/}link")
|
|
|
|
tags = find("{http://purl.org/dc/elements/1.1/}subject")
|
|
|
|
title = find("{http://purl.org/rss/1.0/}title")
|
|
|
|
ts_str = find("{http://purl.org/dc/elements/1.1/}date")
|
|
|
|
|
2021-08-04 13:26:51 +00:00
|
|
|
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
|
|
|
# Pinboard includes a colon in its date stamp timezone offsets, which
|
|
|
|
# Python can't parse. Remove it:
|
|
|
|
if ts_str and ts_str[-3:-2] == ":":
|
|
|
|
ts_str = ts_str[:-3]+ts_str[-2:]
|
|
|
|
|
|
|
|
if ts_str:
|
|
|
|
time = datetime.strptime(ts_str, "%Y-%m-%dT%H:%M:%S%z")
|
|
|
|
else:
|
2021-04-10 08:19:30 +00:00
|
|
|
time = datetime.now(timezone.utc)
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
yield Link(
|
|
|
|
url=htmldecode(url),
|
|
|
|
timestamp=str(time.timestamp()),
|
|
|
|
title=htmldecode(title) or None,
|
|
|
|
tags=htmldecode(tags) or None,
|
|
|
|
sources=[rss_file.name],
|
|
|
|
)
|
2021-03-31 05:05:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
KEY = 'pinboard_rss'
|
|
|
|
NAME = 'Pinboard RSS'
|
|
|
|
PARSER = parse_pinboard_rss_export
|