archivebox_list.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #!/usr/bin/env python3
  2. __package__ = 'archivebox.cli'
  3. __command__ = 'archivebox list'
  4. import sys
  5. import argparse
  6. from typing import Optional, List, IO
  7. from ..main import list_all
  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, stderr
  23. @docstring(list_all.__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=list_all.__doc__,
  28. add_help=True,
  29. formatter_class=SmartFormatter,
  30. )
  31. group = parser.add_mutually_exclusive_group()
  32. group.add_argument(
  33. '--csv', #'-c',
  34. type=str,
  35. help="Print the output in CSV format with the given columns, e.g.: timestamp,url,extension",
  36. default=None,
  37. )
  38. group.add_argument(
  39. '--json', #'-j',
  40. action='store_true',
  41. help="Print the output in JSON format with all columns included.",
  42. )
  43. group.add_argument(
  44. '--html',
  45. action='store_true',
  46. help="Print the output in HTML format"
  47. )
  48. parser.add_argument(
  49. '--with-headers',
  50. action='store_true',
  51. help='Include the headers in the output document'
  52. )
  53. parser.add_argument(
  54. '--sort', #'-s',
  55. type=str,
  56. help="List the links sorted using the given key, e.g. timestamp or updated.",
  57. default=None,
  58. )
  59. parser.add_argument(
  60. '--before', #'-b',
  61. type=float,
  62. help="List only links bookmarked before the given timestamp.",
  63. default=None,
  64. )
  65. parser.add_argument(
  66. '--after', #'-a',
  67. type=float,
  68. help="List only links bookmarked after the given timestamp.",
  69. default=None,
  70. )
  71. parser.add_argument(
  72. '--status',
  73. type=str,
  74. choices=('indexed', 'archived', 'unarchived', 'present', 'valid', 'invalid', 'duplicate', 'orphaned', 'corrupted', 'unrecognized'),
  75. default='indexed',
  76. help=(
  77. 'List only links or data directories that have the given status\n'
  78. f' indexed {get_indexed_folders.__doc__} (the default)\n'
  79. f' archived {get_archived_folders.__doc__}\n'
  80. f' unarchived {get_unarchived_folders.__doc__}\n'
  81. '\n'
  82. f' present {get_present_folders.__doc__}\n'
  83. f' valid {get_valid_folders.__doc__}\n'
  84. f' invalid {get_invalid_folders.__doc__}\n'
  85. '\n'
  86. f' duplicate {get_duplicate_folders.__doc__}\n'
  87. f' orphaned {get_orphaned_folders.__doc__}\n'
  88. f' corrupted {get_corrupted_folders.__doc__}\n'
  89. f' unrecognized {get_unrecognized_folders.__doc__}\n'
  90. )
  91. )
  92. parser.add_argument(
  93. '--filter-type',
  94. type=str,
  95. choices=('exact', 'substring', 'domain', 'regex', 'tag', 'search'),
  96. default='exact',
  97. help='Type of pattern matching to use when filtering URLs',
  98. )
  99. parser.add_argument(
  100. 'filter_patterns',
  101. nargs='*',
  102. type=str,
  103. default=None,
  104. help='List only URLs matching these filter patterns.'
  105. )
  106. command = parser.parse_args(args or ())
  107. filter_patterns_str = accept_stdin(stdin)
  108. if command.with_headers and not (command.json or command.html or command.csv):
  109. stderr(
  110. '[X] --with-headers can only be used with --json, --html or --csv options.\n',
  111. color='red',
  112. )
  113. raise SystemExit(2)
  114. matching_folders = list_all(
  115. filter_patterns_str=filter_patterns_str,
  116. filter_patterns=command.filter_patterns,
  117. filter_type=command.filter_type,
  118. status=command.status,
  119. after=command.after,
  120. before=command.before,
  121. sort=command.sort,
  122. csv=command.csv,
  123. json=command.json,
  124. html=command.html,
  125. with_headers=command.with_headers,
  126. out_dir=pwd or OUTPUT_DIR,
  127. )
  128. raise SystemExit(not matching_folders)
  129. if __name__ == '__main__':
  130. main(args=sys.argv[1:], stdin=sys.stdin)