File: ia_configure.py

package info (click to toggle)
python-internetarchive 3.3.0-2~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,096 kB
  • sloc: python: 6,276; xml: 180; makefile: 180
file content (99 lines) | stat: -rw-r--r-- 4,133 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
#
# The internetarchive module is a Python/CLI interface to Archive.org.
#
# Copyright (C) 2012-2019 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/>.

"""Configure 'ia' with your Archive.org credentials.

usage:
    ia configure
    ia configure --username=<username> --password=<password>
    ia configure --print-cookies
    ia configure --netrc
    ia configure [--help]

options:
    -h, --help
    -u, --username=<username>  Provide username as an option rather than
                               providing it interactively.
    -p, --password=<password>  Provide password as an option rather than
                               providing it interactively.
    -n, --netrc                Use netrc file for login.
    -c, --print-cookies        Print archive.org logged-in-* cookies.
"""
from __future__ import annotations

import netrc
import sys

from docopt import docopt

from internetarchive import ArchiveSession, configure
from internetarchive.exceptions import AuthenticationError


def main(argv: list[str], session: ArchiveSession) -> None:
    args = docopt(__doc__, argv=argv)
    if args['--print-cookies']:
        user = session.config.get('cookies', {}).get('logged-in-user')
        sig = session.config.get('cookies', {}).get('logged-in-sig')
        if not user or not sig:
            if not user and not sig:
                print('error: "logged-in-user" and "logged-in-sig" cookies '
                      'not found in config file, try reconfiguring.', file=sys.stderr)
            elif not user:
                print('error: "logged-in-user" cookie not found in config file, '
                      'try reconfiguring.', file=sys.stderr)
            elif not sig:
                print('error: "logged-in-sig" cookie not found in config file, '
                      'try reconfiguring.', file=sys.stderr)
            sys.exit(1)
        print(f'logged-in-user={user}; logged-in-sig={sig}')
        sys.exit()
    try:
        # CLI params.
        if args['--username'] and args['--password']:
            config_file_path = configure(args['--username'],
                                         args['--password'],
                                         config_file=session.config_file,
                                         host=session.host)
            print(f'Config saved to: {config_file_path}', file=sys.stderr)

        # Netrc
        elif args['--netrc']:
            print("Configuring 'ia' with netrc file...", file=sys.stderr)
            try:
                n = netrc.netrc()
            except netrc.NetrcParseError as exc:
                print('error: netrc.netrc() cannot parse your .netrc file.', file=sys.stderr)
                sys.exit(1)
            username, _, password = n.hosts['archive.org']
            config_file_path = configure(username,
                                         password or "",
                                         config_file=session.config_file,
                                         host=session.host)
            print(f'Config saved to: {config_file_path}', file=sys.stderr)

        # Interactive input.
        else:
            print("Enter your Archive.org credentials below to configure 'ia'.\n")
            config_file_path = configure(config_file=session.config_file,
                                         host=session.host)
            print(f'\nConfig saved to: {config_file_path}')

    except AuthenticationError as exc:
        print(f'\nerror: {exc}', file=sys.stderr)
        sys.exit(1)