File: ia_account.py

package info (click to toggle)
python-internetarchive 5.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,048 kB
  • sloc: python: 8,208; makefile: 180; xml: 180
file content (110 lines) | stat: -rw-r--r-- 4,140 bytes parent folder | download | duplicates (2)
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
104
105
106
107
108
109
110
"""
ia_account.py

'ia' subcommand for configuring 'ia' with your archive.org credentials.
"""

# 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/>.

import argparse
import json
import sys

from internetarchive import configure
from internetarchive.account import Account
from internetarchive.exceptions import AccountAPIError
from internetarchive.utils import is_valid_email


def setup(subparsers):
    """
    Setup args for configure command.

    Args:
        subparsers: subparser object passed from ia.py
    """
    parser = subparsers.add_parser("account",
                                   aliases=["ac"],
                                   description=(
                                       "Manage an archive.org account.\n\n"
                                       "Note: This command requires administrative "
                                       "privileges. "
                                   ),
                                   help=("Manage an archive.org account. "
                                         "Note: requires admin privileges"))

    group = parser.add_mutually_exclusive_group()
    parser.add_argument("user",
                        help="Email address, screenname, or itemname "
                             "for an archive.org account")
    group.add_argument("-g", "--get-email",
                        action="store_true",
                        help="Print the email address associated with the user and exit")
    group.add_argument("-s", "--get-screenname",
                        action="store_true",
                        help="Print the screenname associated with the user and exit")
    group.add_argument("-i", "--get-itemname",
                        action="store_true",
                        help="Print the itemname associated with the user and exit")
    group.add_argument("-l", "--is-locked",
                        action="store_true",
                        help="Check if an account is locked")
    group.add_argument("-L", "--lock",
                        action="store_true",
                        help="Lock an account")
    group.add_argument("-u", "--unlock",
                        action="store_true",
                        help="Unlock an account")

    parser.add_argument("-c", "--comment",
                        type=str,
                        help="Comment to include with lock/unlock action")

    parser.set_defaults(func=main)


def main(args: argparse.Namespace) -> None:
    """
    Main entrypoint for 'ia account'.
    """
    try:
        if args.user.startswith('@'):
            account = Account.from_account_lookup('itemname', args.user)
        elif not is_valid_email(args.user):
            account = Account.from_account_lookup('screenname', args.user)
        else:
            account = Account.from_account_lookup('email', args.user)
    except AccountAPIError as exc:
        print(json.dumps(exc.error_data))
        sys.exit(1)

    if args.get_email:
        print(account.canonical_email)
    elif args.get_screenname:
        print(account.screenname)
    elif args.get_itemname:
        print(account.itemname)
    elif args.is_locked:
        print(account.locked)
    elif args.lock:
        r = account.lock(args.comment, session=args.session)
        print(r.text)
    elif args.unlock:
        r = account.unlock(args.comment, session=args.session)
        print(r.text)
    else:
        account_data = account.to_dict()
        print(json.dumps(account_data))