archivebox_update.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python3
  2. __package__ = 'archivebox.cli'
  3. __command__ = 'archivebox update'
  4. import sys
  5. import argparse
  6. from typing import List, Optional, IO
  7. from ..main import update
  8. from ..util import docstring
  9. from ..config import OUTPUT_DIR
  10. from ..index import (
  11. LINK_FILTERS,
  12. get_indexed_folders,
  13. get_archived_folders,
  14. get_unarchived_folders,
  15. get_present_folders,
  16. get_valid_folders,
  17. get_invalid_folders,
  18. get_duplicate_folders,
  19. get_orphaned_folders,
  20. get_corrupted_folders,
  21. get_unrecognized_folders,
  22. )
  23. from ..logging_util import SmartFormatter, accept_stdin
  24. @docstring(update.__doc__)
  25. def main(args: Optional[List[str]]=None, stdin: Optional[IO]=None, pwd: Optional[str]=None) -> None:
  26. parser = argparse.ArgumentParser(
  27. prog=__command__,
  28. description=update.__doc__,
  29. add_help=True,
  30. formatter_class=SmartFormatter,
  31. )
  32. parser.add_argument(
  33. '--only-new', #'-n',
  34. action='store_true',
  35. help="Don't attempt to retry previously skipped/failed links when updating",
  36. )
  37. parser.add_argument(
  38. '--index-only', #'-o',
  39. action='store_true',
  40. help="Update the main index without archiving any content",
  41. )
  42. parser.add_argument(
  43. '--resume', #'-r',
  44. type=float,
  45. help='Resume the update process from a given timestamp',
  46. default=None,
  47. )
  48. parser.add_argument(
  49. '--overwrite', #'-x',
  50. action='store_true',
  51. help='Ignore existing archived content and overwrite with new versions (DANGEROUS)',
  52. )
  53. parser.add_argument(
  54. '--before', #'-b',
  55. type=float,
  56. help="Update only links bookmarked before the given timestamp.",
  57. default=None,
  58. )
  59. parser.add_argument(
  60. '--after', #'-a',
  61. type=float,
  62. help="Update only links bookmarked after the given timestamp.",
  63. default=None,
  64. )
  65. parser.add_argument(
  66. '--status',
  67. type=str,
  68. choices=('indexed', 'archived', 'unarchived', 'present', 'valid', 'invalid', 'duplicate', 'orphaned', 'corrupted', 'unrecognized'),
  69. default='indexed',
  70. help=(
  71. 'Update only links or data directories that have the given status\n'
  72. f' indexed {get_indexed_folders.__doc__} (the default)\n'
  73. f' archived {get_archived_folders.__doc__}\n'
  74. f' unarchived {get_unarchived_folders.__doc__}\n'
  75. '\n'
  76. f' present {get_present_folders.__doc__}\n'
  77. f' valid {get_valid_folders.__doc__}\n'
  78. f' invalid {get_invalid_folders.__doc__}\n'
  79. '\n'
  80. f' duplicate {get_duplicate_folders.__doc__}\n'
  81. f' orphaned {get_orphaned_folders.__doc__}\n'
  82. f' corrupted {get_corrupted_folders.__doc__}\n'
  83. f' unrecognized {get_unrecognized_folders.__doc__}\n'
  84. )
  85. )
  86. parser.add_argument(
  87. '--filter-type', '-t',
  88. type=str,
  89. choices=(*LINK_FILTERS.keys(), 'search'),
  90. default='exact',
  91. help='Type of pattern matching to use when filtering URLs',
  92. )
  93. parser.add_argument(
  94. 'filter_patterns',
  95. nargs='*',
  96. type=str,
  97. default=None,
  98. help='Update only URLs matching these filter patterns.'
  99. )
  100. parser.add_argument(
  101. "--extract",
  102. type=str,
  103. help="Pass a list of the extractors to be used. If the method name is not correct, it will be ignored. \
  104. This does not take precedence over the configuration",
  105. default=""
  106. )
  107. command = parser.parse_args(args or ())
  108. filter_patterns_str = None
  109. if not command.filter_patterns:
  110. filter_patterns_str = accept_stdin(stdin)
  111. update(
  112. resume=command.resume,
  113. only_new=command.only_new,
  114. index_only=command.index_only,
  115. overwrite=command.overwrite,
  116. filter_patterns_str=filter_patterns_str,
  117. filter_patterns=command.filter_patterns,
  118. filter_type=command.filter_type,
  119. status=command.status,
  120. after=command.after,
  121. before=command.before,
  122. out_dir=pwd or OUTPUT_DIR,
  123. extractors=command.extract,
  124. )
  125. if __name__ == '__main__':
  126. main(args=sys.argv[1:], stdin=sys.stdin)