1
0
Fork 0
mirror of synced 2024-06-26 10:00:19 +12:00
ArchiveBox/archivebox/parsers/generic_txt.py

53 lines
1.4 KiB
Python
Raw Normal View History

2019-04-28 09:26:24 +12:00
__package__ = 'archivebox.parsers'
__description__ = 'Plain Text'
from typing import IO, Iterable
from datetime import datetime, timezone
from pathlib import Path
2019-04-28 09:26:24 +12:00
from ..index.schema import Link
from ..util import (
htmldecode,
enforce_types,
find_all_urls,
2019-04-28 09:26:24 +12:00
)
2019-04-28 09:26:24 +12:00
@enforce_types
def parse_generic_txt_export(text_file: IO[str], **_kwargs) -> Iterable[Link]:
2021-03-21 05:38:00 +13:00
"""Parse links from a text file, ignoring other text"""
2019-04-28 09:26:24 +12:00
text_file.seek(0)
for line in text_file.readlines():
if not line.strip():
continue
# if the line is a local file path that resolves, then we can archive it
2020-08-19 00:23:57 +12:00
try:
if Path(line).exists():
yield Link(
url=line,
timestamp=str(datetime.now(timezone.utc).timestamp()),
2020-08-19 00:23:57 +12:00
title=None,
tags=None,
sources=[text_file.name],
)
except (OSError, PermissionError):
# nvm, not a valid path...
pass
# otherwise look for anything that looks like a URL in the line
for url in find_all_urls(line):
2019-04-28 09:26:24 +12:00
yield Link(
url=htmldecode(url),
timestamp=str(datetime.now(timezone.utc).timestamp()),
2019-04-28 09:26:24 +12:00
title=None,
tags=None,
sources=[text_file.name],
)
KEY = 'txt'
NAME = 'Generic TXT'
PARSER = parse_generic_txt_export