-
Notifications
You must be signed in to change notification settings - Fork 0
/
kona-cli
executable file
·122 lines (87 loc) · 3.67 KB
/
kona-cli
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
#!/bin/zsh
get_new(){
last_id=$(cat ~/.cache/konachan 2> /dev/null ||
curl -s "https://konachan.com/post.json" -d "page=2" | sed -nE 's_\[\{"id":([0-9]*),.*_\1_p')
latest_id=$(curl -s "https://konachan.com/post.json" | sed -nE 's_\[\{"id":([0-9]*),.*_\1_p')
if [ "$latest_id" -ne "$last_id" ]
then
# json_data=$(curl -s "https://konachan.com/post.json" -d 'limit=1000' | jq '.[]' | sed -n "/\"id\": $(printf '%s' "$last_id")/q;p" | sed '${/{/d;}')
json_data=$(curl -s "https://konachan.com/post.json" -d 'limit=1000' | tr "," "\n" | sed -n "/\"id\": $(printf '%s' "$last_id")/q;p")
printf '%s' "$latest_id" > ~/.cache/konachan &
main "$json_data"
else
printf '\n\x1B[31m%s\x1B[0m\n' "No new wallpapers have been uploaded"
fi
}
get_id(){
if [ -n "$1" ]
then
url=$(curl -s "https://konachan.com/post/show/$1" | sed -nE '/class="highres-show"/s/(.*)href="(.*)"(.*)/\2/p')
curl --create-dirs -so "${XDG_DATA_HOME:-~/.local/share}"/backgrounds/"$(printf '%s' "$url" | sed -nE 's/(.*)%20([0-9]{6})%20(.*)/\2/p')".jpg "$url"
printf '\n\x1B[32m%s\x1B[0m\n' "Wallpaper $1 has been downloaded"
else
printf '\n\x1B[31m%s\x1B[0m\n' "No id number was given, try again"
fi
}
search(){
if [ -n "$1" ] && [ -n "$2" ]
then
tag_list=$(curl -s "https://konachan.com/tag.json?limit=10" -d "order=count" -d "name=$2"|tr "{|}" "\n" |sed -nE 's_.*"name":"([^"]*)".*_\1_p')
PS3=$(printf '\n\x1B[34m%s\x1B[0m\n' "Choose your mood: ")
export PS3
select choice in $tag_list
do
# json_data=$(curl -s "https://konachan.com/post.json?limit=$1" -d "tags=$choice" | jq | sed -e '1d;$d' -e 's/\},/\}/g')
json_data=$(curl -s "https://konachan.com/post.json?limit=$1" -d "tags=$choice"|tr "{|}|," "\n")
break
done
if [ -n "$json_data" ]
then
main "$json_data"
else
printf '\n\x1B[31m%s\x1B[0m\n' "The selected tag does not have wallpapers"
fi
else
printf '\n\x1B[31m%s\x1B[0m\n' "Search string or number of wallpapers was not given, try again"
fi
}
main(){
# sample_urls=$(printf '%s' "$1" | jq '.sample_url' | tr -d '"')
sample_urls=$(printf '%s' "$json_data"|sed -nE 's@.*"sample_url":"([^"]*)".*@\1@p')
printf '\n\x1B[34m%s\x1B[0m\n' "Downloading sample wallpapers..."
for sample_url in $(printf '%s' "$sample_urls")
do
curl --create-dirs -so /tmp/konachan/"$(printf '%s' "$sample_url" | sed -nE 's/(.*)%20([0-9]{6})%20(.*)/\2/p')" "$sample_url"
done
# shellcheck disable=SC2046
dl_list=$(sxiv -to $(find /tmp/konachan -type f | sort -r) | sed -nE 's/(.*)([0-9]{6})(.*)/\2/p')
if [ -n "$dl_list" ]
then
for selected in $(printf '%s' "$dl_list")
do
urls=$(printf '%s' "$1" | jq ". | select( .id == $selected ).jpeg_url" | tr -d '"')
for url in $(printf '%s' "$urls")
do
curl --create-dirs -so "${XDG_DATA_HOME:-~/.local/share}"/backgrounds/"$(printf '%s' "$url" | sed -nE 's/(.*)%20([0-9]{6})%20(.*)/\2/p')".jpg "$url"
done
done
printf '\n\x1B[32m%s\x1B[0m\n' "Wallpapers have been downloaded"
else
printf '\n\x1B[31m%s\x1B[0m\n' "No wallpaper was selected"
fi
rm /tmp/konachan/* &> /dev/null
}
case $1 in
gn|get_new ) get_new ;;
id|get_id ) get_id "$2" ;;
se|search ) search "$2" "$3" ;;
* ) printf '%s\n' "
Usage: mv [gn|id [X]|se [Y] [Z]]
gn, get_new downloads newly uploaded wallpapers
id, get_id downloads the high res jpeg image of the given X(6 digit
unsigned integer)
se, search searches for Z(string) tag and gives you 10 related tags
sorted by posts count in descending order to choose from
and downloads Y(unsigned non-zero integer) number of latest
wallpapers belonging to the tag you chose" ;;
esac