2019-04-27 17:26:24 -04:00
|
|
|
__package__ = 'archivebox.parsers'
|
|
|
|
__description__ = 'Plain Text'
|
|
|
|
|
|
|
|
from typing import IO, Iterable
|
2021-04-10 04:19:30 -04:00
|
|
|
from datetime import datetime, timezone
|
2019-04-27 17:26:24 -04:00
|
|
|
|
|
|
|
from ..index.schema import Link
|
2024-09-30 17:25:15 -07:00
|
|
|
from archivebox.misc.util import (
|
2019-04-27 17:26:24 -04:00
|
|
|
htmldecode,
|
|
|
|
enforce_types,
|
2024-04-24 17:45:45 -07:00
|
|
|
find_all_urls,
|
2019-04-27 17:26:24 -04:00
|
|
|
)
|
|
|
|
|
2020-07-13 11:22:58 -04:00
|
|
|
|
2019-04-27 17:26:24 -04:00
|
|
|
@enforce_types
|
2020-08-18 08:27:47 -04:00
|
|
|
def parse_generic_txt_export(text_file: IO[str], **_kwargs) -> Iterable[Link]:
|
2021-03-20 17:38:00 +01:00
|
|
|
"""Parse links from a text file, ignoring other text"""
|
2019-04-27 17:26:24 -04:00
|
|
|
|
|
|
|
text_file.seek(0)
|
|
|
|
for line in text_file.readlines():
|
2020-07-13 11:22:58 -04:00
|
|
|
if not line.strip():
|
|
|
|
continue
|
|
|
|
|
2024-10-08 03:02:34 -07:00
|
|
|
# # if the line is a local file path that resolves, then we can archive it
|
|
|
|
# if line.startswith('file://'):
|
|
|
|
# try:
|
|
|
|
# if Path(line).exists():
|
|
|
|
# yield Link(
|
|
|
|
# url=line,
|
|
|
|
# timestamp=str(datetime.now(timezone.utc).timestamp()),
|
|
|
|
# title=None,
|
|
|
|
# tags=None,
|
|
|
|
# sources=[text_file.name],
|
|
|
|
# )
|
|
|
|
# except (OSError, PermissionError):
|
|
|
|
# # nvm, not a valid path...
|
|
|
|
# pass
|
2020-07-13 11:22:58 -04:00
|
|
|
|
|
|
|
# otherwise look for anything that looks like a URL in the line
|
2024-04-24 17:45:45 -07:00
|
|
|
for url in find_all_urls(line):
|
2019-04-27 17:26:24 -04:00
|
|
|
yield Link(
|
|
|
|
url=htmldecode(url),
|
2021-04-10 04:19:30 -04:00
|
|
|
timestamp=str(datetime.now(timezone.utc).timestamp()),
|
2019-04-27 17:26:24 -04:00
|
|
|
title=None,
|
|
|
|
tags=None,
|
|
|
|
sources=[text_file.name],
|
|
|
|
)
|
2020-07-27 18:52:02 -04:00
|
|
|
|
2021-03-31 01:05:49 -04:00
|
|
|
|
|
|
|
KEY = 'txt'
|
|
|
|
NAME = 'Generic TXT'
|
|
|
|
PARSER = parse_generic_txt_export
|