aboutsummaryrefslogtreecommitdiff
path: root/units/sn_auth.zsh
blob: 67e1e2dabfd41deb40a24ecb4ea0b50e85445474 (plain)
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
# Copyright (C) 2024-2026 Maria Lisina
# Copyright (C) 2024-2026 Danil Lisin
# SPDX-License-Identifier: Apache-2.0

if [[ -n "${1}" ]]
then
    sn_key="${1}"
elif [[ -f "${user_config}/saucenao" ]]
then
    sn_key="$(< "${user_config}/saucenao")"
    return
elif [[ -n "${sn_key}" ]]
then
    return
else
    output_text="You must provide your SauceNAO API key before you can use SauceNAO"
    return
fi

if ! mkdir -p "${user_config}"
then
    output_text="Failed to create user config"

    log_text="sn_auth (${update_id}): ${output_text}"
    source "${units}/log.zsh"

    return
fi

if ! sn_data="$(
    curl --connect-timeout ${connrefused_timeout} \
        --data-urlencode "output_type=2" \
        --data-urlencode "api_key=${sn_key}" \
        --get \
        --max-time ${external_timeout} \
        --proxy "${external_proxy}" \
        --retry 1 \
        --retry-connrefused \
        --retry-max-time $((external_timeout - connrefused_timeout)) \
        --silent \
        --user-agent "${useragent}" \
        "https://saucenao.com/search.php"
)"
then
    output_text="Failed to access SauceNAO API"

    log_text="sn_auth (${update_id}): ${output_text}"
    source "${units}/log.zsh"

    return
fi

if ! jq -e '.' <<< "${sn_data}" > /dev/null
then
    output_text="An unknown error occurred"

    log_text="sn_auth (${update_id}): ${output_text}"
    source "${units}/log.zsh"

    return
fi

sn_status="$(jq -r '.header.status' <<< "${sn_data}")"

case "${sn_status}" in
    (-3)
        output_text="Authorized successfully"
        printf "%s\n" "${sn_key}" > "${user_config}/saucenao"
    ;;
    (-2)
        output_text="Rate limit exceeded, try again later"
    ;;
    (-1)
        output_text="Invalid API key"
    ;;
    (*)
        output_text="An unknown error occurred"
    ;;
esac