archivebox_update.py 4.2 KB

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