|
@@ -3,18 +3,18 @@ import os
|
|
|
from functools import wraps
|
|
from functools import wraps
|
|
|
from collections import defaultdict
|
|
from collections import defaultdict
|
|
|
from datetime import datetime
|
|
from datetime import datetime
|
|
|
|
|
+from stdlib_patches import run, PIPE, DEVNULL
|
|
|
|
|
|
|
|
from index import (
|
|
from index import (
|
|
|
- parse_json_link_index,
|
|
|
|
|
write_link_index,
|
|
write_link_index,
|
|
|
- update_main_index,
|
|
|
|
|
|
|
+ patch_links_index,
|
|
|
|
|
+ load_json_link_index,
|
|
|
)
|
|
)
|
|
|
from config import (
|
|
from config import (
|
|
|
CURL_BINARY,
|
|
CURL_BINARY,
|
|
|
GIT_BINARY,
|
|
GIT_BINARY,
|
|
|
WGET_BINARY,
|
|
WGET_BINARY,
|
|
|
YOUTUBEDL_BINARY,
|
|
YOUTUBEDL_BINARY,
|
|
|
- CHROME_BINARY,
|
|
|
|
|
FETCH_FAVICON,
|
|
FETCH_FAVICON,
|
|
|
FETCH_TITLE,
|
|
FETCH_TITLE,
|
|
|
FETCH_WGET,
|
|
FETCH_WGET,
|
|
@@ -25,62 +25,37 @@ from config import (
|
|
|
FETCH_WARC,
|
|
FETCH_WARC,
|
|
|
FETCH_GIT,
|
|
FETCH_GIT,
|
|
|
FETCH_MEDIA,
|
|
FETCH_MEDIA,
|
|
|
- RESOLUTION,
|
|
|
|
|
- CHECK_SSL_VALIDITY,
|
|
|
|
|
SUBMIT_ARCHIVE_DOT_ORG,
|
|
SUBMIT_ARCHIVE_DOT_ORG,
|
|
|
- COOKIES_FILE,
|
|
|
|
|
- WGET_USER_AGENT,
|
|
|
|
|
- CHROME_USER_AGENT,
|
|
|
|
|
- CHROME_USER_DATA_DIR,
|
|
|
|
|
- CHROME_HEADLESS,
|
|
|
|
|
- CHROME_SANDBOX,
|
|
|
|
|
TIMEOUT,
|
|
TIMEOUT,
|
|
|
MEDIA_TIMEOUT,
|
|
MEDIA_TIMEOUT,
|
|
|
ANSI,
|
|
ANSI,
|
|
|
- ARCHIVE_DIR,
|
|
|
|
|
|
|
+ OUTPUT_DIR,
|
|
|
GIT_DOMAINS,
|
|
GIT_DOMAINS,
|
|
|
GIT_SHA,
|
|
GIT_SHA,
|
|
|
|
|
+ WGET_USER_AGENT,
|
|
|
|
|
+ CHECK_SSL_VALIDITY,
|
|
|
|
|
+ COOKIES_FILE,
|
|
|
)
|
|
)
|
|
|
from util import (
|
|
from util import (
|
|
|
domain,
|
|
domain,
|
|
|
|
|
+ extension,
|
|
|
without_query,
|
|
without_query,
|
|
|
without_fragment,
|
|
without_fragment,
|
|
|
fetch_page_title,
|
|
fetch_page_title,
|
|
|
is_static_file,
|
|
is_static_file,
|
|
|
progress,
|
|
progress,
|
|
|
chmod_file,
|
|
chmod_file,
|
|
|
- pretty_path,
|
|
|
|
|
- print_error_hints,
|
|
|
|
|
check_link_structure,
|
|
check_link_structure,
|
|
|
wget_output_path,
|
|
wget_output_path,
|
|
|
- run, PIPE, DEVNULL,
|
|
|
|
|
|
|
+ chrome_args,
|
|
|
|
|
+)
|
|
|
|
|
+from logs import (
|
|
|
|
|
+ _LAST_RUN_STATS,
|
|
|
|
|
+ log_link_archiving_started,
|
|
|
|
|
+ log_link_archiving_failed,
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
-_RESULTS_TOTALS = { # globals are bad, mmkay
|
|
|
|
|
- 'skipped': 0,
|
|
|
|
|
- 'succeded': 0,
|
|
|
|
|
- 'failed': 0,
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-def load_link_index(link_dir, link):
|
|
|
|
|
- """check for an existing link archive in the given directory,
|
|
|
|
|
- and load+merge it into the given link dict
|
|
|
|
|
- """
|
|
|
|
|
- is_new = not os.path.exists(link_dir)
|
|
|
|
|
- if is_new:
|
|
|
|
|
- os.makedirs(link_dir)
|
|
|
|
|
- else:
|
|
|
|
|
- link = {
|
|
|
|
|
- **parse_json_link_index(link_dir),
|
|
|
|
|
- **link,
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- check_link_structure(link)
|
|
|
|
|
- print_link_status_line(link_dir, link, is_new)
|
|
|
|
|
-
|
|
|
|
|
- return link
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
class ArchiveError(Exception):
|
|
class ArchiveError(Exception):
|
|
|
def __init__(self, message, hints=None):
|
|
def __init__(self, message, hints=None):
|
|
@@ -105,32 +80,24 @@ def archive_link(link_dir, link, overwrite=True):
|
|
|
active_methods = [method for toggle, method in ARCHIVE_METHODS if toggle]
|
|
active_methods = [method for toggle, method in ARCHIVE_METHODS if toggle]
|
|
|
|
|
|
|
|
try:
|
|
try:
|
|
|
- link = load_link_index(link_dir, link)
|
|
|
|
|
|
|
+ is_new = not os.path.exists(link_dir)
|
|
|
|
|
+ if is_new:
|
|
|
|
|
+ os.makedirs(link_dir)
|
|
|
|
|
+
|
|
|
|
|
+ link = load_json_link_index(link_dir, link)
|
|
|
|
|
+ log_link_archiving_started(link_dir, link, is_new)
|
|
|
|
|
|
|
|
for archive_method in active_methods:
|
|
for archive_method in active_methods:
|
|
|
archive_method(link_dir, link, overwrite=overwrite)
|
|
archive_method(link_dir, link, overwrite=overwrite)
|
|
|
|
|
|
|
|
-
|
|
|
|
|
write_link_index(link_dir, link)
|
|
write_link_index(link_dir, link)
|
|
|
- update_main_index(link)
|
|
|
|
|
|
|
+ patch_links_index(link)
|
|
|
|
|
|
|
|
except Exception as err:
|
|
except Exception as err:
|
|
|
print(' ! Failed to archive link: {}: {}'.format(err.__class__.__name__, err))
|
|
print(' ! Failed to archive link: {}: {}'.format(err.__class__.__name__, err))
|
|
|
|
|
|
|
|
return link
|
|
return link
|
|
|
|
|
|
|
|
-def print_link_status_line(link_dir, link, is_new):
|
|
|
|
|
- print('[{symbol_color}{symbol}{reset}] [{now}] "{title}"\n {blue}{url}{reset}'.format(
|
|
|
|
|
- symbol='+' if is_new else '*',
|
|
|
|
|
- symbol_color=ANSI['green' if is_new else 'black'],
|
|
|
|
|
- now=datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
|
|
|
|
- **{**link, 'title': link['title'] or link['url']},
|
|
|
|
|
- **ANSI,
|
|
|
|
|
- ))
|
|
|
|
|
-
|
|
|
|
|
- print(' > {}{}'.format(pretty_path(link_dir), ' (new)' if is_new else ''))
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
def attach_result_to_link(method):
|
|
def attach_result_to_link(method):
|
|
|
"""
|
|
"""
|
|
@@ -178,15 +145,75 @@ def attach_result_to_link(method):
|
|
|
link['history'][method].append(history_entry)
|
|
link['history'][method].append(history_entry)
|
|
|
link['latest'][method] = result['output']
|
|
link['latest'][method] = result['output']
|
|
|
|
|
|
|
|
- _RESULTS_TOTALS[history_entry['status']] += 1
|
|
|
|
|
|
|
+ _LAST_RUN_STATS[history_entry['status']] += 1
|
|
|
|
|
|
|
|
return link
|
|
return link
|
|
|
return timed_fetch_func
|
|
return timed_fetch_func
|
|
|
return decorator
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
+@attach_result_to_link('title')
|
|
|
|
|
+def fetch_title(link_dir, link, timeout=TIMEOUT):
|
|
|
|
|
+ """try to guess the page's title from its content"""
|
|
|
|
|
+
|
|
|
|
|
+ # if link already has valid title, skip it
|
|
|
|
|
+ if link['title'] and not link['title'].lower().startswith('http'):
|
|
|
|
|
+ return {'output': link['title'], 'status': 'skipped'}
|
|
|
|
|
+
|
|
|
|
|
+ if is_static_file(link['url']):
|
|
|
|
|
+ return {'output': None, 'status': 'skipped'}
|
|
|
|
|
+
|
|
|
|
|
+ end = progress(timeout, prefix=' ')
|
|
|
|
|
+ try:
|
|
|
|
|
+ title = fetch_page_title(link['url'], timeout=timeout, progress=False)
|
|
|
|
|
+ end()
|
|
|
|
|
+ output = title
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ end()
|
|
|
|
|
+ output = e
|
|
|
|
|
+ print(' {}Failed: {} {}{}'.format(ANSI['red'], e.__class__.__name__, e, ANSI['reset']))
|
|
|
|
|
+
|
|
|
|
|
+ if title and title.strip():
|
|
|
|
|
+ link['title'] = title
|
|
|
|
|
+ output = title
|
|
|
|
|
+
|
|
|
|
|
+ return {
|
|
|
|
|
+ 'cmd': 'fetch_page_title("{}")'.format(link['url']),
|
|
|
|
|
+ 'output': output,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+@attach_result_to_link('favicon')
|
|
|
|
|
+def fetch_favicon(link_dir, link, timeout=TIMEOUT):
|
|
|
|
|
+ """download site favicon from google's favicon api"""
|
|
|
|
|
+
|
|
|
|
|
+ output = 'favicon.ico'
|
|
|
|
|
+ if os.path.exists(os.path.join(link_dir, output)):
|
|
|
|
|
+ return {'output': output, 'status': 'skipped'}
|
|
|
|
|
+
|
|
|
|
|
+ CMD = [
|
|
|
|
|
+ CURL_BINARY,
|
|
|
|
|
+ '--max-time', str(timeout),
|
|
|
|
|
+ '--location',
|
|
|
|
|
+ '--output', output,
|
|
|
|
|
+ *(() if CHECK_SSL_VALIDITY else ('--insecure',)),
|
|
|
|
|
+ 'https://www.google.com/s2/favicons?domain={}'.format(domain(link['url'])),
|
|
|
|
|
+ ]
|
|
|
|
|
+ end = progress(timeout, prefix=' ')
|
|
|
|
|
+ try:
|
|
|
|
|
+ run(CMD, stdout=PIPE, stderr=PIPE, cwd=link_dir, timeout=timeout)
|
|
|
|
|
+ end()
|
|
|
|
|
+ chmod_file(output, cwd=link_dir)
|
|
|
|
|
+ except Exception as e:
|
|
|
|
|
+ end()
|
|
|
|
|
+ output = e
|
|
|
|
|
+ print_error_hints(cmd=CMD, pwd=link_dir, err=e)
|
|
|
|
|
+
|
|
|
|
|
+ return {
|
|
|
|
|
+ 'cmd': CMD,
|
|
|
|
|
+ 'output': output,
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
@attach_result_to_link('wget')
|
|
@attach_result_to_link('wget')
|
|
|
-def fetch_wget(link_dir, link, requisites=FETCH_WGET_REQUISITES, warc=FETCH_WARC, timeout=TIMEOUT):
|
|
|
|
|
|
|
+def fetch_wget(link_dir, link, timeout=TIMEOUT):
|
|
|
"""download full site using wget"""
|
|
"""download full site using wget"""
|
|
|
|
|
|
|
|
domain_dir = os.path.join(link_dir, domain(link['url']))
|
|
domain_dir = os.path.join(link_dir, domain(link['url']))
|
|
@@ -194,7 +221,7 @@ def fetch_wget(link_dir, link, requisites=FETCH_WGET_REQUISITES, warc=FETCH_WARC
|
|
|
if os.path.exists(domain_dir) and existing_file:
|
|
if os.path.exists(domain_dir) and existing_file:
|
|
|
return {'output': existing_file, 'status': 'skipped'}
|
|
return {'output': existing_file, 'status': 'skipped'}
|
|
|
|
|
|
|
|
- if warc:
|
|
|
|
|
|
|
+ if FETCH_WARC:
|
|
|
warc_dir = os.path.join(link_dir, 'warc')
|
|
warc_dir = os.path.join(link_dir, 'warc')
|
|
|
os.makedirs(warc_dir, exist_ok=True)
|
|
os.makedirs(warc_dir, exist_ok=True)
|
|
|
warc_path = os.path.join('warc', str(int(datetime.now().timestamp())))
|
|
warc_path = os.path.join('warc', str(int(datetime.now().timestamp())))
|
|
@@ -213,8 +240,8 @@ def fetch_wget(link_dir, link, requisites=FETCH_WGET_REQUISITES, warc=FETCH_WARC
|
|
|
'-e', 'robots=off',
|
|
'-e', 'robots=off',
|
|
|
'--restrict-file-names=unix',
|
|
'--restrict-file-names=unix',
|
|
|
'--timeout={}'.format(timeout),
|
|
'--timeout={}'.format(timeout),
|
|
|
- *(() if warc else ('--timestamping',)),
|
|
|
|
|
- *(('--warc-file={}'.format(warc_path),) if warc else ()),
|
|
|
|
|
|
|
+ *(() if FETCH_WARC else ('--timestamping',)),
|
|
|
|
|
+ *(('--warc-file={}'.format(warc_path),) if FETCH_WARC else ()),
|
|
|
*(('--page-requisites',) if FETCH_WGET_REQUISITES else ()),
|
|
*(('--page-requisites',) if FETCH_WGET_REQUISITES else ()),
|
|
|
*(('--user-agent={}'.format(WGET_USER_AGENT),) if WGET_USER_AGENT else ()),
|
|
*(('--user-agent={}'.format(WGET_USER_AGENT),) if WGET_USER_AGENT else ()),
|
|
|
*(('--load-cookies', COOKIES_FILE) if COOKIES_FILE else ()),
|
|
*(('--load-cookies', COOKIES_FILE) if COOKIES_FILE else ()),
|
|
@@ -233,7 +260,8 @@ def fetch_wget(link_dir, link, requisites=FETCH_WGET_REQUISITES, warc=FETCH_WARC
|
|
|
if line.strip()
|
|
if line.strip()
|
|
|
]
|
|
]
|
|
|
|
|
|
|
|
- # parse out number of files downloaded from "Downloaded: 76 files, 4.0M in 1.6s (2.52 MB/s)"
|
|
|
|
|
|
|
+ # parse out number of files downloaded from last line of stderr:
|
|
|
|
|
+ # "Downloaded: 76 files, 4.0M in 1.6s (2.52 MB/s)"
|
|
|
files_downloaded = (
|
|
files_downloaded = (
|
|
|
int(output_tail[-1].strip().split(' ', 2)[1] or 0)
|
|
int(output_tail[-1].strip().split(' ', 2)[1] or 0)
|
|
|
if 'Downloaded:' in output_tail[-1]
|
|
if 'Downloaded:' in output_tail[-1]
|
|
@@ -263,20 +291,19 @@ def fetch_wget(link_dir, link, requisites=FETCH_WGET_REQUISITES, warc=FETCH_WARC
|
|
|
'output': output,
|
|
'output': output,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
@attach_result_to_link('pdf')
|
|
@attach_result_to_link('pdf')
|
|
|
-def fetch_pdf(link_dir, link, timeout=TIMEOUT, **chrome_kwargs):
|
|
|
|
|
|
|
+def fetch_pdf(link_dir, link, timeout=TIMEOUT):
|
|
|
"""print PDF of site to file using chrome --headless"""
|
|
"""print PDF of site to file using chrome --headless"""
|
|
|
|
|
|
|
|
if is_static_file(link['url']):
|
|
if is_static_file(link['url']):
|
|
|
- return {'output': wget_output_path(link), 'status': 'skipped'}
|
|
|
|
|
|
|
+ return {'output': None, 'status': 'skipped'}
|
|
|
|
|
|
|
|
output = 'output.pdf'
|
|
output = 'output.pdf'
|
|
|
if os.path.exists(os.path.join(link_dir, output)):
|
|
if os.path.exists(os.path.join(link_dir, output)):
|
|
|
return {'output': output, 'status': 'skipped'}
|
|
return {'output': output, 'status': 'skipped'}
|
|
|
|
|
|
|
|
CMD = [
|
|
CMD = [
|
|
|
- *chrome_headless(timeout=timeout, **chrome_kwargs),
|
|
|
|
|
|
|
+ *chrome_args(timeout=timeout),
|
|
|
'--print-to-pdf',
|
|
'--print-to-pdf',
|
|
|
link['url']
|
|
link['url']
|
|
|
]
|
|
]
|
|
@@ -302,18 +329,18 @@ def fetch_pdf(link_dir, link, timeout=TIMEOUT, **chrome_kwargs):
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@attach_result_to_link('screenshot')
|
|
@attach_result_to_link('screenshot')
|
|
|
-def fetch_screenshot(link_dir, link, timeout=TIMEOUT, **chrome_kwargs):
|
|
|
|
|
|
|
+def fetch_screenshot(link_dir, link, timeout=TIMEOUT):
|
|
|
"""take screenshot of site using chrome --headless"""
|
|
"""take screenshot of site using chrome --headless"""
|
|
|
|
|
|
|
|
if is_static_file(link['url']):
|
|
if is_static_file(link['url']):
|
|
|
- return {'output': wget_output_path(link), 'status': 'skipped'}
|
|
|
|
|
|
|
+ return {'output': None, 'status': 'skipped'}
|
|
|
|
|
|
|
|
output = 'screenshot.png'
|
|
output = 'screenshot.png'
|
|
|
if os.path.exists(os.path.join(link_dir, output)):
|
|
if os.path.exists(os.path.join(link_dir, output)):
|
|
|
return {'output': output, 'status': 'skipped'}
|
|
return {'output': output, 'status': 'skipped'}
|
|
|
|
|
|
|
|
CMD = [
|
|
CMD = [
|
|
|
- *chrome_headless(timeout=timeout, **chrome_kwargs),
|
|
|
|
|
|
|
+ *chrome_args(timeout=timeout),
|
|
|
'--screenshot',
|
|
'--screenshot',
|
|
|
link['url'],
|
|
link['url'],
|
|
|
]
|
|
]
|
|
@@ -337,18 +364,19 @@ def fetch_screenshot(link_dir, link, timeout=TIMEOUT, **chrome_kwargs):
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@attach_result_to_link('dom')
|
|
@attach_result_to_link('dom')
|
|
|
-def fetch_dom(link_dir, link, timeout=TIMEOUT, **chrome_kwargs):
|
|
|
|
|
|
|
+def fetch_dom(link_dir, link, timeout=TIMEOUT):
|
|
|
"""print HTML of site to file using chrome --dump-html"""
|
|
"""print HTML of site to file using chrome --dump-html"""
|
|
|
|
|
|
|
|
if is_static_file(link['url']):
|
|
if is_static_file(link['url']):
|
|
|
- return {'output': wget_output_path(link), 'status': 'skipped'}
|
|
|
|
|
|
|
+ return {'output': None, 'status': 'skipped'}
|
|
|
|
|
|
|
|
output = 'output.html'
|
|
output = 'output.html'
|
|
|
- if os.path.exists(os.path.join(link_dir, output)):
|
|
|
|
|
|
|
+ output_path = os.path.join(link_dir, output)
|
|
|
|
|
+ if os.path.exists(output_path):
|
|
|
return {'output': output, 'status': 'skipped'}
|
|
return {'output': output, 'status': 'skipped'}
|
|
|
|
|
|
|
|
CMD = [
|
|
CMD = [
|
|
|
- *chrome_headless(timeout=timeout, **chrome_kwargs),
|
|
|
|
|
|
|
+ *chrome_args(timeout=timeout),
|
|
|
'--dump-dom',
|
|
'--dump-dom',
|
|
|
link['url']
|
|
link['url']
|
|
|
]
|
|
]
|
|
@@ -372,100 +400,43 @@ def fetch_dom(link_dir, link, timeout=TIMEOUT, **chrome_kwargs):
|
|
|
'output': output,
|
|
'output': output,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-def parse_archive_dot_org_response(response):
|
|
|
|
|
- # Parse archive.org response headers
|
|
|
|
|
- headers = defaultdict(list)
|
|
|
|
|
-
|
|
|
|
|
- # lowercase all the header names and store in dict
|
|
|
|
|
- for header in response.splitlines():
|
|
|
|
|
- if b':' not in header or not header.strip():
|
|
|
|
|
- continue
|
|
|
|
|
- name, val = header.decode().split(':', 1)
|
|
|
|
|
- headers[name.lower().strip()].append(val.strip())
|
|
|
|
|
-
|
|
|
|
|
- # Get successful archive url in "content-location" header or any errors
|
|
|
|
|
- content_location = headers['content-location']
|
|
|
|
|
- errors = headers['x-archive-wayback-runtime-error']
|
|
|
|
|
- return content_location, errors
|
|
|
|
|
-
|
|
|
|
|
-@attach_result_to_link('archive_org')
|
|
|
|
|
-def archive_dot_org(link_dir, link, timeout=TIMEOUT):
|
|
|
|
|
- """submit site to archive.org for archiving via their service, save returned archive url"""
|
|
|
|
|
-
|
|
|
|
|
- output = 'archive.org.txt'
|
|
|
|
|
- archive_org_url = None
|
|
|
|
|
-
|
|
|
|
|
- path = os.path.join(link_dir, output)
|
|
|
|
|
- if os.path.exists(path):
|
|
|
|
|
- archive_org_url = open(path, 'r').read().strip()
|
|
|
|
|
- return {'output': archive_org_url, 'status': 'skipped'}
|
|
|
|
|
-
|
|
|
|
|
- submit_url = 'https://web.archive.org/save/{}'.format(link['url'])
|
|
|
|
|
- CMD = [
|
|
|
|
|
- CURL_BINARY,
|
|
|
|
|
- '--location',
|
|
|
|
|
- '--head',
|
|
|
|
|
- '--user-agent', 'ArchiveBox/{} (+https://github.com/pirate/ArchiveBox/)'.format(GIT_SHA), # be nice to the Archive.org people and show them where all this ArchiveBox traffic is coming from
|
|
|
|
|
- '--max-time', str(timeout),
|
|
|
|
|
- *(() if CHECK_SSL_VALIDITY else ('--insecure',)),
|
|
|
|
|
- submit_url,
|
|
|
|
|
- ]
|
|
|
|
|
- end = progress(timeout, prefix=' ')
|
|
|
|
|
- try:
|
|
|
|
|
- result = run(CMD, stdout=PIPE, stderr=DEVNULL, cwd=link_dir, timeout=timeout)
|
|
|
|
|
- end()
|
|
|
|
|
- content_location, errors = parse_archive_dot_org_response(result.stdout)
|
|
|
|
|
- if content_location:
|
|
|
|
|
- archive_org_url = 'https://web.archive.org{}'.format(content_location[0])
|
|
|
|
|
- elif len(errors) == 1 and 'RobotAccessControlException' in errors[0]:
|
|
|
|
|
- archive_org_url = None
|
|
|
|
|
- # raise ArchiveError('Archive.org denied by {}/robots.txt'.format(domain(link['url'])))
|
|
|
|
|
- elif errors:
|
|
|
|
|
- raise ArchiveError(', '.join(errors))
|
|
|
|
|
- else:
|
|
|
|
|
- raise ArchiveError('Failed to find "content-location" URL header in Archive.org response.')
|
|
|
|
|
- except Exception as e:
|
|
|
|
|
- end()
|
|
|
|
|
- output = e
|
|
|
|
|
- print_error_hints(cmd=CMD, pwd=link_dir, err=e)
|
|
|
|
|
-
|
|
|
|
|
- if not isinstance(output, Exception):
|
|
|
|
|
- # instead of writing None when archive.org rejects the url write the
|
|
|
|
|
- # url to resubmit it to archive.org. This is so when the user visits
|
|
|
|
|
- # the URL in person, it will attempt to re-archive it, and it'll show the
|
|
|
|
|
- # nicer error message explaining why the url was rejected if it fails.
|
|
|
|
|
- archive_org_url = archive_org_url or submit_url
|
|
|
|
|
- with open(os.path.join(link_dir, output), 'w', encoding='utf-8') as f:
|
|
|
|
|
- f.write(archive_org_url)
|
|
|
|
|
- chmod_file('archive.org.txt', cwd=link_dir)
|
|
|
|
|
- output = archive_org_url
|
|
|
|
|
|
|
+@attach_result_to_link('git')
|
|
|
|
|
+def fetch_git(link_dir, link, timeout=TIMEOUT):
|
|
|
|
|
+ """download full site using git"""
|
|
|
|
|
|
|
|
- return {
|
|
|
|
|
- 'cmd': CMD,
|
|
|
|
|
- 'output': output,
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ is_clonable_url = (
|
|
|
|
|
+ domain(link['url']) in GIT_DOMAINS
|
|
|
|
|
+ or extension(link['url']) == 'git'
|
|
|
|
|
+ )
|
|
|
|
|
+ if is_static_file(link['url']) or not is_clonable_url:
|
|
|
|
|
+ return {'output': None, 'status': 'skipped'}
|
|
|
|
|
|
|
|
-@attach_result_to_link('favicon')
|
|
|
|
|
-def fetch_favicon(link_dir, link, timeout=TIMEOUT):
|
|
|
|
|
- """download site favicon from google's favicon api"""
|
|
|
|
|
|
|
+ output = 'git'
|
|
|
|
|
+ output_path = os.path.join(link_dir, 'git')
|
|
|
|
|
|
|
|
- output = 'favicon.ico'
|
|
|
|
|
- if os.path.exists(os.path.join(link_dir, output)):
|
|
|
|
|
|
|
+ if os.path.exists(output_path):
|
|
|
return {'output': output, 'status': 'skipped'}
|
|
return {'output': output, 'status': 'skipped'}
|
|
|
|
|
|
|
|
|
|
+ os.makedirs(output_path, exist_ok=True)
|
|
|
CMD = [
|
|
CMD = [
|
|
|
- CURL_BINARY,
|
|
|
|
|
- '--max-time', str(timeout),
|
|
|
|
|
- '--location',
|
|
|
|
|
- '--output', output,
|
|
|
|
|
- *(() if CHECK_SSL_VALIDITY else ('--insecure',)),
|
|
|
|
|
- 'https://www.google.com/s2/favicons?domain={}'.format(domain(link['url'])),
|
|
|
|
|
|
|
+ GIT_BINARY,
|
|
|
|
|
+ 'clone',
|
|
|
|
|
+ '--mirror',
|
|
|
|
|
+ '--recursive',
|
|
|
|
|
+ *(() if CHECK_SSL_VALIDITY else ('-c', 'http.sslVerify=false')),
|
|
|
|
|
+ without_query(without_fragment(link['url'])),
|
|
|
]
|
|
]
|
|
|
end = progress(timeout, prefix=' ')
|
|
end = progress(timeout, prefix=' ')
|
|
|
try:
|
|
try:
|
|
|
- run(CMD, stdout=PIPE, stderr=PIPE, cwd=link_dir, timeout=timeout)
|
|
|
|
|
|
|
+ result = run(CMD, stdout=PIPE, stderr=PIPE, cwd=output_path, timeout=timeout + 1)
|
|
|
end()
|
|
end()
|
|
|
- chmod_file(output, cwd=link_dir)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if result.returncode == 128:
|
|
|
|
|
+ # ignore failed re-download when the folder already exists
|
|
|
|
|
+ pass
|
|
|
|
|
+ elif result.returncode > 0:
|
|
|
|
|
+ hints = 'got git response code {}:'.format(result.returncode)
|
|
|
|
|
+ raise ArchiveError('Failed git download', hints)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
end()
|
|
end()
|
|
|
output = e
|
|
output = e
|
|
@@ -476,36 +447,6 @@ def fetch_favicon(link_dir, link, timeout=TIMEOUT):
|
|
|
'output': output,
|
|
'output': output,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-@attach_result_to_link('title')
|
|
|
|
|
-def fetch_title(link_dir, link, timeout=TIMEOUT):
|
|
|
|
|
- """try to guess the page's title from its content"""
|
|
|
|
|
-
|
|
|
|
|
- # if link already has valid title, skip it
|
|
|
|
|
- if link['title'] and not link['title'].lower().startswith('http'):
|
|
|
|
|
- return {'output': link['title'], 'status': 'skipped'}
|
|
|
|
|
-
|
|
|
|
|
- if is_static_file(link['url']):
|
|
|
|
|
- return {'output': None, 'status': 'skipped'}
|
|
|
|
|
-
|
|
|
|
|
- end = progress(timeout, prefix=' ')
|
|
|
|
|
- try:
|
|
|
|
|
- title = fetch_page_title(link['url'], timeout=timeout, progress=False)
|
|
|
|
|
- end()
|
|
|
|
|
- output = title
|
|
|
|
|
- except Exception as e:
|
|
|
|
|
- end()
|
|
|
|
|
- output = e
|
|
|
|
|
- print(' {}Failed: {} {}{}'.format(ANSI['red'], e.__class__.__name__, e, ANSI['reset']))
|
|
|
|
|
-
|
|
|
|
|
- if title and title.strip():
|
|
|
|
|
- link['title'] = title
|
|
|
|
|
- output = title
|
|
|
|
|
-
|
|
|
|
|
- return {
|
|
|
|
|
- 'cmd': 'fetch_page_title("{}")'.format(link['url']),
|
|
|
|
|
- 'output': output,
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
@attach_result_to_link('media')
|
|
@attach_result_to_link('media')
|
|
|
def fetch_media(link_dir, link, timeout=MEDIA_TIMEOUT, overwrite=False):
|
|
def fetch_media(link_dir, link, timeout=MEDIA_TIMEOUT, overwrite=False):
|
|
|
"""Download playlists or individual video, audio, and subtitles using youtube-dl"""
|
|
"""Download playlists or individual video, audio, and subtitles using youtube-dl"""
|
|
@@ -569,102 +510,77 @@ def fetch_media(link_dir, link, timeout=MEDIA_TIMEOUT, overwrite=False):
|
|
|
'output': output,
|
|
'output': output,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+def parse_archive_dot_org_response(response):
|
|
|
|
|
+ # Parse archive.org response headers
|
|
|
|
|
+ headers = defaultdict(list)
|
|
|
|
|
|
|
|
-@attach_result_to_link('git')
|
|
|
|
|
-def fetch_git(link_dir, link, timeout=TIMEOUT):
|
|
|
|
|
- """download full site using git"""
|
|
|
|
|
|
|
+ # lowercase all the header names and store in dict
|
|
|
|
|
+ for header in response.splitlines():
|
|
|
|
|
+ if b':' not in header or not header.strip():
|
|
|
|
|
+ continue
|
|
|
|
|
+ name, val = header.decode().split(':', 1)
|
|
|
|
|
+ headers[name.lower().strip()].append(val.strip())
|
|
|
|
|
|
|
|
- url_is_clonable = (
|
|
|
|
|
- domain(link['url']) in GIT_DOMAINS
|
|
|
|
|
- or link['url'].endswith('.git')
|
|
|
|
|
- )
|
|
|
|
|
- if not url_is_clonable or is_static_file(link['url']):
|
|
|
|
|
- return {'output': None, 'status': 'skipped'}
|
|
|
|
|
|
|
+ # Get successful archive url in "content-location" header or any errors
|
|
|
|
|
+ content_location = headers['content-location']
|
|
|
|
|
+ errors = headers['x-archive-wayback-runtime-error']
|
|
|
|
|
+ return content_location, errors
|
|
|
|
|
|
|
|
- output = 'git'
|
|
|
|
|
- output_path = os.path.join(link_dir, 'git')
|
|
|
|
|
|
|
+@attach_result_to_link('archive_org')
|
|
|
|
|
+def archive_dot_org(link_dir, link, timeout=TIMEOUT):
|
|
|
|
|
+ """submit site to archive.org for archiving via their service, save returned archive url"""
|
|
|
|
|
|
|
|
- if os.path.exists(output_path):
|
|
|
|
|
- return {'output': output, 'status': 'skipped'}
|
|
|
|
|
|
|
+ output = 'archive.org.txt'
|
|
|
|
|
+ archive_org_url = None
|
|
|
|
|
|
|
|
- os.makedirs(output_path, exist_ok=True)
|
|
|
|
|
|
|
+ path = os.path.join(link_dir, output)
|
|
|
|
|
+ if os.path.exists(path):
|
|
|
|
|
+ archive_org_url = open(path, 'r').read().strip()
|
|
|
|
|
+ return {'output': archive_org_url, 'status': 'skipped'}
|
|
|
|
|
+
|
|
|
|
|
+ submit_url = 'https://web.archive.org/save/{}'.format(link['url'])
|
|
|
CMD = [
|
|
CMD = [
|
|
|
- GIT_BINARY,
|
|
|
|
|
- 'clone',
|
|
|
|
|
- '--mirror',
|
|
|
|
|
- '--recursive',
|
|
|
|
|
- *(() if CHECK_SSL_VALIDITY else ('-c', 'http.sslVerify=false')),
|
|
|
|
|
- without_query(without_fragment(link['url'])),
|
|
|
|
|
|
|
+ CURL_BINARY,
|
|
|
|
|
+ '--location',
|
|
|
|
|
+ '--head',
|
|
|
|
|
+ '--user-agent', 'ArchiveBox/{} (+https://github.com/pirate/ArchiveBox/)'.format(GIT_SHA), # be nice to the Archive.org people and show them where all this ArchiveBox traffic is coming from
|
|
|
|
|
+ '--max-time', str(timeout),
|
|
|
|
|
+ *(() if CHECK_SSL_VALIDITY else ('--insecure',)),
|
|
|
|
|
+ submit_url,
|
|
|
]
|
|
]
|
|
|
end = progress(timeout, prefix=' ')
|
|
end = progress(timeout, prefix=' ')
|
|
|
try:
|
|
try:
|
|
|
- result = run(CMD, stdout=PIPE, stderr=PIPE, cwd=output_path, timeout=timeout + 1)
|
|
|
|
|
|
|
+ result = run(CMD, stdout=PIPE, stderr=DEVNULL, cwd=link_dir, timeout=timeout)
|
|
|
end()
|
|
end()
|
|
|
-
|
|
|
|
|
- if result.returncode == 128:
|
|
|
|
|
- # ignore failed re-download when the folder already exists
|
|
|
|
|
- pass
|
|
|
|
|
- elif result.returncode > 0:
|
|
|
|
|
- hints = 'got git response code {}:'.format(result.returncode)
|
|
|
|
|
- raise ArchiveError('Failed git download', hints)
|
|
|
|
|
|
|
+ content_location, errors = parse_archive_dot_org_response(result.stdout)
|
|
|
|
|
+ if content_location:
|
|
|
|
|
+ archive_org_url = 'https://web.archive.org{}'.format(content_location[0])
|
|
|
|
|
+ elif len(errors) == 1 and 'RobotAccessControlException' in errors[0]:
|
|
|
|
|
+ archive_org_url = None
|
|
|
|
|
+ # raise ArchiveError('Archive.org denied by {}/robots.txt'.format(domain(link['url'])))
|
|
|
|
|
+ elif errors:
|
|
|
|
|
+ raise ArchiveError(', '.join(errors))
|
|
|
|
|
+ else:
|
|
|
|
|
+ raise ArchiveError('Failed to find "content-location" URL header in Archive.org response.')
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
end()
|
|
end()
|
|
|
output = e
|
|
output = e
|
|
|
print_error_hints(cmd=CMD, pwd=link_dir, err=e)
|
|
print_error_hints(cmd=CMD, pwd=link_dir, err=e)
|
|
|
|
|
|
|
|
|
|
+ if not isinstance(output, Exception):
|
|
|
|
|
+ # instead of writing None when archive.org rejects the url write the
|
|
|
|
|
+ # url to resubmit it to archive.org. This is so when the user visits
|
|
|
|
|
+ # the URL in person, it will attempt to re-archive it, and it'll show the
|
|
|
|
|
+ # nicer error message explaining why the url was rejected if it fails.
|
|
|
|
|
+ archive_org_url = archive_org_url or submit_url
|
|
|
|
|
+ with open(os.path.join(link_dir, output), 'w', encoding='utf-8') as f:
|
|
|
|
|
+ f.write(archive_org_url)
|
|
|
|
|
+ chmod_file('archive.org.txt', cwd=link_dir)
|
|
|
|
|
+ output = archive_org_url
|
|
|
|
|
+
|
|
|
return {
|
|
return {
|
|
|
'cmd': CMD,
|
|
'cmd': CMD,
|
|
|
'output': output,
|
|
'output': output,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-def chrome_headless(binary=CHROME_BINARY, user_data_dir=CHROME_USER_DATA_DIR, headless=CHROME_HEADLESS, sandbox=CHROME_SANDBOX, check_ssl_validity=CHECK_SSL_VALIDITY, user_agent=CHROME_USER_AGENT, resolution=RESOLUTION, timeout=TIMEOUT):
|
|
|
|
|
- global CACHED_USER_DATA_DIR
|
|
|
|
|
- user_data_dir = user_data_dir or CACHED_USER_DATA_DIR
|
|
|
|
|
- cmd_args = [binary]
|
|
|
|
|
-
|
|
|
|
|
- if headless:
|
|
|
|
|
- cmd_args += ('--headless',)
|
|
|
|
|
-
|
|
|
|
|
- if not sandbox:
|
|
|
|
|
- # dont use GPU or sandbox when running inside docker container
|
|
|
|
|
- cmd_args += ('--no-sandbox', '--disable-gpu')
|
|
|
|
|
-
|
|
|
|
|
- if not check_ssl_validity:
|
|
|
|
|
- cmd_args += ('--disable-web-security', '--ignore-certificate-errors')
|
|
|
|
|
-
|
|
|
|
|
- if user_agent:
|
|
|
|
|
- cmd_args += ('--user-agent={}'.format(user_agent),)
|
|
|
|
|
-
|
|
|
|
|
- if resolution:
|
|
|
|
|
- cmd_args += ('--window-size={}'.format(RESOLUTION),)
|
|
|
|
|
-
|
|
|
|
|
- if timeout:
|
|
|
|
|
- cmd_args += ('--timeout={}'.format((timeout) * 1000),)
|
|
|
|
|
-
|
|
|
|
|
- # Find chrome user data directory
|
|
|
|
|
- default_profile_paths = (
|
|
|
|
|
- '~/.config/chromium',
|
|
|
|
|
- '~/.config/google-chrome',
|
|
|
|
|
- '~/.config/google-chrome-beta',
|
|
|
|
|
- '~/.config/google-chrome-unstable',
|
|
|
|
|
- '~/Library/Application Support/Chromium',
|
|
|
|
|
- '~/Library/Application Support/Google/Chrome',
|
|
|
|
|
- '~/Library/Application Support/Google/Chrome Canary',
|
|
|
|
|
- '~/AppData/Local/Chromium/User Data',
|
|
|
|
|
- '~/AppData/Local/Google/Chrome/User Data',
|
|
|
|
|
- '~/AppData/Local/Google/Chrome SxS/User Data',
|
|
|
|
|
- )
|
|
|
|
|
- if user_data_dir:
|
|
|
|
|
- cmd_args.append('--user-data-dir={}'.format(user_data_dir))
|
|
|
|
|
- else:
|
|
|
|
|
- for path in default_profile_paths:
|
|
|
|
|
- full_path = os.path.expanduser(path)
|
|
|
|
|
- if os.path.exists(full_path):
|
|
|
|
|
- CACHED_USER_DATA_DIR = full_path
|
|
|
|
|
- cmd_args.append('--user-data-dir={}'.format(full_path))
|
|
|
|
|
- break
|
|
|
|
|
-
|
|
|
|
|
- return cmd_args
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
-CACHED_USER_DATA_DIR = CHROME_USER_DATA_DIR
|
|
|