File: ia_flag.py

package info (click to toggle)
python-internetarchive 5.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,000 kB
  • sloc: python: 7,445; xml: 180; makefile: 180
file content (103 lines) | stat: -rw-r--r-- 3,038 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
"""
ia_flag.py

'ia' subcommand for managing flags on archive.org.
"""

# Copyright (C) 2012-2025 Internet Archive
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import annotations

import argparse


def setup(subparsers):
    """Set up argument parser for the 'flag' subcommand.

    Args:
        subparsers: argparse subparsers object from main CLI
    """
    parser = subparsers.add_parser(
        "flag",
        aliases=["fl"],
        help="Manage flags",
    )
    parser.add_argument(
        "identifier",
        nargs="?",
        type=str,
        help="Identifier for the upload",
    )
    parser.add_argument(
        "-u",
        "--user",
        type=str,
        help="User associated with the flag",
    )

    group = parser.add_argument_group("Add flag operations")
    group.add_argument(
        "-a",
        "--add-flag",
        metavar="CATEGORY",
        type=str,
        help="Add a flag to the item",
    )

    group = parser.add_argument_group("Delete flag operations")
    group.add_argument(
        "-d",
        "--delete-flag",
        metavar="CATEGORY",
        type=str,
        help="Delete a flag from the item",
    )

    parser.set_defaults(func=lambda args: main(args, parser))

def main(args: argparse.Namespace, parser: argparse.ArgumentParser) -> None:
    """Handle flag subcommand execution.

    Args:
        args: Parsed command-line arguments
        parser: Argument parser for error handling
    """
    item = args.session.get_item(args.identifier)
    if args.user:
        flag_user = args.user
    else:
        flag_user = args.session.config.get("general", {}).get("screenname")
    if not flag_user.startswith('@'):
        flag_user = f"@{flag_user}"
    if args.add_flag:
        r = item.add_flag(args.add_flag, flag_user)
        j = r.json()
        if j.get("status") == "success":
            print(f"success: added '{args.add_flag}' flag by {flag_user} to {args.identifier}")
        else:
            print(f"error: {item.identifier} - {r.text}")

    elif args.delete_flag:
        r = item.delete_flag(args.delete_flag, flag_user)
        j = r.json()
        if j.get("status") == "success":
            print(f"success: deleted '{args.delete_flag}' flag by {flag_user} from {args.identifier}")
        else:
            print(f"error: {item.identifier} - {r.text}")

    else:
        r = item.get_flags()
        print(r.text)