json.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. __package__ = 'archivebox.legacy.storage'
  2. import os
  3. import sys
  4. import json
  5. from datetime import datetime
  6. from typing import List, Optional, Iterator
  7. from ..schema import Link, ArchiveResult
  8. from ..config import (
  9. VERSION,
  10. OUTPUT_DIR,
  11. FOOTER_INFO,
  12. GIT_SHA,
  13. DEPENDENCIES,
  14. JSON_INDEX_FILENAME,
  15. )
  16. from ..util import (
  17. enforce_types,
  18. atomic_write,
  19. )
  20. MAIN_INDEX_HEADER = {
  21. 'info': 'This is an index of site data archived by ArchiveBox: The self-hosted web archive.',
  22. 'schema': 'archivebox.legacy.storage.json',
  23. 'copyright_info': FOOTER_INFO,
  24. 'meta': {
  25. 'project': 'ArchiveBox',
  26. 'version': VERSION,
  27. 'git_sha': GIT_SHA,
  28. 'website': 'https://ArchiveBox.io',
  29. 'docs': 'https://github.com/pirate/ArchiveBox/wiki',
  30. 'source': 'https://github.com/pirate/ArchiveBox',
  31. 'issues': 'https://github.com/pirate/ArchiveBox/issues',
  32. 'dependencies': DEPENDENCIES,
  33. },
  34. }
  35. ### Main Links Index
  36. @enforce_types
  37. def parse_json_main_index(out_dir: str=OUTPUT_DIR) -> Iterator[Link]:
  38. """parse a archive index json file and return the list of links"""
  39. index_path = os.path.join(out_dir, JSON_INDEX_FILENAME)
  40. if os.path.exists(index_path):
  41. with open(index_path, 'r', encoding='utf-8') as f:
  42. links = json.load(f)['links']
  43. for link_json in links:
  44. yield Link.from_json(link_json)
  45. return ()
  46. @enforce_types
  47. def write_json_main_index(links: List[Link], out_dir: str=OUTPUT_DIR) -> None:
  48. """write the json link index to a given path"""
  49. assert isinstance(links, List), 'Links must be a list, not a generator.'
  50. assert not links or isinstance(links[0].history, dict)
  51. assert not links or isinstance(links[0].sources, list)
  52. if links and links[0].history.get('title'):
  53. assert isinstance(links[0].history['title'][0], ArchiveResult)
  54. if links and links[0].sources:
  55. assert isinstance(links[0].sources[0], str)
  56. main_index_json = {
  57. **MAIN_INDEX_HEADER,
  58. 'num_links': len(links),
  59. 'updated': datetime.now(),
  60. 'last_run_cmd': sys.argv,
  61. 'links': links,
  62. }
  63. atomic_write(main_index_json, os.path.join(out_dir, JSON_INDEX_FILENAME))
  64. ### Link Details Index
  65. @enforce_types
  66. def write_json_link_details(link: Link, out_dir: Optional[str]=None) -> None:
  67. """write a json file with some info about the link"""
  68. out_dir = out_dir or link.link_dir
  69. path = os.path.join(out_dir, JSON_INDEX_FILENAME)
  70. atomic_write(link._asdict(extended=True), path)
  71. @enforce_types
  72. def parse_json_link_details(out_dir: str) -> Optional[Link]:
  73. """load the json link index from a given directory"""
  74. existing_index = os.path.join(out_dir, JSON_INDEX_FILENAME)
  75. if os.path.exists(existing_index):
  76. with open(existing_index, 'r', encoding='utf-8') as f:
  77. link_json = json.load(f)
  78. return Link.from_json(link_json)
  79. return None