Browse Source

Add header mode (e.g. for tasks API)

master
JustAnotherArchivist 1 year ago
parent
commit
5eae0c45c2
1 changed files with 13 additions and 3 deletions
  1. +13
    -3
      curl-ia

+ 13
- 3
curl-ia View File

@@ -1,11 +1,15 @@
#!/bin/bash
if [[ "$#" -eq 0 || "$1" == '-h' || "$1" == '--help' ]]
if [[ "$#" -eq 0 || ( "$1" != 'cookie' && "$1" != 'header' ) ]]
then
echo 'Usage: curl-ia CURL-ARGUMENTS...' >&2
echo 'Usage: curl-ia MODE CURL-ARGUMENTS...' >&2
echo 'Runs curl with the cookies from `ia`'"'s"' config file' >&2
echo "MODE must be either cookie or header; in the latter case, the 'Authorization' header is added to *all* requests!" >&2
exit 1
fi

mode="$1"
shift

# Logic for the config file location is the same as upstream: the first one of $IA_CONFIG_FILE, ${XDG_CONFIG_HOME}/internetarchive/ia.ini, ~/.config/ia.ini, ~/.ia that exists is used.
if [[ -z "${XDG_CONFIG_HOME}" || "${XDG_CONFIG_HOME}" != /* || ! -d "${XDG_CONFIG_HOME}" ]]
then
@@ -24,4 +28,10 @@ then
exit 1
fi

exec curl --cookie <(python3 -c 'import configparser, sys; config = configparser.RawConfigParser(); config.read(sys.argv[1]); {print(f".archive.org\tTRUE\t/\tFALSE\t0\t{k}\t{v}") for k, v in config["cookies"].items()}' "${path}") "$@"
if [[ "${mode}" == 'cookie' ]]
then
exec curl --cookie <(python3 -c 'import configparser, sys; config = configparser.RawConfigParser(); config.read(sys.argv[1]); {print(f".archive.org\tTRUE\t/\tFALSE\t0\t{k}\t{v}") for k, v in config["cookies"].items()}' "${path}") "$@"
else
header="$(python3 -c 'import configparser, sys; config = configparser.RawConfigParser(); config.read(sys.argv[1]); print("LOW " + config["s3"]["access"] + ":" + config["s3"]["secret"])' "${path}")"
exec curl --header "Authorization: ${header}" "$@"
fi

Loading…
Cancel
Save