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

url_table=(
    a / 5 "https://safebooru.donmai.us/posts/.*"
    a / 3 "safebooru.donmai.us/posts/.*"
    d / 5 "https://danbooru.donmai.us/posts/.*"
    d / 3 "danbooru.donmai.us/posts/.*"
    g = 4 "https://gelbooru.com/index.php?page=post&s=view&id=.*"
    g = 4 "gelbooru.com/index.php?page=post&s=view&id=.*"
    i / 6 "https://www.idolcomplex.com/.*/posts/.*"
    i / 4 "www.idolcomplex.com/.*/posts/.*"
    i / 4 "idolcomplex.com/.*/posts/.*"
    i / 5 "https://www.idolcomplex.com/posts/.*"
    i / 3 "www.idolcomplex.com/posts/.*"
    i / 3 "idolcomplex.com/posts/.*"
    k / 6 "https://konachan.com/post/show/.*"
    k / 4 "konachan.com/post/show/.*"
    s / 6 "https://www.sankakucomplex.com/.*/posts/.*"
    s / 4 "www.sankakucomplex.com/.*/posts/.*"
    i / 4 "sankakucomplex.com/.*/posts/.*"
    s / 5 "https://www.sankakucomplex.com/posts/.*"
    s / 3 "www.sankakucomplex.com/posts/.*"
    i / 3 "sankakucomplex.com/posts/.*"
    y / 6 "https://yande.re/post/show/.*"
    y / 4 "yande.re/post/show/.*"
)

ib_mode="p"

while [[ ${#url_table} -ge 4 ]]
do
    if grep -qx "${url_table[4]}" <<< ${command}
    then
        ib_board="${url_table[1]}"
        ib_post_id="$(cut -d ${url_table[2]} -f ${url_table[3]} <<< ${command} | cut -d '?' -f 1)"

        break
    fi

    shift 4 url_table
done

if [[ -z "${ib_post_id}" ]]
then
    exit 0
fi

source "${units}/ib_config.zsh"
source "${units}/ib_authconfig.zsh"

if [[ ${#ib_post_id} -gt 32 ]]
then
    exit 0
elif [[ ${#ib_post_id} -eq 32 ]]
then
    ib_query="md5:${ib_post_id}"
else
    ib_query="id:${ib_post_id}"
fi

ib_hash="$(sha1sum <<< "${user_id}${ib_board}${ib_query}" | cut -d ' ' -f 1)"
ib_file="${cache}/${ib_hash}.json"

until mkdir "${cache}/${ib_hash}.lock"
do
    sleep 1
done

source "${units}/ib_file.zsh"

if [[ -n "${output_text}" ]]
then
    keyboard_text1="Delete"
    keyboard_data1="delete"

    reply_markup="$(
        jq --null-input --compact-output \
            --arg text1 "${keyboard_text1}" \
            --arg data1 "${keyboard_data1}" \
            '{"inline_keyboard": [[{"text": $text1, "callback_data": $data1}]]}'
    )"

    rmdir "${cache}/${ib_hash}.lock"
    return 0
fi

source "${units}/ib_post.zsh"

link_preview_options="$(
    jq --null-input --compact-output \
        --arg url "${ib_sample_url}" \
        '{"url": $url, "prefer_small_media": true, "show_above_text": true}'
)"

keyboard_text1="Post link"
keyboard_url1="${ib_url}$(urlencode <<< "${ib_id}")"
keyboard_text2="Delete"
keyboard_data2="delete"

reply_markup="$(
    jq --null-input --compact-output \
        --arg text1 "${keyboard_text1}" \
        --arg url1 "${keyboard_url1}" \
        --arg text2 "${keyboard_text2}" \
        --arg data2 "${keyboard_data2}" \
        '{"inline_keyboard": [[{"text": $text1, "url": $url1}], [{"text": $text2, "callback_data": $data2}]]}'
)"

if [[ ${#ib_tags} -gt 0 ]]
then
    keyboard_text1="Tags (${#ib_tags})"
    keyboard_data1="tags ${ib_board} ${ib_post_id}"

    reply_markup="$(
        jq --compact-output \
            --arg text1 "${keyboard_text1}" \
            --arg data1 "${keyboard_data1}" \
            '.inline_keyboard[0] += [{"text": $text1, "callback_data": $data1}]' \
        <<< "${reply_markup}"
    )"
fi

rmdir "${cache}/${ib_hash}.lock"