2019-04-03 04:27:37 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
__package__ = 'archivebox.cli'
|
|
|
|
__command__ = 'archivebox list'
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import argparse
|
|
|
|
|
2019-04-27 21:26:24 +00:00
|
|
|
from typing import Optional, List, IO
|
|
|
|
|
2020-07-24 17:25:25 +00:00
|
|
|
from ..main import list_all
|
|
|
|
from ..util import docstring
|
2019-04-27 21:26:24 +00:00
|
|
|
from ..config import OUTPUT_DIR
|
|
|
|
from ..index import (
|
2021-02-16 01:43:36 +00:00
|
|
|
LINK_FILTERS,
|
2019-04-24 15:40:10 +00:00
|
|
|
get_indexed_folders,
|
|
|
|
get_archived_folders,
|
|
|
|
get_unarchived_folders,
|
|
|
|
get_present_folders,
|
|
|
|
get_valid_folders,
|
|
|
|
get_invalid_folders,
|
|
|
|
get_duplicate_folders,
|
|
|
|
get_orphaned_folders,
|
|
|
|
get_corrupted_folders,
|
|
|
|
get_unrecognized_folders,
|
|
|
|
)
|
2021-02-16 06:20:47 +00:00
|
|
|
from ..logging_util import SmartFormatter, reject_stdin, stderr
|
2019-04-03 04:27:37 +00:00
|
|
|
|
2019-05-01 03:10:48 +00:00
|
|
|
|
|
|
|
@docstring(list_all.__doc__)
|
2019-04-27 21:26:24 +00:00
|
|
|
def main(args: Optional[List[str]]=None, stdin: Optional[IO]=None, pwd: Optional[str]=None) -> None:
|
2019-04-03 04:27:37 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog=__command__,
|
2019-05-01 03:10:48 +00:00
|
|
|
description=list_all.__doc__,
|
2019-04-03 04:27:37 +00:00
|
|
|
add_help=True,
|
2019-04-24 15:40:10 +00:00
|
|
|
formatter_class=SmartFormatter,
|
2019-04-03 04:27:37 +00:00
|
|
|
)
|
|
|
|
group = parser.add_mutually_exclusive_group()
|
|
|
|
group.add_argument(
|
|
|
|
'--csv', #'-c',
|
|
|
|
type=str,
|
|
|
|
help="Print the output in CSV format with the given columns, e.g.: timestamp,url,extension",
|
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
group.add_argument(
|
|
|
|
'--json', #'-j',
|
|
|
|
action='store_true',
|
|
|
|
help="Print the output in JSON format with all columns included.",
|
|
|
|
)
|
2020-08-19 18:02:12 +00:00
|
|
|
group.add_argument(
|
|
|
|
'--html',
|
|
|
|
action='store_true',
|
|
|
|
help="Print the output in HTML format"
|
|
|
|
)
|
2020-08-19 17:32:25 +00:00
|
|
|
parser.add_argument(
|
2020-09-08 14:17:10 +00:00
|
|
|
'--with-headers',
|
2020-08-19 17:32:25 +00:00
|
|
|
action='store_true',
|
2020-09-08 14:17:10 +00:00
|
|
|
help='Include the headers in the output document'
|
2020-08-19 17:32:25 +00:00
|
|
|
)
|
2019-04-03 04:27:37 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--sort', #'-s',
|
|
|
|
type=str,
|
2019-04-11 11:00:26 +00:00
|
|
|
help="List the links sorted using the given key, e.g. timestamp or updated.",
|
2019-04-03 04:27:37 +00:00
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--before', #'-b',
|
|
|
|
type=float,
|
2019-04-24 15:40:10 +00:00
|
|
|
help="List only links bookmarked before the given timestamp.",
|
2019-04-03 04:27:37 +00:00
|
|
|
default=None,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--after', #'-a',
|
|
|
|
type=float,
|
2019-04-24 15:40:10 +00:00
|
|
|
help="List only links bookmarked after the given timestamp.",
|
2019-04-03 04:27:37 +00:00
|
|
|
default=None,
|
|
|
|
)
|
2019-04-24 15:40:10 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'--status',
|
|
|
|
type=str,
|
|
|
|
choices=('indexed', 'archived', 'unarchived', 'present', 'valid', 'invalid', 'duplicate', 'orphaned', 'corrupted', 'unrecognized'),
|
|
|
|
default='indexed',
|
|
|
|
help=(
|
|
|
|
'List only links or data directories that have the given status\n'
|
|
|
|
f' indexed {get_indexed_folders.__doc__} (the default)\n'
|
|
|
|
f' archived {get_archived_folders.__doc__}\n'
|
|
|
|
f' unarchived {get_unarchived_folders.__doc__}\n'
|
|
|
|
'\n'
|
|
|
|
f' present {get_present_folders.__doc__}\n'
|
|
|
|
f' valid {get_valid_folders.__doc__}\n'
|
|
|
|
f' invalid {get_invalid_folders.__doc__}\n'
|
|
|
|
'\n'
|
|
|
|
f' duplicate {get_duplicate_folders.__doc__}\n'
|
|
|
|
f' orphaned {get_orphaned_folders.__doc__}\n'
|
|
|
|
f' corrupted {get_corrupted_folders.__doc__}\n'
|
|
|
|
f' unrecognized {get_unrecognized_folders.__doc__}\n'
|
|
|
|
)
|
|
|
|
)
|
2019-04-11 11:00:26 +00:00
|
|
|
parser.add_argument(
|
2021-02-16 01:43:36 +00:00
|
|
|
'--filter-type', '-t',
|
2019-04-11 11:00:26 +00:00
|
|
|
type=str,
|
2021-02-16 01:43:36 +00:00
|
|
|
choices=(*LINK_FILTERS.keys(), 'search'),
|
2019-04-11 11:00:26 +00:00
|
|
|
default='exact',
|
|
|
|
help='Type of pattern matching to use when filtering URLs',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2019-04-27 21:26:24 +00:00
|
|
|
'filter_patterns',
|
2019-04-11 11:00:26 +00:00
|
|
|
nargs='*',
|
|
|
|
type=str,
|
|
|
|
default=None,
|
|
|
|
help='List only URLs matching these filter patterns.'
|
|
|
|
)
|
2019-04-27 21:26:24 +00:00
|
|
|
command = parser.parse_args(args or ())
|
2021-02-16 06:20:47 +00:00
|
|
|
reject_stdin(stdin)
|
2019-04-03 04:27:37 +00:00
|
|
|
|
2020-09-08 14:17:10 +00:00
|
|
|
if command.with_headers and not (command.json or command.html or command.csv):
|
2020-08-19 18:14:04 +00:00
|
|
|
stderr(
|
2020-09-08 14:17:10 +00:00
|
|
|
'[X] --with-headers can only be used with --json, --html or --csv options.\n',
|
2020-08-19 18:14:04 +00:00
|
|
|
color='red',
|
|
|
|
)
|
|
|
|
raise SystemExit(2)
|
|
|
|
|
2019-05-01 03:11:07 +00:00
|
|
|
matching_folders = list_all(
|
2019-04-27 21:26:24 +00:00
|
|
|
filter_patterns=command.filter_patterns,
|
2019-04-11 11:00:26 +00:00
|
|
|
filter_type=command.filter_type,
|
2019-04-27 21:26:24 +00:00
|
|
|
status=command.status,
|
2019-04-03 04:27:37 +00:00
|
|
|
after=command.after,
|
2019-04-27 21:26:24 +00:00
|
|
|
before=command.before,
|
|
|
|
sort=command.sort,
|
|
|
|
csv=command.csv,
|
|
|
|
json=command.json,
|
2020-08-19 18:02:12 +00:00
|
|
|
html=command.html,
|
2020-09-08 14:17:10 +00:00
|
|
|
with_headers=command.with_headers,
|
2019-04-27 21:26:24 +00:00
|
|
|
out_dir=pwd or OUTPUT_DIR,
|
2019-04-03 04:27:37 +00:00
|
|
|
)
|
2019-05-01 03:11:07 +00:00
|
|
|
raise SystemExit(not matching_folders)
|
2019-04-24 15:40:10 +00:00
|
|
|
|
2019-04-03 04:27:37 +00:00
|
|
|
if __name__ == '__main__':
|
2019-04-27 21:26:24 +00:00
|
|
|
main(args=sys.argv[1:], stdin=sys.stdin)
|