archivebox_list.py 4.3 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. 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, reject_stdin, stderr
  24. @docstring(list_all.__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=list_all.__doc__,
  29. add_help=True,
  30. formatter_class=SmartFormatter,
  31. )
  32. group = parser.add_mutually_exclusive_group()
  33. group.add_argument(
  34. '--csv', #'-c',
  35. type=str,
  36. help="Print the output in CSV format with the given columns, e.g.: timestamp,url,extension",
  37. default=None,
  38. )
  39. group.add_argument(
  40. '--json', #'-j',
  41. action='store_true',
  42. help="Print the output in JSON format with all columns included",
  43. )
  44. group.add_argument(
  45. '--html',
  46. action='store_true',
  47. help="Print the output in HTML format"
  48. )
  49. parser.add_argument(
  50. '--with-headers',
  51. action='store_true',
  52. help='Include the headers in the output document'
  53. )
  54. parser.add_argument(
  55. '--sort', #'-s',
  56. type=str,
  57. help="List the links sorted using the given key, e.g. timestamp or updated",
  58. default=None,
  59. )
  60. parser.add_argument(
  61. '--before', #'-b',
  62. type=float,
  63. help="List only links bookmarked before (less than) the given timestamp",
  64. default=None,
  65. )
  66. parser.add_argument(
  67. '--after', #'-a',
  68. type=float,
  69. help="List only links bookmarked after (greater than or equal to) the given timestamp",
  70. default=None,
  71. )
  72. parser.add_argument(
  73. '--status',
  74. type=str,
  75. choices=('indexed', 'archived', 'unarchived', 'present', 'valid', 'invalid', 'duplicate', 'orphaned', 'corrupted', 'unrecognized'),
  76. default='indexed',
  77. help=(
  78. 'List only links or data directories that have the given status\n'
  79. f' indexed {get_indexed_folders.__doc__} (the default)\n'
  80. f' archived {get_archived_folders.__doc__}\n'
  81. f' unarchived {get_unarchived_folders.__doc__}\n'
  82. '\n'
  83. f' present {get_present_folders.__doc__}\n'
  84. f' valid {get_valid_folders.__doc__}\n'
  85. f' invalid {get_invalid_folders.__doc__}\n'
  86. '\n'
  87. f' duplicate {get_duplicate_folders.__doc__}\n'
  88. f' orphaned {get_orphaned_folders.__doc__}\n'
  89. f' corrupted {get_corrupted_folders.__doc__}\n'
  90. f' unrecognized {get_unrecognized_folders.__doc__}\n'
  91. )
  92. )
  93. parser.add_argument(
  94. '--filter-type', '-t',
  95. type=str,
  96. choices=(*LINK_FILTERS.keys(), 'search'),
  97. default='exact',
  98. help='Type of pattern matching to use when filtering URLs',
  99. )
  100. parser.add_argument(
  101. 'filter_patterns',
  102. nargs='*',
  103. type=str,
  104. default=None,
  105. help='List only URLs matching these filter patterns'
  106. )
  107. command = parser.parse_args(args or ())
  108. reject_stdin(stdin)
  109. if command.with_headers and not (command.json or command.html or command.csv):
  110. stderr(
  111. '[X] --with-headers can only be used with --json, --html or --csv options\n',
  112. color='red',
  113. )
  114. raise SystemExit(2)
  115. matching_folders = list_all(
  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)