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

if [[ -n "${nocommand_source}" ]]
then
    exit
fi

until mkdir "${user_config}.lock"
do
    sleep 1
done

source "${units}/sn_auth.zsh"

if [[ -d "${user_config}" ]]
then
    user_data=($(ls -1 "${user_config}"))

    if [[ ${#user_data} -eq 0 ]]
    then
        rmdir "${user_config}"
    fi
fi

rmdir "${user_config}.lock"

if [[ -n "${output_text}" ]]
then
    return
fi

source "${units}/sn_get.zsh"

if [[ -n "${output_text}" ]]
then
    return
fi

booru_table=(
    danbooru_id d "Danbooru"
    gelbooru_id g "Gelbooru"
    idol_id i "Idol Complex"
    konachan_id k "Konachan.com"
    sankaku_id s "Sankaku Channel"
    yandere_id y "yande.re"
)

external_table=(
    pixiv_id "www.pixiv.net/artworks" "Pixiv"
    tweet_id "x.com/i/status" "X (Twitter)"
)

minimum_similarity="$(jq -r ".header.minimum_similarity" <<< "${sn_data}")"

for ((idx = 0; idx >= 0; idx++))
do
    similarity="$(jq -r ".results.[${idx}].header.similarity" <<< "${sn_data}")"

    if [[ "${similarity}" == "null" ]]
    then
        break
    fi

    if [[ ${highest_similarity} -lt ${similarity} ]]
    then
        highest_similarity=${similarity}
        highest_index=${idx}
    fi
done

if [[ ${highest_similarity} -lt ${minimum_similarity} || ${highest_similarity} -lt 70.00 ]]
then
    output_text="No results found"
    return
fi

index_name="$(jq -r ".results.[${highest_index}].header.index_name" <<< "${sn_data}")"

thumbnail="$(jq -r ".results.[${highest_index}].header.thumbnail" <<< "${sn_data}")"
link_preview_options="$(
    jq --null-input --compact-output \
        --arg url "${thumbnail}" \
        '{"url": $url, "prefer_small_media": true, "show_above_text": true}'
)"

output_text="$(printf "<b>SauceNAO</b>\n<b>Similarity:</b> %.2f%%" "${highest_similarity}")"

while [[ ${#booru_table} -ge 3 ]]
do
    if ! jq -e ".results.[${highest_index}].data|has(\"${booru_table[1]}\")" <<< "${sn_data}" > /dev/null
    then
        shift 3 booru_table
        continue
    fi

    index_md5="$(cut -d '_' -f 1 <<< "${index_name##* }")"

    keyboard_text1="${booru_table[3]}"
    keyboard_query1="post ${booru_table[2]} ${index_md5}"

    reply_markup="$(
        jq --compact-output \
            --argjson offset "$(printf "%u" "${keyboard_offset}")" \
            --arg text1 "${keyboard_text1}" \
            --arg query1 "${keyboard_query1}" \
            '.inline_keyboard[$offset] += [{"text": $text1, "switch_inline_query_current_chat": $query1}]' \
        <<< "${reply_markup:-"{}"}"
    )"

    keyboard_offset=$((keyboard_offset + 0.5))
    shift 3 booru_table
done

while [[ ${#external_table} -ge 3 ]]
do
    source_id="$(jq -r ".results.[${highest_index}].data.${external_table[1]}" <<< "${sn_data}")"

    if [[ "${source_id}" == "null" ]]
    then
        shift 3 external_table
        continue
    fi

    keyboard_text1="${external_table[3]}"
    keyboard_url1="${external_table[2]}/${source_id}"

    reply_markup="$(
        jq --compact-output \
            --argjson offset "$(printf "%u" "${keyboard_offset}")" \
            --arg text1 "${keyboard_text1}" \
            --arg url1 "${keyboard_url1}" \
            '.inline_keyboard[$offset] += [{"text": $text1, "url": $url1}]' \
        <<< "${reply_markup:-"{}"}"
    )"

    keyboard_offset=$((keyboard_offset + 0.5))
    shift 3 external_table
done