2017-07-04 22:38:07 +12:00
|
|
|
import os
|
2017-10-19 11:38:17 +13:00
|
|
|
import json
|
|
|
|
|
2017-07-04 22:38:07 +12:00
|
|
|
from datetime import datetime
|
2017-07-05 21:59:09 +12:00
|
|
|
from string import Template
|
2019-04-03 17:27:37 +13:00
|
|
|
from typing import List, Tuple, Iterator, Optional, Mapping, Iterable
|
|
|
|
from collections import OrderedDict
|
2019-03-26 20:20:41 +13:00
|
|
|
|
2019-04-03 11:53:21 +13:00
|
|
|
from .schema import Link, ArchiveResult
|
|
|
|
from .config import (
|
2019-02-22 11:45:28 +13:00
|
|
|
OUTPUT_DIR,
|
2018-06-11 13:12:55 +12:00
|
|
|
TEMPLATES_DIR,
|
2019-03-28 08:35:13 +13:00
|
|
|
VERSION,
|
2017-10-19 11:38:17 +13:00
|
|
|
GIT_SHA,
|
2018-04-18 01:13:38 +12:00
|
|
|
FOOTER_INFO,
|
2019-03-28 03:31:22 +13:00
|
|
|
TIMEOUT,
|
2019-04-03 17:27:37 +13:00
|
|
|
URL_BLACKLIST_PTN,
|
2017-07-06 09:33:51 +12:00
|
|
|
)
|
2019-04-03 11:53:21 +13:00
|
|
|
from .util import (
|
2019-04-03 17:27:37 +13:00
|
|
|
scheme,
|
|
|
|
fuzzy_url,
|
2019-04-03 09:36:41 +13:00
|
|
|
ts_to_date,
|
2019-03-21 22:35:41 +13:00
|
|
|
urlencode,
|
2019-03-31 15:25:10 +13:00
|
|
|
htmlencode,
|
|
|
|
urldecode,
|
2019-03-20 11:09:46 +13:00
|
|
|
wget_output_path,
|
2019-03-27 15:26:21 +13:00
|
|
|
enforce_types,
|
2019-03-28 03:31:22 +13:00
|
|
|
TimedProgress,
|
2019-03-28 04:39:51 +13:00
|
|
|
copy_and_overwrite,
|
2019-03-28 11:24:57 +13:00
|
|
|
atomic_write,
|
2017-10-23 22:58:41 +13:00
|
|
|
)
|
2019-04-03 11:53:21 +13:00
|
|
|
from .parse import parse_links
|
|
|
|
from .logs import (
|
2019-03-23 08:09:39 +13:00
|
|
|
log_indexing_process_started,
|
2019-03-21 18:28:12 +13:00
|
|
|
log_indexing_started,
|
|
|
|
log_indexing_finished,
|
|
|
|
log_parsing_started,
|
|
|
|
log_parsing_finished,
|
|
|
|
)
|
2017-10-19 11:38:17 +13:00
|
|
|
|
2019-03-20 11:09:46 +13:00
|
|
|
TITLE_LOADING_MSG = 'Not yet archived...'
|
|
|
|
|
2017-10-19 11:38:17 +13:00
|
|
|
|
2019-03-28 11:24:30 +13:00
|
|
|
|
2019-04-03 17:27:37 +13:00
|
|
|
### Link filtering and checking
|
|
|
|
|
|
|
|
@enforce_types
|
|
|
|
def derived_link_info(link: Link) -> dict:
|
|
|
|
"""extend link info with the archive urls and other derived data"""
|
|
|
|
|
|
|
|
info = link._asdict(extended=True)
|
|
|
|
info.update(link.canonical_outputs())
|
|
|
|
|
|
|
|
return info
|
|
|
|
|
|
|
|
|
|
|
|
@enforce_types
|
|
|
|
def merge_links(a: Link, b: Link) -> Link:
|
|
|
|
"""deterministially merge two links, favoring longer field values over shorter,
|
|
|
|
and "cleaner" values over worse ones.
|
|
|
|
"""
|
|
|
|
assert a.base_url == b.base_url, 'Cannot merge two links with different URLs'
|
|
|
|
|
|
|
|
url = a.url if len(a.url) > len(b.url) else b.url
|
|
|
|
|
|
|
|
possible_titles = [
|
|
|
|
title
|
|
|
|
for title in (a.title, b.title)
|
|
|
|
if title and title.strip() and '://' not in title
|
|
|
|
]
|
|
|
|
title = None
|
|
|
|
if len(possible_titles) == 2:
|
|
|
|
title = max(possible_titles, key=lambda t: len(t))
|
|
|
|
elif len(possible_titles) == 1:
|
|
|
|
title = possible_titles[0]
|
|
|
|
|
|
|
|
timestamp = (
|
|
|
|
a.timestamp
|
|
|
|
if float(a.timestamp or 0) < float(b.timestamp or 0) else
|
|
|
|
b.timestamp
|
|
|
|
)
|
|
|
|
|
|
|
|
tags_set = (
|
|
|
|
set(tag.strip() for tag in (a.tags or '').split(','))
|
|
|
|
| set(tag.strip() for tag in (b.tags or '').split(','))
|
|
|
|
)
|
|
|
|
tags = ','.join(tags_set) or None
|
|
|
|
|
|
|
|
sources = list(set(a.sources + b.sources))
|
|
|
|
|
|
|
|
all_methods = set(list(a.history.keys()) + list(a.history.keys()))
|
|
|
|
history = {
|
|
|
|
method: (a.history.get(method) or []) + (b.history.get(method) or [])
|
|
|
|
for method in all_methods
|
|
|
|
}
|
|
|
|
|
|
|
|
return Link(
|
|
|
|
url=url,
|
|
|
|
timestamp=timestamp,
|
|
|
|
title=title,
|
|
|
|
tags=tags,
|
|
|
|
sources=sources,
|
|
|
|
history=history,
|
|
|
|
)
|
|
|
|
|
|
|
|
def validate_links(links: Iterable[Link]) -> Iterable[Link]:
|
|
|
|
links = archivable_links(links) # remove chrome://, about:, mailto: etc.
|
|
|
|
links = sorted_links(links) # deterministically sort the links based on timstamp, url
|
|
|
|
links = uniquefied_links(links) # merge/dedupe duplicate timestamps & urls
|
|
|
|
|
|
|
|
if not links:
|
|
|
|
print('[X] No links found :(')
|
|
|
|
raise SystemExit(1)
|
|
|
|
|
|
|
|
return links
|
|
|
|
|
|
|
|
def archivable_links(links: Iterable[Link]) -> Iterable[Link]:
|
|
|
|
"""remove chrome://, about:// or other schemed links that cant be archived"""
|
|
|
|
for link in links:
|
|
|
|
scheme_is_valid = scheme(link.url) in ('http', 'https', 'ftp')
|
|
|
|
not_blacklisted = (not URL_BLACKLIST_PTN.match(link.url)) if URL_BLACKLIST_PTN else True
|
|
|
|
if scheme_is_valid and not_blacklisted:
|
|
|
|
yield link
|
|
|
|
|
|
|
|
|
|
|
|
def uniquefied_links(sorted_links: Iterable[Link]) -> Iterable[Link]:
|
|
|
|
"""
|
|
|
|
ensures that all non-duplicate links have monotonically increasing timestamps
|
|
|
|
"""
|
|
|
|
|
|
|
|
unique_urls: OrderedDict[str, Link] = OrderedDict()
|
|
|
|
|
|
|
|
for link in sorted_links:
|
|
|
|
fuzzy = fuzzy_url(link.url)
|
|
|
|
if fuzzy in unique_urls:
|
|
|
|
# merge with any other links that share the same url
|
|
|
|
link = merge_links(unique_urls[fuzzy], link)
|
|
|
|
unique_urls[fuzzy] = link
|
|
|
|
|
|
|
|
unique_timestamps: OrderedDict[str, Link] = OrderedDict()
|
|
|
|
for link in unique_urls.values():
|
|
|
|
new_link = link.overwrite(
|
|
|
|
timestamp=lowest_uniq_timestamp(unique_timestamps, link.timestamp),
|
|
|
|
)
|
|
|
|
unique_timestamps[new_link.timestamp] = new_link
|
|
|
|
|
|
|
|
return unique_timestamps.values()
|
|
|
|
|
|
|
|
|
|
|
|
def sorted_links(links: Iterable[Link]) -> Iterable[Link]:
|
|
|
|
sort_func = lambda link: (link.timestamp.split('.', 1)[0], link.url)
|
|
|
|
return sorted(links, key=sort_func, reverse=True)
|
|
|
|
|
|
|
|
|
|
|
|
def links_after_timestamp(links: Iterable[Link], resume: float=None) -> Iterable[Link]:
|
|
|
|
if not resume:
|
|
|
|
yield from links
|
|
|
|
return
|
|
|
|
|
|
|
|
for link in links:
|
|
|
|
try:
|
|
|
|
if float(link.timestamp) <= resume:
|
|
|
|
yield link
|
|
|
|
except (ValueError, TypeError):
|
|
|
|
print('Resume value and all timestamp values must be valid numbers.')
|
|
|
|
|
|
|
|
|
|
|
|
def lowest_uniq_timestamp(used_timestamps: OrderedDict, timestamp: str) -> str:
|
|
|
|
"""resolve duplicate timestamps by appending a decimal 1234, 1234 -> 1234.1, 1234.2"""
|
|
|
|
|
|
|
|
timestamp = timestamp.split('.')[0]
|
|
|
|
nonce = 0
|
|
|
|
|
|
|
|
# first try 152323423 before 152323423.0
|
|
|
|
if timestamp not in used_timestamps:
|
|
|
|
return timestamp
|
|
|
|
|
|
|
|
new_timestamp = '{}.{}'.format(timestamp, nonce)
|
|
|
|
while new_timestamp in used_timestamps:
|
|
|
|
nonce += 1
|
|
|
|
new_timestamp = '{}.{}'.format(timestamp, nonce)
|
|
|
|
|
|
|
|
return new_timestamp
|
|
|
|
|
|
|
|
|
2019-03-28 11:24:30 +13:00
|
|
|
|
2017-10-19 11:38:17 +13:00
|
|
|
### Homepage index for all the links
|
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
@enforce_types
|
2019-03-28 11:24:30 +13:00
|
|
|
def write_links_index(links: List[Link], out_dir: str=OUTPUT_DIR, finished: bool=False) -> None:
|
2017-10-19 11:38:17 +13:00
|
|
|
"""create index.html file for a given list of links"""
|
|
|
|
|
2019-03-23 08:09:39 +13:00
|
|
|
log_indexing_process_started()
|
2019-02-22 11:45:28 +13:00
|
|
|
|
2019-03-23 08:09:39 +13:00
|
|
|
log_indexing_started(out_dir, 'index.json')
|
2019-03-28 03:31:22 +13:00
|
|
|
timer = TimedProgress(TIMEOUT * 2, prefix=' ')
|
2019-03-28 11:24:30 +13:00
|
|
|
write_json_links_index(links, out_dir=out_dir)
|
2019-03-28 03:31:22 +13:00
|
|
|
timer.end()
|
2019-03-21 18:28:12 +13:00
|
|
|
log_indexing_finished(out_dir, 'index.json')
|
2019-02-07 19:06:21 +13:00
|
|
|
|
2019-03-23 08:09:39 +13:00
|
|
|
log_indexing_started(out_dir, 'index.html')
|
2019-03-28 03:31:22 +13:00
|
|
|
timer = TimedProgress(TIMEOUT * 2, prefix=' ')
|
2019-03-28 11:24:30 +13:00
|
|
|
write_html_links_index(links, out_dir=out_dir, finished=finished)
|
2019-03-28 03:31:22 +13:00
|
|
|
timer.end()
|
2019-03-21 18:28:12 +13:00
|
|
|
log_indexing_finished(out_dir, 'index.html')
|
2019-03-26 22:33:34 +13:00
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
|
|
|
|
@enforce_types
|
2019-03-27 16:25:07 +13:00
|
|
|
def load_links_index(out_dir: str=OUTPUT_DIR, import_path: Optional[str]=None) -> Tuple[List[Link], List[Link]]:
|
2019-03-21 18:28:12 +13:00
|
|
|
"""parse and load existing index with any new links from import_path merged in"""
|
|
|
|
|
2019-03-26 20:20:41 +13:00
|
|
|
existing_links: List[Link] = []
|
2019-03-21 18:28:12 +13:00
|
|
|
if out_dir:
|
2019-03-27 12:21:34 +13:00
|
|
|
existing_links = list(parse_json_links_index(out_dir))
|
2019-03-21 18:28:12 +13:00
|
|
|
|
2019-03-26 20:20:41 +13:00
|
|
|
new_links: List[Link] = []
|
2019-03-21 18:28:12 +13:00
|
|
|
if import_path:
|
|
|
|
# parse and validate the import file
|
|
|
|
log_parsing_started(import_path)
|
|
|
|
raw_links, parser_name = parse_links(import_path)
|
2019-03-27 12:21:34 +13:00
|
|
|
new_links = list(validate_links(raw_links))
|
2019-03-21 18:28:12 +13:00
|
|
|
|
|
|
|
# merge existing links in out_dir and new links
|
2019-03-27 12:21:34 +13:00
|
|
|
all_links = list(validate_links(existing_links + new_links))
|
2019-03-21 18:28:12 +13:00
|
|
|
|
|
|
|
if import_path and parser_name:
|
2019-03-28 08:16:53 +13:00
|
|
|
num_parsed = len(raw_links)
|
|
|
|
num_new_links = len(all_links) - len(existing_links)
|
|
|
|
log_parsing_finished(num_parsed, num_new_links, parser_name)
|
2019-03-21 18:28:12 +13:00
|
|
|
|
|
|
|
return all_links, new_links
|
2017-07-04 22:38:07 +12:00
|
|
|
|
2019-03-26 22:33:34 +13:00
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
@enforce_types
|
2019-03-28 11:24:30 +13:00
|
|
|
def write_json_links_index(links: List[Link], out_dir: str=OUTPUT_DIR) -> None:
|
2017-10-19 11:38:17 +13:00
|
|
|
"""write the json link index to a given path"""
|
|
|
|
|
2019-03-27 12:21:34 +13:00
|
|
|
assert isinstance(links, List), 'Links must be a list, not a generator.'
|
|
|
|
assert isinstance(links[0].history, dict)
|
|
|
|
assert isinstance(links[0].sources, list)
|
|
|
|
|
|
|
|
if links[0].history.get('title'):
|
|
|
|
assert isinstance(links[0].history['title'][0], ArchiveResult)
|
|
|
|
|
|
|
|
if links[0].sources:
|
|
|
|
assert isinstance(links[0].sources[0], str)
|
2019-02-22 11:45:28 +13:00
|
|
|
|
2017-10-19 11:38:17 +13:00
|
|
|
path = os.path.join(out_dir, 'index.json')
|
|
|
|
|
2019-03-28 08:32:39 +13:00
|
|
|
index_json = {
|
|
|
|
'info': 'ArchiveBox Index',
|
|
|
|
'source': 'https://github.com/pirate/ArchiveBox',
|
|
|
|
'docs': 'https://github.com/pirate/ArchiveBox/wiki',
|
|
|
|
'version': VERSION,
|
|
|
|
'num_links': len(links),
|
|
|
|
'updated': datetime.now(),
|
|
|
|
'links': links,
|
|
|
|
}
|
2019-03-28 11:24:57 +13:00
|
|
|
atomic_write(index_json, path)
|
2017-10-19 11:38:17 +13:00
|
|
|
|
2019-03-26 22:33:34 +13:00
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
@enforce_types
|
2019-03-27 12:21:34 +13:00
|
|
|
def parse_json_links_index(out_dir: str=OUTPUT_DIR) -> Iterator[Link]:
|
2019-03-21 18:28:12 +13:00
|
|
|
"""parse a archive index json file and return the list of links"""
|
2019-03-27 12:21:34 +13:00
|
|
|
|
2017-10-23 22:58:41 +13:00
|
|
|
index_path = os.path.join(out_dir, 'index.json')
|
|
|
|
if os.path.exists(index_path):
|
|
|
|
with open(index_path, 'r', encoding='utf-8') as f:
|
2019-02-22 11:45:28 +13:00
|
|
|
links = json.load(f)['links']
|
2019-03-28 03:31:07 +13:00
|
|
|
for link_json in links:
|
2019-03-31 08:03:46 +13:00
|
|
|
yield Link.from_json(link_json)
|
2017-10-23 22:58:41 +13:00
|
|
|
|
2019-03-27 12:21:34 +13:00
|
|
|
return ()
|
2017-10-23 22:58:41 +13:00
|
|
|
|
2019-03-26 22:33:34 +13:00
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
@enforce_types
|
2019-03-28 11:24:57 +13:00
|
|
|
def write_html_links_index(links: List[Link], out_dir: str=OUTPUT_DIR, finished: bool=False) -> None:
|
2017-10-19 11:38:17 +13:00
|
|
|
"""write the html link index to a given path"""
|
|
|
|
|
2019-03-28 11:24:57 +13:00
|
|
|
copy_and_overwrite(
|
|
|
|
os.path.join(TEMPLATES_DIR, 'static'),
|
|
|
|
os.path.join(out_dir, 'static'),
|
|
|
|
)
|
2018-04-17 23:00:40 +12:00
|
|
|
|
2019-03-28 11:24:57 +13:00
|
|
|
atomic_write('User-agent: *\nDisallow: /', os.path.join(out_dir, 'robots.txt'))
|
2018-09-13 11:25:48 +12:00
|
|
|
|
2018-06-11 13:12:55 +12:00
|
|
|
with open(os.path.join(TEMPLATES_DIR, 'index.html'), 'r', encoding='utf-8') as f:
|
2017-07-04 22:38:07 +12:00
|
|
|
index_html = f.read()
|
|
|
|
|
2018-06-11 13:12:55 +12:00
|
|
|
with open(os.path.join(TEMPLATES_DIR, 'index_row.html'), 'r', encoding='utf-8') as f:
|
2017-10-19 11:38:17 +13:00
|
|
|
link_row_html = f.read()
|
2017-07-04 22:38:07 +12:00
|
|
|
|
2019-03-31 14:29:16 +13:00
|
|
|
link_rows = []
|
|
|
|
for link in links:
|
|
|
|
template_row_vars: Mapping[str, str] = {
|
2019-03-27 12:21:34 +13:00
|
|
|
**derived_link_info(link),
|
2019-03-20 11:09:46 +13:00
|
|
|
'title': (
|
2019-03-27 12:21:34 +13:00
|
|
|
link.title
|
|
|
|
or (link.base_url if link.is_archived else TITLE_LOADING_MSG)
|
2019-03-20 11:09:46 +13:00
|
|
|
),
|
2019-03-27 20:49:39 +13:00
|
|
|
'tags': (link.tags or '') + (' {}'.format(link.extension) if link.is_static else ''),
|
2019-03-20 21:30:00 +13:00
|
|
|
'favicon_url': (
|
2019-03-27 12:21:34 +13:00
|
|
|
os.path.join('archive', link.timestamp, 'favicon.ico')
|
2019-03-20 21:30:00 +13:00
|
|
|
# if link['is_archived'] else 'data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs='
|
|
|
|
),
|
2019-03-21 22:35:41 +13:00
|
|
|
'archive_url': urlencode(
|
2019-03-20 11:09:46 +13:00
|
|
|
wget_output_path(link) or 'index.html'
|
|
|
|
),
|
2019-03-31 14:29:16 +13:00
|
|
|
}
|
|
|
|
link_rows.append(Template(link_row_html).substitute(**template_row_vars))
|
2017-07-04 22:38:07 +12:00
|
|
|
|
2019-03-31 14:29:16 +13:00
|
|
|
template_vars: Mapping[str, str] = {
|
|
|
|
'num_links': str(len(links)),
|
2017-07-04 23:24:03 +12:00
|
|
|
'date_updated': datetime.now().strftime('%Y-%m-%d'),
|
|
|
|
'time_updated': datetime.now().strftime('%Y-%m-%d %H:%M'),
|
2018-04-18 01:14:01 +12:00
|
|
|
'footer_info': FOOTER_INFO,
|
2019-03-28 08:35:13 +13:00
|
|
|
'version': VERSION,
|
2018-04-18 01:14:01 +12:00
|
|
|
'git_sha': GIT_SHA,
|
2019-03-31 14:29:16 +13:00
|
|
|
'rows': '\n'.join(link_rows),
|
2019-03-20 11:08:42 +13:00
|
|
|
'status': 'finished' if finished else 'running',
|
2017-07-04 23:24:03 +12:00
|
|
|
}
|
2019-03-31 14:29:16 +13:00
|
|
|
template_html = Template(index_html).substitute(**template_vars)
|
2017-07-04 22:38:07 +12:00
|
|
|
|
2019-03-31 14:29:16 +13:00
|
|
|
atomic_write(template_html, os.path.join(out_dir, 'index.html'))
|
2017-07-06 09:33:51 +12:00
|
|
|
|
|
|
|
|
2017-10-19 11:38:17 +13:00
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
@enforce_types
|
2019-03-26 20:20:41 +13:00
|
|
|
def patch_links_index(link: Link, out_dir: str=OUTPUT_DIR) -> None:
|
2019-03-20 11:09:46 +13:00
|
|
|
"""hack to in-place update one row's info in the generated index html"""
|
|
|
|
|
2019-03-27 12:21:34 +13:00
|
|
|
title = link.title or link.latest_outputs()['title']
|
|
|
|
successful = link.num_outputs
|
2019-02-22 11:45:28 +13:00
|
|
|
|
2019-03-20 11:09:46 +13:00
|
|
|
# Patch JSON index
|
2019-03-21 18:28:12 +13:00
|
|
|
json_file_links = parse_json_links_index(out_dir)
|
2019-03-27 12:21:34 +13:00
|
|
|
patched_links = []
|
2019-03-21 18:28:12 +13:00
|
|
|
for saved_link in json_file_links:
|
2019-03-27 12:21:34 +13:00
|
|
|
if saved_link.url == link.url:
|
2019-03-27 20:49:39 +13:00
|
|
|
patched_links.append(saved_link.overwrite(
|
|
|
|
title=title,
|
|
|
|
history=link.history,
|
|
|
|
updated=link.updated,
|
|
|
|
))
|
2019-03-27 12:21:34 +13:00
|
|
|
else:
|
|
|
|
patched_links.append(saved_link)
|
|
|
|
|
2019-03-28 11:24:30 +13:00
|
|
|
write_json_links_index(patched_links, out_dir=out_dir)
|
2019-02-22 11:45:28 +13:00
|
|
|
|
2019-03-20 11:09:46 +13:00
|
|
|
# Patch HTML index
|
2019-03-21 18:28:12 +13:00
|
|
|
html_path = os.path.join(out_dir, 'index.html')
|
2019-03-20 11:09:46 +13:00
|
|
|
html = open(html_path, 'r').read().split('\n')
|
|
|
|
for idx, line in enumerate(html):
|
2019-03-27 12:21:34 +13:00
|
|
|
if title and ('<span data-title-for="{}"'.format(link.url) in line):
|
2019-03-20 11:09:46 +13:00
|
|
|
html[idx] = '<span>{}</span>'.format(title)
|
2019-03-27 12:21:34 +13:00
|
|
|
elif successful and ('<span data-number-for="{}"'.format(link.url) in line):
|
2019-03-20 11:09:46 +13:00
|
|
|
html[idx] = '<span>{}</span>'.format(successful)
|
|
|
|
break
|
|
|
|
|
2019-03-28 11:24:57 +13:00
|
|
|
atomic_write('\n'.join(html), html_path)
|
2019-02-22 11:45:28 +13:00
|
|
|
|
2019-03-21 18:28:12 +13:00
|
|
|
|
2017-10-23 22:58:41 +13:00
|
|
|
### Individual link index
|
2017-10-19 11:38:17 +13:00
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
@enforce_types
|
2019-03-28 11:24:30 +13:00
|
|
|
def write_link_index(link: Link, link_dir: Optional[str]=None) -> None:
|
|
|
|
link_dir = link_dir or link.link_dir
|
|
|
|
|
|
|
|
write_json_link_index(link, link_dir)
|
|
|
|
write_html_link_index(link, link_dir)
|
2017-10-19 11:38:17 +13:00
|
|
|
|
2019-03-26 22:33:34 +13:00
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
@enforce_types
|
2019-03-28 11:24:30 +13:00
|
|
|
def write_json_link_index(link: Link, link_dir: Optional[str]=None) -> None:
|
2017-10-19 11:38:17 +13:00
|
|
|
"""write a json file with some info about the link"""
|
|
|
|
|
2019-03-28 11:24:30 +13:00
|
|
|
link_dir = link_dir or link.link_dir
|
|
|
|
path = os.path.join(link_dir, 'index.json')
|
2017-10-19 11:38:17 +13:00
|
|
|
|
2019-03-28 11:24:57 +13:00
|
|
|
atomic_write(link._asdict(), path)
|
2017-10-19 11:38:17 +13:00
|
|
|
|
2019-03-26 22:33:34 +13:00
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
@enforce_types
|
2019-03-28 11:24:30 +13:00
|
|
|
def parse_json_link_index(link_dir: str) -> Optional[Link]:
|
2017-10-23 22:58:41 +13:00
|
|
|
"""load the json link index from a given directory"""
|
2019-03-28 11:24:30 +13:00
|
|
|
existing_index = os.path.join(link_dir, 'index.json')
|
2017-10-23 22:58:41 +13:00
|
|
|
if os.path.exists(existing_index):
|
|
|
|
with open(existing_index, 'r', encoding='utf-8') as f:
|
2019-02-22 11:45:28 +13:00
|
|
|
link_json = json.load(f)
|
2019-03-31 08:38:28 +13:00
|
|
|
return Link.from_json(link_json)
|
2019-03-27 12:21:34 +13:00
|
|
|
return None
|
2017-10-23 22:58:41 +13:00
|
|
|
|
2019-03-26 22:33:34 +13:00
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
@enforce_types
|
2019-03-28 11:24:30 +13:00
|
|
|
def load_json_link_index(link: Link, link_dir: Optional[str]=None) -> Link:
|
2019-03-21 18:28:12 +13:00
|
|
|
"""check for an existing link archive in the given directory,
|
|
|
|
and load+merge it into the given link dict
|
|
|
|
"""
|
2019-03-28 11:24:30 +13:00
|
|
|
link_dir = link_dir or link.link_dir
|
|
|
|
existing_link = parse_json_link_index(link_dir)
|
2019-03-27 15:26:21 +13:00
|
|
|
if existing_link:
|
|
|
|
return merge_links(existing_link, link)
|
|
|
|
return link
|
2019-03-21 18:28:12 +13:00
|
|
|
|
2019-03-26 22:33:34 +13:00
|
|
|
|
2019-03-27 15:26:21 +13:00
|
|
|
@enforce_types
|
2019-03-28 11:24:30 +13:00
|
|
|
def write_html_link_index(link: Link, link_dir: Optional[str]=None) -> None:
|
|
|
|
link_dir = link_dir or link.link_dir
|
|
|
|
|
2019-03-09 11:46:14 +13:00
|
|
|
with open(os.path.join(TEMPLATES_DIR, 'link_index.html'), 'r', encoding='utf-8') as f:
|
2017-10-19 11:38:17 +13:00
|
|
|
link_html = f.read()
|
|
|
|
|
2019-03-28 11:24:57 +13:00
|
|
|
path = os.path.join(link_dir, 'index.html')
|
|
|
|
|
2019-03-31 15:25:10 +13:00
|
|
|
template_vars: Mapping[str, str] = {
|
2019-03-28 11:24:57 +13:00
|
|
|
**derived_link_info(link),
|
|
|
|
'title': (
|
|
|
|
link.title
|
|
|
|
or (link.base_url if link.is_archived else TITLE_LOADING_MSG)
|
|
|
|
),
|
2019-03-31 15:25:10 +13:00
|
|
|
'url_str': htmlencode(urldecode(link.base_url)),
|
2019-03-28 11:24:57 +13:00
|
|
|
'archive_url': urlencode(
|
|
|
|
wget_output_path(link)
|
|
|
|
or (link.domain if link.is_archived else 'about:blank')
|
|
|
|
),
|
|
|
|
'extension': link.extension or 'html',
|
|
|
|
'tags': link.tags or 'untagged',
|
|
|
|
'status': 'archived' if link.is_archived else 'not yet archived',
|
|
|
|
'status_color': 'success' if link.is_archived else 'danger',
|
2019-04-03 09:36:41 +13:00
|
|
|
'oldest_archive_date': ts_to_date(link.oldest_archive_date),
|
2019-03-31 15:25:10 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
html_index = Template(link_html).substitute(**template_vars)
|
2019-03-28 11:24:57 +13:00
|
|
|
|
|
|
|
atomic_write(html_index, path)
|