find-things/triple_tools/list_incoming.py
2025-12-11 20:14:32 +01:00

60 lines
1.6 KiB
Python

from __future__ import annotations
import argparse
import json
import os
import sys
from collections import defaultdict
from dump_things_pyclient.communicate import (
HTTPError,
incoming_read_labels,
incoming_read_records,
)
def _main():
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument('base_url')
argument_parser.add_argument('collection')
argument_parser.add_argument('-s', '--show-records', action='store_true', help='show the records in the inboxes as well')
arguments = argument_parser.parse_args()
curator_token = os.environ.get('CURATOR_TOKEN')
if curator_token is None:
print('ERROR: environment variable CURATOR_TOKEN not set', file=sys.stderr, flush=True)
return 1
result = {}
for label in incoming_read_labels(
service_url=arguments.base_url,
collection=arguments.collection,
token=curator_token,
):
result[label] = []
if arguments.show_records:
for record, _, _, _, _ in incoming_read_records(
service_url=arguments.base_url,
collection=arguments.collection,
label=label,
token=curator_token,
):
result[label].append(record)
if arguments.show_records is False:
result = list(result)
print(json.dumps(result, indent=2, ensure_ascii=False))
return 0
def main():
try:
return _main()
except HTTPError as e:
print(f'ERROR: {e}: {e.response.text}', file=sys.stderr, flush=True)
return 1
if __name__ == '__main__':
sys.exit(main())