2019-04-27 21:26:24 +00:00
|
|
|
__package__ = 'archivebox.parsers'
|
|
|
|
__description__ = 'Plain Text'
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from typing import IO, Iterable
|
2021-04-10 08:19:30 +00:00
|
|
|
from datetime import datetime, timezone
|
2020-07-13 15:22:58 +00:00
|
|
|
from pathlib import Path
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
from ..index.schema import Link
|
|
|
|
from ..util import (
|
|
|
|
htmldecode,
|
|
|
|
enforce_types,
|
|
|
|
URL_REGEX
|
|
|
|
)
|
|
|
|
|
2020-07-13 15:22:58 +00:00
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
@enforce_types
|
2020-08-18 12:27:47 +00:00
|
|
|
def parse_generic_txt_export(text_file: IO[str], **_kwargs) -> Iterable[Link]:
|
2021-03-20 16:38:00 +00:00
|
|
|
"""Parse links from a text file, ignoring other text"""
|
2019-04-27 21:26:24 +00:00
|
|
|
|
|
|
|
text_file.seek(0)
|
|
|
|
for line in text_file.readlines():
|
2020-07-13 15:22:58 +00:00
|
|
|
if not line.strip():
|
|
|
|
continue
|
|
|
|
|
|
|
|
# if the line is a local file path that resolves, then we can archive it
|
2020-08-18 12:23:57 +00:00
|
|
|
try:
|
|
|
|
if Path(line).exists():
|
|
|
|
yield Link(
|
|
|
|
url=line,
|
2021-04-10 08:19:30 +00:00
|
|
|
timestamp=str(datetime.now(timezone.utc).timestamp()),
|
2020-08-18 12:23:57 +00:00
|
|
|
title=None,
|
|
|
|
tags=None,
|
|
|
|
sources=[text_file.name],
|
|
|
|
)
|
|
|
|
except (OSError, PermissionError):
|
|
|
|
# nvm, not a valid path...
|
|
|
|
pass
|
2020-07-13 15:22:58 +00:00
|
|
|
|
|
|
|
# otherwise look for anything that looks like a URL in the line
|
|
|
|
for url in re.findall(URL_REGEX, line):
|
2019-04-27 21:26:24 +00:00
|
|
|
yield Link(
|
|
|
|
url=htmldecode(url),
|
2021-04-10 08:19:30 +00:00
|
|
|
timestamp=str(datetime.now(timezone.utc).timestamp()),
|
2019-04-27 21:26:24 +00:00
|
|
|
title=None,
|
|
|
|
tags=None,
|
|
|
|
sources=[text_file.name],
|
|
|
|
)
|
2020-07-27 22:52:02 +00:00
|
|
|
|
|
|
|
# look inside the URL for any sub-urls, e.g. for archive.org links
|
|
|
|
# https://web.archive.org/web/20200531203453/https://www.reddit.com/r/socialism/comments/gu24ke/nypd_officers_claim_they_are_protecting_the_rule/fsfq0sw/
|
|
|
|
# -> https://www.reddit.com/r/socialism/comments/gu24ke/nypd_officers_claim_they_are_protecting_the_rule/fsfq0sw/
|
2021-01-30 11:07:35 +00:00
|
|
|
for sub_url in re.findall(URL_REGEX, line[1:]):
|
2020-07-27 22:52:02 +00:00
|
|
|
yield Link(
|
2021-01-30 11:07:35 +00:00
|
|
|
url=htmldecode(sub_url),
|
2021-04-10 08:19:30 +00:00
|
|
|
timestamp=str(datetime.now(timezone.utc).timestamp()),
|
2020-07-27 22:52:02 +00:00
|
|
|
title=None,
|
|
|
|
tags=None,
|
|
|
|
sources=[text_file.name],
|
|
|
|
)
|
2021-03-31 05:05:49 +00:00
|
|
|
|
|
|
|
KEY = 'txt'
|
|
|
|
NAME = 'Generic TXT'
|
|
|
|
PARSER = parse_generic_txt_export
|