wallabag_atom.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. __package__ = 'archivebox.parsers'
  2. from typing import IO, Iterable
  3. from datetime import datetime
  4. from ..index.schema import Link
  5. from archivebox.misc.util import (
  6. htmldecode,
  7. enforce_types,
  8. str_between,
  9. )
  10. @enforce_types
  11. def parse_wallabag_atom_export(rss_file: IO[str], **_kwargs) -> Iterable[Link]:
  12. """Parse Wallabag Atom files into links"""
  13. rss_file.seek(0)
  14. entries = rss_file.read().split('<entry>')[1:]
  15. for entry in entries:
  16. # example entry:
  17. # <entry>
  18. # <title><![CDATA[Orient Ray vs Mako: Is There Much Difference? - iknowwatches.com]]></title>
  19. # <link rel="alternate" type="text/html"
  20. # href="http://wallabag.drycat.fr/view/14041"/>
  21. # <link rel="via">https://iknowwatches.com/orient-ray-vs-mako/</link>
  22. # <id>wallabag:wallabag.drycat.fr:milosh:entry:14041</id>
  23. # <updated>2020-10-18T09:14:02+02:00</updated>
  24. # <published>2020-10-18T09:13:56+02:00</published>
  25. # <category term="montres" label="montres" />
  26. # <content type="html" xml:lang="en">
  27. # </entry>
  28. trailing_removed = entry.split('</entry>', 1)[0]
  29. leading_removed = trailing_removed.strip()
  30. splits_fixed = leading_removed.replace('"\n href="', '" href="')
  31. rows = splits_fixed.split('\n')
  32. def get_row(prefix):
  33. return [
  34. row.strip()
  35. for row in rows
  36. if row.strip().startswith('<{}'.format(prefix))
  37. ][0]
  38. title = str_between(get_row('title'), '<title><![CDATA[', ']]></title>').strip()
  39. url_inside_link = str_between(get_row('link rel="via"'), '<link rel="via">', '</link>')
  40. url_inside_attr = str_between(get_row('link rel="via"'), 'href="', '"/>')
  41. ts_str = str_between(get_row('published'), '<published>', '</published>')
  42. time = datetime.strptime(ts_str, "%Y-%m-%dT%H:%M:%S%z")
  43. try:
  44. tags = str_between(get_row('category'), 'label="', '" />')
  45. except Exception:
  46. tags = None
  47. yield Link(
  48. url=htmldecode(url_inside_attr or url_inside_link),
  49. timestamp=str(time.timestamp()),
  50. title=htmldecode(title) or None,
  51. tags=tags or '',
  52. sources=[rss_file.name],
  53. )
  54. KEY = 'wallabag_atom'
  55. NAME = 'Wallabag Atom'
  56. PARSER = parse_wallabag_atom_export