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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#!/usr/bin/env python
"""
ia.py
The internetarchive module is a Python/CLI interface to Archive.org.
"""
# Copyright (C) 2012-2024 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 signal
import sys
from internetarchive import __version__, get_session
from internetarchive.cli import (
ia_account,
ia_configure,
ia_copy,
ia_delete,
ia_download,
ia_flag,
ia_list,
ia_metadata,
ia_move,
ia_reviews,
ia_search,
ia_simplelists,
ia_tasks,
ia_upload,
)
from internetarchive.cli.cli_utils import exit_on_signal
# Handle broken pipe
try:
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
except AttributeError:
# Non-unix support
pass
# Handle <Ctrl-C>
signal.signal(signal.SIGINT, exit_on_signal)
def validate_config_path(path):
"""
Validate the path to the configuration file.
Returns:
str: Validated path to the configuration file.
"""
if "configure" not in sys.argv: # Support for adding config to specific file
file_check = argparse.FileType("r")
file_check(path)
return path
def main():
"""
Main entry point for the CLI.
"""
parser = argparse.ArgumentParser(
description="A command line interface to Archive.org.",
epilog=("Documentation for 'ia' is available at:\n\n\t"
"https://archive.org/developers/internetarchive/cli.html\n\n"
"See 'ia {command} --help' for help on a specific command."),
formatter_class=argparse.RawTextHelpFormatter) # support for \n in epilog
parser.add_argument("-v", "--version",
action="version",
version=__version__)
parser.add_argument("-c", "--config-file",
action="store",
type=validate_config_path,
metavar="FILE",
help="path to configuration file")
parser.add_argument("-l", "--log",
action="store_true",
default=False,
help="enable logging")
parser.add_argument("-d", "--debug",
action="store_true",
help="enable debugging")
parser.add_argument("-i", "--insecure",
action="store_true",
help="allow insecure connections")
parser.add_argument("-H", "--host",
action="store",
help=("host to connect to "
"(doesn't work for requests made to s3.us.archive.org)"))
subparsers = parser.add_subparsers(title="commands",
dest="command",
metavar="{command}")
# Add subcommand parsers
ia_account.setup(subparsers)
ia_configure.setup(subparsers)
ia_copy.setup(subparsers)
ia_delete.setup(subparsers)
ia_download.setup(subparsers)
ia_flag.setup(subparsers)
ia_list.setup(subparsers)
ia_metadata.setup(subparsers)
ia_move.setup(subparsers)
ia_reviews.setup(subparsers)
ia_search.setup(subparsers)
ia_simplelists.setup(subparsers)
ia_tasks.setup(subparsers)
ia_upload.setup(subparsers)
# Suppress help for alias subcommands
args = parser.parse_args()
config: dict[str, dict] = {}
if args.log:
config["logging"] = {"level": "INFO"}
elif args.debug:
config["logging"] = {"level": "DEBUG"}
if args.insecure:
config["general"] = {"secure": False}
if args.host:
if config.get("general"):
config["general"]["host"] = args.host
else:
config["general"] = {"host": args.host}
args.session = get_session(config_file=args.config_file,
config=config,
debug=args.debug)
# Check if any arguments were provided
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
args.func(args)
if __name__ == "__main__":
main()
|