util.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. import os
  2. import re
  3. import sys
  4. import time
  5. import json
  6. from urllib.request import Request, urlopen
  7. from urllib.parse import urlparse
  8. from decimal import Decimal
  9. from urllib.parse import quote
  10. from datetime import datetime
  11. from subprocess import TimeoutExpired, Popen, PIPE, DEVNULL, CompletedProcess, CalledProcessError
  12. from multiprocessing import Process
  13. from config import (
  14. ANSI,
  15. IS_TTY,
  16. TERM_WIDTH,
  17. REPO_DIR,
  18. OUTPUT_DIR,
  19. SOURCES_DIR,
  20. ARCHIVE_DIR,
  21. OUTPUT_PERMISSIONS,
  22. TIMEOUT,
  23. SHOW_PROGRESS,
  24. CHECK_SSL_VALIDITY,
  25. WGET_USER_AGENT,
  26. CURL_BINARY,
  27. WGET_BINARY,
  28. CHROME_BINARY,
  29. GIT_BINARY,
  30. YOUTUBEDL_BINARY,
  31. FETCH_TITLE,
  32. FETCH_FAVICON,
  33. FETCH_WGET,
  34. FETCH_WARC,
  35. FETCH_PDF,
  36. FETCH_SCREENSHOT,
  37. FETCH_DOM,
  38. FETCH_GIT,
  39. FETCH_MEDIA,
  40. SUBMIT_ARCHIVE_DOT_ORG,
  41. )
  42. # URL helpers: https://docs.python.org/3/library/urllib.parse.html#url-parsing
  43. scheme = lambda url: urlparse(url).scheme
  44. without_scheme = lambda url: urlparse(url)._replace(scheme='').geturl().strip('//')
  45. without_query = lambda url: urlparse(url)._replace(query='').geturl().strip('//')
  46. without_fragment = lambda url: urlparse(url)._replace(fragment='').geturl().strip('//')
  47. without_path = lambda url: urlparse(url)._replace(path='', fragment='', query='').geturl().strip('//')
  48. path = lambda url: urlparse(url).path
  49. basename = lambda url: urlparse(url).path.rsplit('/', 1)[-1]
  50. domain = lambda url: urlparse(url).netloc
  51. query = lambda url: urlparse(url).query
  52. fragment = lambda url: urlparse(url).fragment
  53. extension = lambda url: basename(url).rsplit('.', 1)[-1].lower() if '.' in basename(url) else ''
  54. base_url = lambda url: without_scheme(url) # uniq base url used to dedupe links
  55. short_ts = lambda ts: ts.split('.')[0]
  56. URL_REGEX = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))[^<\""]+'
  57. HTML_TITLE_REGEX = '<title>(.[^<>]+)'
  58. def check_dependencies():
  59. """Check that all necessary dependencies are installed, and have valid versions"""
  60. python_vers = float('{}.{}'.format(sys.version_info.major, sys.version_info.minor))
  61. if python_vers < 3.5:
  62. print('{}[X] Python version is not new enough: {} (>3.5 is required){}'.format(ANSI['red'], python_vers, ANSI['reset']))
  63. print(' See https://github.com/pirate/ArchiveBox#troubleshooting for help upgrading your Python installation.')
  64. raise SystemExit(1)
  65. if FETCH_FAVICON or SUBMIT_ARCHIVE_DOT_ORG:
  66. if run(['which', CURL_BINARY], stdout=DEVNULL).returncode or run([CURL_BINARY, '--version'], stdout=DEVNULL).returncode:
  67. print('{red}[X] Missing dependency: curl{reset}'.format(**ANSI))
  68. print(' Run ./setup.sh, then confirm it was installed with: {} --version'.format(CURL_BINARY))
  69. print(' See https://github.com/pirate/ArchiveBox for help.')
  70. raise SystemExit(1)
  71. if FETCH_WGET or FETCH_WARC:
  72. if run(['which', WGET_BINARY], stdout=DEVNULL).returncode or run([WGET_BINARY, '--version'], stdout=DEVNULL).returncode:
  73. print('{red}[X] Missing dependency: wget{reset}'.format(**ANSI))
  74. print(' Run ./setup.sh, then confirm it was installed with: {} --version'.format(WGET_BINARY))
  75. print(' See https://github.com/pirate/ArchiveBox for help.')
  76. raise SystemExit(1)
  77. if FETCH_PDF or FETCH_SCREENSHOT or FETCH_DOM:
  78. if run(['which', CHROME_BINARY], stdout=DEVNULL).returncode:
  79. print('{}[X] Missing dependency: {}{}'.format(ANSI['red'], CHROME_BINARY, ANSI['reset']))
  80. print(' Run ./setup.sh, then confirm it was installed with: {} --version'.format(CHROME_BINARY))
  81. print(' See https://github.com/pirate/ArchiveBox for help.')
  82. raise SystemExit(1)
  83. # parse chrome --version e.g. Google Chrome 61.0.3114.0 canary / Chromium 59.0.3029.110 built on Ubuntu, running on Ubuntu 16.04
  84. try:
  85. result = run([CHROME_BINARY, '--version'], stdout=PIPE)
  86. version_str = result.stdout.decode('utf-8')
  87. version_lines = re.sub("(Google Chrome|Chromium) (\\d+?)\\.(\\d+?)\\.(\\d+?).*?$", "\\2", version_str).split('\n')
  88. version = [l for l in version_lines if l.isdigit()][-1]
  89. if int(version) < 59:
  90. print(version_lines)
  91. print('{red}[X] Chrome version must be 59 or greater for headless PDF, screenshot, and DOM saving{reset}'.format(**ANSI))
  92. print(' See https://github.com/pirate/ArchiveBox for help.')
  93. raise SystemExit(1)
  94. except (IndexError, TypeError, OSError):
  95. print('{red}[X] Failed to parse Chrome version, is it installed properly?{reset}'.format(**ANSI))
  96. print(' Run ./setup.sh, then confirm it was installed with: {} --version'.format(CHROME_BINARY))
  97. print(' See https://github.com/pirate/ArchiveBox for help.')
  98. raise SystemExit(1)
  99. if FETCH_GIT:
  100. if run(['which', GIT_BINARY], stdout=DEVNULL).returncode or run([GIT_BINARY, '--version'], stdout=DEVNULL).returncode:
  101. print('{red}[X] Missing dependency: git{reset}'.format(**ANSI))
  102. print(' Run ./setup.sh, then confirm it was installed with: {} --version'.format(GIT_BINARY))
  103. print(' See https://github.com/pirate/ArchiveBox for help.')
  104. raise SystemExit(1)
  105. if FETCH_MEDIA:
  106. if run(['which', YOUTUBEDL_BINARY], stdout=DEVNULL).returncode or run([YOUTUBEDL_BINARY, '--version'], stdout=DEVNULL).returncode:
  107. print('{red}[X] Missing dependency: youtube-dl{reset}'.format(**ANSI))
  108. print(' Run ./setup.sh, then confirm it was installed with: {} --version'.format(YOUTUBEDL_BINARY))
  109. print(' See https://github.com/pirate/ArchiveBox for help.')
  110. raise SystemExit(1)
  111. def chmod_file(path, cwd='.', permissions=OUTPUT_PERMISSIONS, timeout=30):
  112. """chmod -R <permissions> <cwd>/<path>"""
  113. if not os.path.exists(os.path.join(cwd, path)):
  114. raise Exception('Failed to chmod: {} does not exist (did the previous step fail?)'.format(path))
  115. chmod_result = run(['chmod', '-R', permissions, path], cwd=cwd, stdout=DEVNULL, stderr=PIPE, timeout=timeout)
  116. if chmod_result.returncode == 1:
  117. print(' ', chmod_result.stderr.decode())
  118. raise Exception('Failed to chmod {}/{}'.format(cwd, path))
  119. def progress(seconds=TIMEOUT, prefix=''):
  120. """Show a (subprocess-controlled) progress bar with a <seconds> timeout,
  121. returns end() function to instantly finish the progress
  122. """
  123. if not SHOW_PROGRESS:
  124. return lambda: None
  125. def progress_bar(seconds, prefix):
  126. """show timer in the form of progress bar, with percentage and seconds remaining"""
  127. chunk = '█' if sys.stdout.encoding == 'UTF-8' else '#'
  128. chunks = TERM_WIDTH - len(prefix) - 20 # number of progress chunks to show (aka max bar width)
  129. try:
  130. for s in range(seconds * chunks):
  131. progress = s / chunks / seconds * 100
  132. bar_width = round(progress/(100/chunks))
  133. # ████████████████████ 0.9% (1/60sec)
  134. sys.stdout.write('\r{0}{1}{2}{3} {4}% ({5}/{6}sec)'.format(
  135. prefix,
  136. ANSI['green'],
  137. (chunk * bar_width).ljust(chunks),
  138. ANSI['reset'],
  139. round(progress, 1),
  140. round(s/chunks),
  141. seconds,
  142. ))
  143. sys.stdout.flush()
  144. time.sleep(1 / chunks)
  145. # ██████████████████████████████████ 100.0% (60/60sec)
  146. sys.stdout.write('\r{0}{1}{2}{3} {4}% ({5}/{6}sec)\n'.format(
  147. prefix,
  148. ANSI['red'],
  149. chunk * chunks,
  150. ANSI['reset'],
  151. 100.0,
  152. seconds,
  153. seconds,
  154. ))
  155. sys.stdout.flush()
  156. except KeyboardInterrupt:
  157. print()
  158. pass
  159. p = Process(target=progress_bar, args=(seconds, prefix))
  160. p.start()
  161. def end():
  162. """immediately finish progress and clear the progressbar line"""
  163. # protect from double termination
  164. #if p is None or not hasattr(p, 'kill'):
  165. # return
  166. nonlocal p
  167. if p is not None:
  168. p.terminate()
  169. p = None
  170. sys.stdout.write('\r{}{}\r'.format((' ' * TERM_WIDTH), ANSI['reset'])) # clear whole terminal line
  171. sys.stdout.flush()
  172. return end
  173. def pretty_path(path):
  174. """convert paths like .../ArchiveBox/archivebox/../output/abc into output/abc"""
  175. return path.replace(REPO_DIR + '/', '')
  176. def save_source(raw_text):
  177. if not os.path.exists(SOURCES_DIR):
  178. os.makedirs(SOURCES_DIR)
  179. ts = str(datetime.now().timestamp()).split('.', 1)[0]
  180. source_path = os.path.join(SOURCES_DIR, '{}-{}.txt'.format('stdin', ts))
  181. with open(source_path, 'w', encoding='utf-8') as f:
  182. f.write(raw_text)
  183. return source_path
  184. def fetch_page_content(url, timeout=TIMEOUT):
  185. req = Request(url, headers={'User-Agent': WGET_USER_AGENT})
  186. if CHECK_SSL_VALIDITY:
  187. resp = urlopen(req, timeout=timeout)
  188. else:
  189. import ssl
  190. insecure = ssl._create_unverified_context()
  191. resp = urlopen(req, timeout=timeout, context=insecure)
  192. encoding = resp.headers.get_content_charset() or 'utf-8'
  193. return resp.read().decode(encoding)
  194. def download_url(url, timeout=TIMEOUT):
  195. """download a given url's content into downloads/domain.txt"""
  196. if not os.path.exists(SOURCES_DIR):
  197. os.makedirs(SOURCES_DIR)
  198. ts = str(datetime.now().timestamp()).split('.', 1)[0]
  199. source_path = os.path.join(SOURCES_DIR, '{}-{}.txt'.format(domain(url), ts))
  200. print('[*] [{}] Downloading {} > {}'.format(
  201. datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
  202. url,
  203. pretty_path(source_path),
  204. ))
  205. end = progress(TIMEOUT, prefix=' ')
  206. try:
  207. downloaded_xml = fetch_page_content(url, timeout=timeout)
  208. end()
  209. except Exception as e:
  210. end()
  211. print('[!] Failed to download {}\n'.format(url))
  212. print(' ', e)
  213. raise SystemExit(1)
  214. with open(source_path, 'w', encoding='utf-8') as f:
  215. f.write(downloaded_xml)
  216. return source_path
  217. def fetch_page_title(url, timeout=10, progress=SHOW_PROGRESS):
  218. """Attempt to guess a page's title by downloading the html"""
  219. if not FETCH_TITLE:
  220. return None
  221. try:
  222. if progress:
  223. sys.stdout.write('.')
  224. sys.stdout.flush()
  225. html = fetch_page_content(url, timeout=timeout)
  226. match = re.search(HTML_TITLE_REGEX, html)
  227. return match.group(1).strip() if match else None
  228. except Exception as err:
  229. # print('[!] Failed to fetch title because of {}: {}'.format(
  230. # err.__class__.__name__,
  231. # err,
  232. # ))
  233. return None
  234. def str_between(string, start, end=None):
  235. """(<abc>12345</def>, <abc>, </def>) -> 12345"""
  236. content = string.split(start, 1)[-1]
  237. if end is not None:
  238. content = content.rsplit(end, 1)[0]
  239. return content
  240. def get_link_type(link):
  241. """Certain types of links need to be handled specially, this figures out when that's the case"""
  242. if link['base_url'].endswith('.pdf'):
  243. return 'PDF'
  244. elif link['base_url'].rsplit('.', 1) in ('pdf', 'png', 'jpg', 'jpeg', 'svg', 'bmp', 'gif', 'tiff', 'webp'):
  245. return 'image'
  246. elif 'wikipedia.org' in link['domain']:
  247. return 'wiki'
  248. elif 'youtube.com' in link['domain']:
  249. return 'youtube'
  250. elif 'soundcloud.com' in link['domain']:
  251. return 'soundcloud'
  252. elif 'youku.com' in link['domain']:
  253. return 'youku'
  254. elif 'vimeo.com' in link['domain']:
  255. return 'vimeo'
  256. return None
  257. def merge_links(a, b):
  258. """deterministially merge two links, favoring longer field values over shorter,
  259. and "cleaner" values over worse ones.
  260. """
  261. longer = lambda key: (a[key] if len(a[key]) > len(b[key]) else b[key]) if (a[key] and b[key]) else (a[key] or b[key])
  262. earlier = lambda key: a[key] if a[key] < b[key] else b[key]
  263. url = longer('url')
  264. longest_title = longer('title')
  265. cleanest_title = a['title'] if '://' not in (a['title'] or '') else b['title']
  266. link = {
  267. 'timestamp': earlier('timestamp'),
  268. 'url': url,
  269. 'domain': domain(url),
  270. 'base_url': base_url(url),
  271. 'tags': longer('tags'),
  272. 'title': longest_title if '://' not in (longest_title or '') else cleanest_title,
  273. 'sources': list(set(a.get('sources', []) + b.get('sources', []))),
  274. }
  275. link['type'] = get_link_type(link)
  276. return link
  277. def find_link(folder, links):
  278. """for a given archive folder, find the corresponding link object in links"""
  279. url = parse_url(folder)
  280. if url:
  281. for link in links:
  282. if (link['base_url'] in url) or (url in link['url']):
  283. return link
  284. timestamp = folder.split('.')[0]
  285. for link in links:
  286. if link['timestamp'].startswith(timestamp):
  287. if link['domain'] in os.listdir(os.path.join(ARCHIVE_DIR, folder)):
  288. return link # careful now, this isn't safe for most ppl
  289. if link['domain'] in parse_url(folder):
  290. return link
  291. return None
  292. def parse_url(folder):
  293. """for a given archive folder, figure out what url it's for"""
  294. link_json = os.path.join(ARCHIVE_DIR, folder, 'index.json')
  295. if os.path.exists(link_json):
  296. with open(link_json, 'r') as f:
  297. try:
  298. link_json = f.read().strip()
  299. if link_json:
  300. link = json.loads(link_json)
  301. return link['base_url']
  302. except ValueError:
  303. print('File contains invalid JSON: {}!'.format(link_json))
  304. archive_org_txt = os.path.join(ARCHIVE_DIR, folder, 'archive.org.txt')
  305. if os.path.exists(archive_org_txt):
  306. with open(archive_org_txt, 'r') as f:
  307. original_link = f.read().strip().split('/http', 1)[-1]
  308. with_scheme = 'http{}'.format(original_link)
  309. return with_scheme
  310. return ''
  311. def manually_merge_folders(source, target):
  312. """prompt for user input to resolve a conflict between two archive folders"""
  313. if not IS_TTY:
  314. return
  315. fname = lambda path: path.split('/')[-1]
  316. print(' {} and {} have conflicting files, which do you want to keep?'.format(fname(source), fname(target)))
  317. print(' - [enter]: do nothing (keep both)')
  318. print(' - a: prefer files from {}'.format(source))
  319. print(' - b: prefer files from {}'.format(target))
  320. print(' - q: quit and resolve the conflict manually')
  321. try:
  322. answer = input('> ').strip().lower()
  323. except KeyboardInterrupt:
  324. answer = 'q'
  325. assert answer in ('', 'a', 'b', 'q'), 'Invalid choice.'
  326. if answer == 'q':
  327. print('\nJust run ArchiveBox again to pick up where you left off.')
  328. raise SystemExit(0)
  329. elif answer == '':
  330. return
  331. files_in_source = set(os.listdir(source))
  332. files_in_target = set(os.listdir(target))
  333. for file in files_in_source:
  334. if file in files_in_target:
  335. to_delete = target if answer == 'a' else source
  336. run(['rm', '-Rf', os.path.join(to_delete, file)])
  337. run(['mv', os.path.join(source, file), os.path.join(target, file)])
  338. if not set(os.listdir(source)):
  339. run(['rm', '-Rf', source])
  340. def fix_folder_path(archive_path, link_folder, link):
  341. """given a folder, merge it to the canonical 'correct' path for the given link object"""
  342. source = os.path.join(archive_path, link_folder)
  343. target = os.path.join(archive_path, link['timestamp'])
  344. url_in_folder = parse_url(source)
  345. if not (url_in_folder in link['base_url']
  346. or link['base_url'] in url_in_folder):
  347. raise ValueError('The link does not match the url for this folder.')
  348. if not os.path.exists(target):
  349. # target doesn't exist so nothing needs merging, simply move A to B
  350. run(['mv', source, target])
  351. else:
  352. # target folder exists, check for conflicting files and attempt manual merge
  353. files_in_source = set(os.listdir(source))
  354. files_in_target = set(os.listdir(target))
  355. conflicting_files = files_in_source & files_in_target
  356. if not conflicting_files:
  357. for file in files_in_source:
  358. run(['mv', os.path.join(source, file), os.path.join(target, file)])
  359. if os.path.exists(source):
  360. files_in_source = set(os.listdir(source))
  361. if files_in_source:
  362. manually_merge_folders(source, target)
  363. else:
  364. run(['rm', '-R', source])
  365. def migrate_data():
  366. # migrate old folder to new OUTPUT folder
  367. old_dir = os.path.join(REPO_DIR, 'html')
  368. if os.path.exists(old_dir):
  369. print('[!] WARNING: Moved old output folder "html" to new location: {}'.format(OUTPUT_DIR))
  370. run(['mv', old_dir, OUTPUT_DIR], timeout=10)
  371. def cleanup_archive(archive_path, links):
  372. """move any incorrectly named folders to their canonical locations"""
  373. # for each folder that exists, see if we can match it up with a known good link
  374. # if we can, then merge the two folders (TODO: if not, move it to lost & found)
  375. unmatched = []
  376. bad_folders = []
  377. if not os.path.exists(archive_path):
  378. return
  379. for folder in os.listdir(archive_path):
  380. try:
  381. files = os.listdir(os.path.join(archive_path, folder))
  382. except NotADirectoryError:
  383. continue
  384. if files:
  385. link = find_link(folder, links)
  386. if link is None:
  387. unmatched.append(folder)
  388. continue
  389. if folder != link['timestamp']:
  390. bad_folders.append((folder, link))
  391. else:
  392. # delete empty folders
  393. run(['rm', '-R', os.path.join(archive_path, folder)])
  394. if bad_folders and IS_TTY and input('[!] Cleanup archive? y/[n]: ') == 'y':
  395. print('[!] Fixing {} improperly named folders in archive...'.format(len(bad_folders)))
  396. for folder, link in bad_folders:
  397. fix_folder_path(archive_path, folder, link)
  398. elif bad_folders:
  399. print('[!] Warning! {} folders need to be merged, fix by running ArchiveBox.'.format(len(bad_folders)))
  400. if unmatched:
  401. print('[!] Warning! {} unrecognized folders in html/archive/'.format(len(unmatched)))
  402. print(' '+ '\n '.join(unmatched))
  403. def wget_output_path(link, look_in=None):
  404. """calculate the path to the wgetted .html file, since wget may
  405. adjust some paths to be different than the base_url path.
  406. See docs on wget --adjust-extension (-E)
  407. """
  408. # if we have it stored, always prefer the actual output path to computed one
  409. if link.get('latest', {}).get('wget'):
  410. return link['latest']['wget']
  411. urlencode = lambda s: quote(s, encoding='utf-8', errors='replace')
  412. if link['type'] in ('PDF', 'image'):
  413. return urlencode(link['base_url'])
  414. # Since the wget algorithm to for -E (appending .html) is incredibly complex
  415. # instead of trying to emulate it here, we just look in the output folder
  416. # to see what html file wget actually created as the output
  417. wget_folder = link['base_url'].rsplit('/', 1)[0].split('/')
  418. look_in = os.path.join(ARCHIVE_DIR, link['timestamp'], *wget_folder)
  419. if look_in and os.path.exists(look_in):
  420. html_files = [
  421. f for f in os.listdir(look_in)
  422. if re.search(".+\\.[Hh][Tt][Mm][Ll]?$", f, re.I | re.M)
  423. ]
  424. if html_files:
  425. return urlencode(os.path.join(*wget_folder, html_files[0]))
  426. return None
  427. # If finding the actual output file didn't work, fall back to the buggy
  428. # implementation of the wget .html appending algorithm
  429. # split_url = link['url'].split('#', 1)
  430. # query = ('%3F' + link['url'].split('?', 1)[-1]) if '?' in link['url'] else ''
  431. # if re.search(".+\\.[Hh][Tt][Mm][Ll]?$", split_url[0], re.I | re.M):
  432. # # already ends in .html
  433. # return urlencode(link['base_url'])
  434. # else:
  435. # # .html needs to be appended
  436. # without_scheme = split_url[0].split('://', 1)[-1].split('?', 1)[0]
  437. # if without_scheme.endswith('/'):
  438. # if query:
  439. # return urlencode('#'.join([without_scheme + 'index.html' + query + '.html', *split_url[1:]]))
  440. # return urlencode('#'.join([without_scheme + 'index.html', *split_url[1:]]))
  441. # else:
  442. # if query:
  443. # return urlencode('#'.join([without_scheme + '/index.html' + query + '.html', *split_url[1:]]))
  444. # elif '/' in without_scheme:
  445. # return urlencode('#'.join([without_scheme + '.html', *split_url[1:]]))
  446. # return urlencode(link['base_url'] + '/index.html')
  447. def derived_link_info(link):
  448. """extend link info with the archive urls and other derived data"""
  449. link_info = {
  450. **link,
  451. 'date': datetime.fromtimestamp(Decimal(link['timestamp'])).strftime('%Y-%m-%d %H:%M'),
  452. 'google_favicon_url': 'https://www.google.com/s2/favicons?domain={domain}'.format(**link),
  453. 'favicon_url': 'archive/{timestamp}/favicon.ico'.format(**link),
  454. 'files_url': 'archive/{timestamp}/index.html'.format(**link),
  455. 'archive_url': 'archive/{}/{}'.format(link['timestamp'], wget_output_path(link) or 'index.html'),
  456. 'pdf_link': 'archive/{timestamp}/output.pdf'.format(**link),
  457. 'screenshot_link': 'archive/{timestamp}/screenshot.png'.format(**link),
  458. 'dom_link': 'archive/{timestamp}/output.html'.format(**link),
  459. 'archive_org_url': 'https://web.archive.org/web/{base_url}'.format(**link),
  460. 'title': link['title'] or link['url'],
  461. }
  462. # PDF and images are handled slightly differently
  463. # wget, screenshot, & pdf urls all point to the same file
  464. if link['type'] in ('PDF', 'image'):
  465. link_info.update({
  466. 'archive_url': 'archive/{timestamp}/{base_url}'.format(**link),
  467. 'pdf_link': 'archive/{timestamp}/{base_url}'.format(**link),
  468. 'screenshot_link': 'archive/{timestamp}/{base_url}'.format(**link),
  469. 'dom_link': 'archive/{timestamp}/{base_url}'.format(**link),
  470. 'title': link['title'] or basename(link['url']),
  471. })
  472. return link_info
  473. def run(*popenargs, input=None, capture_output=False, timeout=None, check=False, **kwargs):
  474. """Patched of subprocess.run to fix blocking io making timeout=innefective"""
  475. if input is not None:
  476. if 'stdin' in kwargs:
  477. raise ValueError('stdin and input arguments may not both be used.')
  478. kwargs['stdin'] = PIPE
  479. if capture_output:
  480. if ('stdout' in kwargs) or ('stderr' in kwargs):
  481. raise ValueError('stdout and stderr arguments may not be used '
  482. 'with capture_output.')
  483. kwargs['stdout'] = PIPE
  484. kwargs['stderr'] = PIPE
  485. with Popen(*popenargs, **kwargs) as process:
  486. try:
  487. stdout, stderr = process.communicate(input, timeout=timeout)
  488. except TimeoutExpired:
  489. process.kill()
  490. try:
  491. stdout, stderr = process.communicate(input, timeout=2)
  492. except:
  493. pass
  494. raise TimeoutExpired(popenargs[0][0], timeout)
  495. except BaseException as err:
  496. process.kill()
  497. # We don't call process.wait() as .__exit__ does that for us.
  498. raise
  499. retcode = process.poll()
  500. if check and retcode:
  501. raise CalledProcessError(retcode, process.args,
  502. output=stdout, stderr=stderr)
  503. return CompletedProcess(process.args, retcode, stdout, stderr)
  504. def check_link_structure(link):
  505. assert isinstance(link, dict)
  506. assert isinstance(link.get('url'), str)
  507. assert len(link['url']) > 2
  508. def check_links_structure(links):
  509. assert isinstance(links, list)
  510. if links:
  511. check_link_structure(links[0])