archivebox_remove.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env python3
  2. __package__ = 'archivebox.cli'
  3. __command__ = 'archivebox remove'
  4. __description__ = 'Remove the specified URLs from the archive.'
  5. import sys
  6. import argparse
  7. from ..legacy.config import check_data_folder
  8. from ..legacy.util import reject_stdin
  9. from ..legacy.main import remove_archive_links
  10. def main(args=None):
  11. check_data_folder()
  12. args = sys.argv[1:] if args is None else args
  13. parser = argparse.ArgumentParser(
  14. prog=__command__,
  15. description=__description__,
  16. add_help=True,
  17. )
  18. parser.add_argument(
  19. '--yes', # '-y',
  20. action='store_true',
  21. help='Remove links instantly without prompting to confirm.',
  22. )
  23. parser.add_argument(
  24. '--delete', # '-r',
  25. action='store_true',
  26. help=(
  27. "In addition to removing the link from the index, "
  28. "also delete its archived content and metadata folder."
  29. ),
  30. )
  31. parser.add_argument(
  32. '--before', #'-b',
  33. type=float,
  34. help="List only URLs bookmarked before the given timestamp.",
  35. default=None,
  36. )
  37. parser.add_argument(
  38. '--after', #'-a',
  39. type=float,
  40. help="List only URLs bookmarked after the given timestamp.",
  41. default=None,
  42. )
  43. parser.add_argument(
  44. '--filter-type',
  45. type=str,
  46. choices=('exact', 'substring', 'domain', 'regex'),
  47. default='exact',
  48. help='Type of pattern matching to use when filtering URLs',
  49. )
  50. parser.add_argument(
  51. 'pattern',
  52. nargs='*',
  53. type=str,
  54. default=None,
  55. help='URLs matching this filter pattern will be removed from the index.'
  56. )
  57. command = parser.parse_args(args)
  58. if not sys.stdin.isatty():
  59. stdin_raw_text = sys.stdin.read()
  60. if stdin_raw_text and command.url:
  61. print(
  62. '[X] You should pass either a pattern as an argument, '
  63. 'or pass a list of patterns via stdin, but not both.\n'
  64. )
  65. raise SystemExit(1)
  66. patterns = [pattern.strip() for pattern in stdin_raw_text.split('\n')]
  67. else:
  68. patterns = command.pattern
  69. remove_archive_links(
  70. filter_patterns=patterns, filter_type=command.filter_type,
  71. before=command.before, after=command.after,
  72. yes=command.yes, delete=command.delete,
  73. )
  74. if __name__ == '__main__':
  75. main()