-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz0r
183 lines (175 loc) · 5.49 KB
/
z0r
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/bash
# Copyright (C) 2015 Nhoya.
#
# z0r is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
#text clours
readonly RED="\033[01;31m"
readonly GREEN="\033[01;32m"
readonly BLUE="\033[01;34m"
readonly YELLOW="\033[00;33m"
readonly BOLD="\033[01m"
readonly FINE="\033[0m"
ver='v1.8'
regex='((https?)*:\/\/)*[-A-Za-z0-9\+&@#/%?=~_|!:,.;]+\.[A-Za-z0-9\+&@%#/=~_|():?]{2,}'
mode=-s
dep=(curl awk xclip wget)
apikey=4e4b657a91
clip=$(xclip -o 2>/dev/null)
LOGDIR=/$HOME/.z0r_logs
#upload function (long -> short)
shr(){
url_e=$(echo $1| sed 's/?/%3F/')
short=$(curl $mode -L "http://z0r.it/yourls-api.php?signature=$apikey&action=shorturl&title=uploaded_with_z0r_script&keyword=$2&format=simply&url=$url_e")
if [ -n "$short" ]; then
if curl $mode -L "http://z0r.it/yourls-api.php?signature=$apikey&action=shorturl&title=uploaded_with_z0r_script&keyword=$2&format=simply&url=$url_e" |grep -q "Unreachable"; then
echo -e $RED"Invalid URL"$FINE
elif [ -n "$2" ]; then
if curl $mode -L "http://z0r.it/yourls-api.php?signature=$apikey&action=shorturl&title=uploaded_with_z0r_script&keyword=$2&format=simply&url=$url_e" |grep -q "already exists"; then
echo -e $RED"Custom URL already exists"$FINE
fi
else
echo "$short" && echo "$(date +"%d-%h-%Y %H.%M.%S") - $short - $1">>$LOGDIR
echo "$short" |xclip -selection clipboard
fi
else
if [ -n "$2" ]; then
echo -e $RED"custom URL already in use"$FINE
fi
fi
}
#help screen
help(){
echo -e "$YELLOW$BOLD$0$FINE $GREEN$ver$FINE, faster uploader for z0r.it shortner. Based on YOURL API"
echo "Copyright(C) 2015 Nhoya"
echo ""
echo -e $BOLD" Usage: $0 [long_URL] [custom_name]"$FINE
echo " keep empty custom_name field for normal URL format"
echo ""
echo -e $GREEN"======================================================================================"$FINE
echo " Available Options:"
echo " -h display this page"
echo " -c <short_URL> display numbers of clicks"
echo " -l show shorting history"
echo " -m <long_URL1> <long_URL2> ... mass shorting"
echo " -r reset history"
echo " -u show updates"
echo -e $GREEN"======================================================================================"$FINE
}
#stats
stats(){
lil=$(echo $OPTARG |awk -F '/' '{print$NF}')
click=$(curl -s -L "http://z0r.it/yourls-api.php?signature=$apikey&action=url-stats&format=json&shorturl=$lil"|grep -Po '"clicks":.*?[^\\]"')
echo $click
}
mss(){
for i in ${@:2}; do
mass=$(curl -s -L "http://z0r.it/yourls-api.php?signature=$apikey&action=shorturl&title=uploaded_with_z0r_script&format=simply&url=$(echo $i| sed 's/?/%3F/')")
if [ -n "$mass" ] ; then
if curl $mode -L "http://z0r.it/yourls-api.php?signature=$apikey&action=shorturl&title=uploaded_with_z0r_script&format=simply&url=$(echo $i| sed 's/?/%3F/')" |grep -q "Unreachable"; then
echo -e $RED"Invalid URL"$FINE
else
echo "$mass" && echo "$(date +"%d-%h-%Y %H.%M.%S") - $mass - $i">>$LOGDIR
fi
else
echo -e $RED"Bad Request"$FINE
fi
done
}
#control over argument
is_url(){
if [[ $1 =~ $regex ]];then
shr $1 $2
else
echo -e $RED""$1" is not a valid URL"$FINE
fi
}
#check dipendencies
checkdep(){
for i in "${dep[@]}"; do
if ( ! which $i &>/dev/null ); then
echo -e $RED"$i not found, solve the dipendencies before running the script"$FINE
missing="1"
fi
done
}
del_history(){
echo -e $RED"Are you sure? History can not be restored [y/n]"$FINE
read input
case $input in
[Yy]) echo "" > $LOGDIR
;;
*) exit 0
;;
esac
}
check_ver(){
r_ver=$(curl -s "https://api.github.com/repos/InforgeNet/z0r-script/releases" | egrep -m 1 --color 'tag_name":\s*".*"' | cut -d '"' -f 4)
if [ ! "$r_ver" = "$ver" ]; then
echo -e $BLUE"$r_ver"$FINE $GREEN"update found(https://github.com/InforgeNet/z0r-script)"$FINE
echo -e $GREEN"Want to download the $r_ver release(zipball)?[y/n]"$FINE
read input
case $input in
[Yy]) wget -q --show-progress https://github.com/InforgeNet/z0r-script/zipball/master -O $HOME/z0r-$r_ver.zip
;;
*) exit 0
;;
esac
else
echo -e $RED"No update found"$FINE
fi
}
#option parser
opsparser(){
while getopts ":hvulr:c:m:" opt; do
case "$opt" in
h) help
exit 0
;;
c) stats $OPTARG
exit 0
;;
m)
mss $@
exit 0
;;
u) check_ver
exit 0
;;
v) echo $ver
exit 0
;;
l) echo -e $GREEN"====================================================LOGs==============================================="$FINE
cat $LOGDIR
echo -e $GREEN"======================================================================================================="$FINE
exit 0
;;
r) del_history
exit 0
;;
\?) echo -e $RED"enter a valid option, use -h for help"$FINE
exit 0
;;
esac
done
}
checkdep
if [ "$missing" == "1" ]; then
exit 0
fi
opsparser $@
if [ "$#" == "0" ]; then
is_url $clip
else
is_url $1 $2
fi