2020-10-18 09:20:07 +00:00
|
|
|
__package__ = 'archivebox.parsers'
|
|
|
|
|
|
|
|
|
|
|
|
from typing import IO, Iterable
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from ..index.schema import Link
|
2024-10-01 00:25:15 +00:00
|
|
|
from archivebox.misc.util import (
|
2020-10-18 09:20:07 +00:00
|
|
|
htmldecode,
|
|
|
|
enforce_types,
|
|
|
|
str_between,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
|
|
|
def parse_wallabag_atom_export(rss_file: IO[str], **_kwargs) -> Iterable[Link]:
|
|
|
|
"""Parse Wallabag Atom files into links"""
|
|
|
|
|
|
|
|
rss_file.seek(0)
|
|
|
|
entries = rss_file.read().split('<entry>')[1:]
|
|
|
|
for entry in entries:
|
|
|
|
# example entry:
|
|
|
|
# <entry>
|
|
|
|
# <title><![CDATA[Orient Ray vs Mako: Is There Much Difference? - iknowwatches.com]]></title>
|
|
|
|
# <link rel="alternate" type="text/html"
|
|
|
|
# href="http://wallabag.drycat.fr/view/14041"/>
|
|
|
|
# <link rel="via">https://iknowwatches.com/orient-ray-vs-mako/</link>
|
|
|
|
# <id>wallabag:wallabag.drycat.fr:milosh:entry:14041</id>
|
|
|
|
# <updated>2020-10-18T09:14:02+02:00</updated>
|
|
|
|
# <published>2020-10-18T09:13:56+02:00</published>
|
|
|
|
# <category term="montres" label="montres" />
|
|
|
|
# <content type="html" xml:lang="en">
|
|
|
|
# </entry>
|
|
|
|
|
|
|
|
trailing_removed = entry.split('</entry>', 1)[0]
|
|
|
|
leading_removed = trailing_removed.strip()
|
2022-05-10 02:07:42 +00:00
|
|
|
splits_fixed = leading_removed.replace('"\n href="', '" href="')
|
|
|
|
rows = splits_fixed.split('\n')
|
2020-10-18 09:20:07 +00:00
|
|
|
|
2022-05-10 02:07:42 +00:00
|
|
|
def get_row(prefix):
|
|
|
|
return [
|
|
|
|
row.strip()
|
|
|
|
for row in rows
|
|
|
|
if row.strip().startswith('<{}'.format(prefix))
|
|
|
|
][0]
|
2020-10-18 09:20:07 +00:00
|
|
|
|
|
|
|
title = str_between(get_row('title'), '<title><![CDATA[', ']]></title>').strip()
|
2022-05-10 02:07:42 +00:00
|
|
|
url_inside_link = str_between(get_row('link rel="via"'), '<link rel="via">', '</link>')
|
|
|
|
url_inside_attr = str_between(get_row('link rel="via"'), 'href="', '"/>')
|
2020-10-18 09:20:07 +00:00
|
|
|
ts_str = str_between(get_row('published'), '<published>', '</published>')
|
|
|
|
time = datetime.strptime(ts_str, "%Y-%m-%dT%H:%M:%S%z")
|
|
|
|
try:
|
|
|
|
tags = str_between(get_row('category'), 'label="', '" />')
|
2021-01-30 11:07:35 +00:00
|
|
|
except Exception:
|
2020-10-18 09:20:07 +00:00
|
|
|
tags = None
|
|
|
|
|
|
|
|
yield Link(
|
2022-05-10 02:07:42 +00:00
|
|
|
url=htmldecode(url_inside_attr or url_inside_link),
|
2020-10-18 09:20:07 +00:00
|
|
|
timestamp=str(time.timestamp()),
|
|
|
|
title=htmldecode(title) or None,
|
|
|
|
tags=tags or '',
|
|
|
|
sources=[rss_file.name],
|
|
|
|
)
|
2021-03-31 05:05:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
KEY = 'wallabag_atom'
|
|
|
|
NAME = 'Wallabag Atom'
|
|
|
|
PARSER = parse_wallabag_atom_export
|