2017-05-05 09:00:30 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-06-30 08:23:19 +00:00
|
|
|
# Bookmark Archiver
|
|
|
|
# Nick Sweeting 2017 | MIT License
|
|
|
|
# https://github.com/pirate/bookmark-archiver
|
2017-05-05 09:00:30 +00:00
|
|
|
|
|
|
|
import sys
|
2017-05-05 23:36:46 +00:00
|
|
|
|
2017-06-30 08:23:19 +00:00
|
|
|
from datetime import datetime
|
2017-05-29 18:00:46 +00:00
|
|
|
|
2017-10-23 09:56:21 +00:00
|
|
|
from parse import parse_links
|
2017-10-18 22:38:17 +00:00
|
|
|
from links import validate_links
|
|
|
|
from archive_methods import archive_links, _RESULTS_TOTALS
|
|
|
|
from index import (
|
|
|
|
write_links_index,
|
|
|
|
write_link_index,
|
|
|
|
parse_json_links_index,
|
|
|
|
parse_json_link_index,
|
|
|
|
)
|
2017-07-04 10:38:07 +00:00
|
|
|
from config import (
|
|
|
|
ARCHIVE_PERMISSIONS,
|
2017-10-18 22:38:17 +00:00
|
|
|
HTML_FOLDER,
|
|
|
|
ARCHIVE_FOLDER,
|
2017-07-05 21:33:51 +00:00
|
|
|
ANSI,
|
2017-10-18 22:38:17 +00:00
|
|
|
TIMEOUT,
|
|
|
|
)
|
|
|
|
from util import (
|
|
|
|
download_url,
|
2017-07-04 10:38:07 +00:00
|
|
|
check_dependencies,
|
2017-10-18 22:38:17 +00:00
|
|
|
progress,
|
2017-10-23 09:56:21 +00:00
|
|
|
cleanup_archive,
|
2017-07-04 10:38:07 +00:00
|
|
|
)
|
2017-06-15 22:33:01 +00:00
|
|
|
|
2017-10-23 09:56:21 +00:00
|
|
|
__DESCRIPTION__ = 'Bookmark Archiver: Create a browsable html archive of a list of links.'
|
2017-07-04 10:38:07 +00:00
|
|
|
__DOCUMENTATION__ = 'https://github.com/pirate/bookmark-archiver'
|
2017-05-05 11:27:05 +00:00
|
|
|
|
2017-06-30 08:23:19 +00:00
|
|
|
|
2017-10-23 09:56:21 +00:00
|
|
|
def update_archive(export_path, links, resume=None, append=True):
|
|
|
|
"""update or create index.html+json given a path to an export file containing new links"""
|
2017-06-30 08:23:19 +00:00
|
|
|
|
2017-10-18 22:38:17 +00:00
|
|
|
start_ts = datetime.now().timestamp()
|
|
|
|
|
2017-10-23 09:56:21 +00:00
|
|
|
# loop over links and archive them
|
|
|
|
archive_links(ARCHIVE_FOLDER, links, export_path, resume=resume)
|
|
|
|
|
|
|
|
# print timing information & summary
|
|
|
|
end_ts = datetime.now().timestamp()
|
2017-10-30 09:31:23 +00:00
|
|
|
seconds = round(end_ts - start_ts, 2)
|
|
|
|
duration = '{} min'.format(round(seconds / 60, 2)) if seconds > 60 else '{} sec'.format(seconds)
|
2017-10-23 09:56:21 +00:00
|
|
|
print('{}[√] [{}] Archive update complete ({}){}'.format(
|
|
|
|
ANSI['green'],
|
|
|
|
datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
duration,
|
|
|
|
ANSI['reset'],
|
|
|
|
))
|
|
|
|
print(' - {} entries skipped'.format(_RESULTS_TOTALS['skipped']))
|
|
|
|
print(' - {} entries updated'.format(_RESULTS_TOTALS['succeded']))
|
|
|
|
print(' - {} errors'.format(_RESULTS_TOTALS['failed']))
|
|
|
|
|
|
|
|
|
|
|
|
def update_index(export_path, resume=None, append=True):
|
|
|
|
"""handling parsing new links into the json index, returns a set of clean links"""
|
|
|
|
|
2017-10-18 22:38:17 +00:00
|
|
|
# parse an validate the export file
|
2017-10-23 09:56:21 +00:00
|
|
|
new_links = validate_links(parse_links(export_path))
|
2017-10-18 22:38:17 +00:00
|
|
|
|
|
|
|
# load existing links if archive folder is present
|
2017-10-23 09:56:21 +00:00
|
|
|
existing_links = []
|
2017-10-18 22:38:17 +00:00
|
|
|
if append:
|
|
|
|
existing_links = parse_json_links_index(HTML_FOLDER)
|
|
|
|
links = validate_links(existing_links + new_links)
|
2017-10-23 09:56:21 +00:00
|
|
|
|
2017-10-18 22:38:17 +00:00
|
|
|
|
|
|
|
# merge existing links and new links
|
|
|
|
num_new_links = len(links) - len(existing_links)
|
|
|
|
print('[*] [{}] Adding {} new links from {} to index'.format(
|
2017-07-05 21:33:51 +00:00
|
|
|
datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
2017-10-18 22:38:17 +00:00
|
|
|
num_new_links,
|
|
|
|
export_path,
|
2017-07-05 21:33:51 +00:00
|
|
|
))
|
2017-06-15 23:31:33 +00:00
|
|
|
|
2017-10-18 22:38:17 +00:00
|
|
|
# write link index html & json
|
|
|
|
write_links_index(HTML_FOLDER, links)
|
|
|
|
|
2017-10-23 09:56:21 +00:00
|
|
|
return links
|
2017-05-05 09:00:30 +00:00
|
|
|
|
2017-05-29 18:00:46 +00:00
|
|
|
|
2017-05-05 09:00:30 +00:00
|
|
|
if __name__ == '__main__':
|
2017-05-06 12:23:49 +00:00
|
|
|
argc = len(sys.argv)
|
2017-06-30 08:23:19 +00:00
|
|
|
|
|
|
|
if argc < 2 or sys.argv[1] in ('-h', '--help', 'help'):
|
2017-10-23 09:56:21 +00:00
|
|
|
print(__DESCRIPTION__)
|
2017-06-30 08:23:19 +00:00
|
|
|
print("Documentation: {}".format(__DOCUMENTATION__))
|
|
|
|
print("")
|
|
|
|
print("Usage:")
|
|
|
|
print(" ./archive.py ~/Downloads/bookmarks_export.html")
|
|
|
|
print("")
|
|
|
|
raise SystemExit(0)
|
|
|
|
|
2017-10-18 22:38:17 +00:00
|
|
|
export_path = sys.argv[1] # path to export file
|
|
|
|
resume_from = sys.argv[2] if argc > 2 else None # timestamp to resume dowloading from
|
|
|
|
|
|
|
|
if any(export_path.startswith(s) for s in ('http://', 'https://', 'ftp://')):
|
|
|
|
export_path = download_url(export_path)
|
2017-05-05 09:00:30 +00:00
|
|
|
|
2017-10-23 09:56:21 +00:00
|
|
|
links = update_index(export_path, resume=resume_from, append=True)
|
|
|
|
|
|
|
|
# make sure folder structure is sane
|
|
|
|
cleanup_archive(ARCHIVE_FOLDER, links)
|
|
|
|
update_archive(export_path, links, resume=resume_from, append=True)
|