-
Notifications
You must be signed in to change notification settings - Fork 0
/
wereBot
328 lines (305 loc) · 8.28 KB
/
wereBot
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/bash
VERSION=1.1
# VARIABLES
TRIES=0
CANDOWNLOAD=0
CANRESIZE=0
CANCONVERT=0
DEBUG=0
# YOUTUBE VARIABLES
YTAPI="ERR"
YTID="ERR"
YTURL="ERR"
CHID=UCAPR27YUyxmgwm3Wc2WSHLw
# URL VARIABLES
THUMBDIR=img/thumb/
YT=https://www.youtube.com/watch?v=
BACKUP=https://raw.githubusercontent.com/Luxtylo/WhoWhatWere/master/index.html
# HTML VARIABLES
HTMLDESC="UNKNOWN"
HTMLTITLE="UNKNOWN"
HTMLDATE="UNKNOWN"
HTMLTYPE="VIDEO"
# COLORS
GRAY='\033[0;37m'
CYAN='\033[0;36m'
RED='\033[0;31m'
NC='\033[0m'
# ERROR
function tryAgain(){
read -r -p "Try again? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
TRIES=$(($TRIES + 1))
init
;;
*)
exit
;;
esac
}
# RESTORE
function restore(){
if [ ! -f index_backup.html ]; then
#echo -e "${RED}WereBot could not find 'index_backup.html'"
echo -e "${CYAN}WereBot: Attempting to restore backup from GitHub...${NC}"
wget "$BACKUP" -O index.html || rm -f index.html
if [ ! -f index.html ]; then
echo -e "${RED}WereBot was unable to restore 'index.html'${NC}"
tryAgain
else
echo -e "${CYAN}WereBot: Successfully restored 'index.html'!${NC}"
sleep 2
init
fi
else
echo -e "${CYAN}WereBot: Local backup restored!${NC}"
mv index_backup.html index.html
fi
}
# WELCOME SCREEN
function welcome(){
clear
echo -e "${CYAN}--------------------"
echo "Welcome to WereBot v${VERSION}"
echo "--------------------"
echo -e "${GRAY}"
echo -e "a | auto - Auto grab the latest video and add to index."
echo -e "r | restore - Restores index.html from GitHub repository."
echo -e "d | debug - Runs script in test mode and adds empty article to index."
echo -e "${CYAN}"
}
# ASK RESTORE
function askRestore(){
read -r -p "Attempt to restore 'index.html' to current directory? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
restore
;;
*)
exit
;;
esac
}
# GET LINK
function getLink(){
echo -e "${NC}Enter YouTube URL or ID:"
read YTURL
if [ "$YTURL" != "" ]; then
if [ "$YTURL" == "auto" ] || [ "$YTURL" == "a" ]; then
getLatest
YTURL=$YTID
fi
if [ "$YTURL" == "debug" ] || [ "$YTURL" == "d" ]; then
echo -e "${CYAN}WereBot: Debug On${NC}"
DEBUG=1
linkCorrect
fi
if [ "$YTURL" == "restore" ] || [ "$YTURL" == "r" ]; then
restore
fi
if [ ${#YTURL} == 11 ]; then
echo -e "${CYAN}WereBot: Potential YouTube ID:${NC}"
echo -e "${RED}${YT}${YTURL}${NC}"
YTURL="${YT}${YTURL}"
fi
if [[ "$YTURL" == "${YT}"* ]];then
read -r -p "Select download type: (audio/video) [a/v] " response
case $response in
[vV])
echo -e "${CYAN}WereBot: Video selected!${NC}"
;;
[aA])
HTMLTYPE="AUDIO"
echo -e "${CYAN}WereBot: Audio selected!${NC}"
;;
esac
linkCorrect
else
echo -e "${CYAN}WereBot: This doesn't look like a YouTube Address.${NC}"
getLink
fi
else
echo -e "${CYAN}WereBot: No input, exiting!${NC}"
exit
fi
}
# LINK CORRECT
function linkCorrect(){
YTID=${YTURL#*?=}
API="https://www.googleapis.com/youtube/v3/videos?id=${YTID}&key=${YTAPI}&part=snippet&fields=items(snippet(title,description,publishedAt))"
if [ $DEBUG == 0 ]; then
echo -e "${CYAN}WereBot: Scraping YouTube...${NC}"
GET=$(curl -s "$API")
else
GET="title"
fi
if [[ $GET == *"title"* ]]; then
if [ $DEBUG == 0 ]; then
HTMLDATE=$(echo $GET | grep -Pom 1 '"publishedAt": "\K[^"]*')
HTMLDATE=${HTMLDATE/"T"*}
HTMLTITLE=$(echo $GET | grep -Pom 1 '"title": "\K[^"]*')
HTMLDESC=$(echo $GET | grep -Pom 1 '"description": "\K[^}]*')
HTMLDESC=${HTMLDESC::-2}
HTMLDESC=${HTMLDESC//\\n/<br>}
HTMLDESC="${HTMLDESC//\\\"/\"}"
# DISPLAY OUTPUT TO USER
echo -e "${RED}-------- VIDEO INFO --------"
echo -e "${RED}TITLE: ${GRAY}"$HTMLTITLE
echo -e "${RED}DATE: ${GRAY}"$HTMLDATE
echo -e "${RED}DESC: ${GRAY}"$HTMLDESC
echo -e "${RED}-------- VIDEO INFO --------"
fi
generate
else
echo -e "${RED}WereBot failed to connect to YouTube. Are you using a correct API key?"
tryAgain
fi
}
# GET LATEST
function getLatest(){
echo -e "${CYAN}WereBot: Auto-grabbing recent videos...${NC}"
API="https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=${CHID}&key=${YTAPI}"
GET=$(curl -s "$API")
if [[ $GET == *"title"* ]]; then
RETURN=${GET%%"snippet"*}
YTID=$(echo $RETURN | grep -Pom 1 '"videoId": "\K[^"]*')
echo -e "${CYAN}WereBot: Latest video found! ${YTID}${NC}"
else
echo -e "${RED}WereBot was unable to auto-retrieve latest YouTube video${NC}"
fi
}
# DOWNLOAD VIDEO
function getVideo(){
if [ $CANDOWNLOAD == 1 ] && [ $DEBUG == 0 ]; then
if [ "$HTMLTYPE" == "VIDEO" ]; then
echo -e "${CYAN}WereBot: Downloading video...${NC}"
youtube-dl -o videos/$YTID.webm -f 43 "$YT$YTID"
else
echo -e "${CYAN}WereBot: Downloading audio...${NC}"
youtube-dl -o videos/$YTID.ogg --extract-audio --audio-format vorbis --audio-quality 0 "$YT$YTID"
fi
else
echo -e "${CYAN}WereBot: Download skipped.${NC}"
fi
if [ "$HTMLTYPE" == "AUDIO" ] && [ $DEBUG == 0 ]; then
echo -e "${CYAN}WereBot: Downloading thumbnail...${NC}"
THUMBURL=http://img.youtube.com/vi/$YTID/maxresdefault.jpg
wget $THUMBURL -O $THUMBDIR$YTID.jpg
if [ $CANRESIZE == 1 ]; then
echo -e "${CYAN}WereBot: Optimising thumbnail...${NC}"
convert $THUMBDIR$YTID.jpg -resize 640x360 tmp_$THUMBDIR$YTID.jpg
mv tmp_$THUMBDIR$YTID.jpg $THUMBDIR$YTID.jpg
else
echo -e "${CYAN}WereBot: Thumbnail could not be resized automatically [640x360].${NC}"
fi
fi
}
# GENERATE
function generate(){
# GRAB CURRENT INDEX
echo -e "${CYAN}WereBot: Preparing index.html for new post...${NC}"
if [ ! -f index.html ]; then
echo -e "${RED}WereBot could not find 'index.html'"
tryAgain
else
template=$(<index.html)
header=${template/"-->"*/"-->"}
content=${template#*"-->"}
content="${content//<details open>/<details>}"
#if [ $DEBUG == 0 ]; then
#echo -e "${CYAN}WereBot: Making a backup...${NC}"
#mv index.html index_backup.html
#fi
echo -e "${CYAN}WereBot: Saving HTML...${NC}"
# LETS GO
{
echo -e "$header"
echo -e "<article>"
echo -e "\t<mark>$HTMLTYPE</mark>"
echo -e "\t<a href="#$YTID" name="$YTID">"
echo -e "\t\t<h2>$HTMLTITLE</h2>"
echo -e "\t</a>"
echo -e "\t<time>Published $HTMLDATE</time>"
if [ "$HTMLTYPE" == "VIDEO" ]; then
echo -e "\t<video width="640" height="360" controls>"
echo -e "\t\t<source src="videos/$YTID.webm" type="video/webm">"
else
echo -e "\t<video width="640" height="360" poster="$THUMBDIR$YTID.jpg" controls>"
echo -e "\t\t<source src="videos/$YTID.ogg" type="audio/ogg">"
fi
echo -e "\t</video>"
echo -e "\t<details open>"
echo -e "\t\t<summary>Show Notes</summary>"
echo -e "\t\t<br>"
echo -e "\t\t<p><strong>YouTube:</strong> <a href="$YT$YTID">$YT$YTID</a></p>"
echo -e "\t\t<p>"
echo -e "\t\t$HTMLDESC"
echo -e "\t\t</p>"
echo -e "\t</details>"
echo -e "</article>"
echo -e "$content"
} > index.html
getVideo
echo -e "${CYAN}WereBot: Done!${NC}"
exit
fi
}
# INIT
function init(){
welcome
# CHECK FOR youtube-dl
if ! [ -x "$(command -v youtube-dl)" ]; then
echo -e "${RED}- youtube-dl not installed. Video auto-download disabled.${CYAN}"
echo "--------------------"
else
CANDOWNLOAD=1
fi
# CHECK FOR imagemagick
if ! [ -x "$(command -v imagemagick)" ]; then
echo -e "${RED}- imagemagick not installed. Thumbnail resizing disabled.${CYAN}"
echo "--------------------"
else
CANRESIZE=1
fi
# CHECK FOR ffmpeg
if ! [ -x "$(command -v ffmpeg)" ]; then
echo -e "${RED}- ffmpeg not installed. Audio conversions to ogg may not work.${CYAN}"
echo "--------------------"
else
CANCONVERT=1
fi
# DISPLAY API GET ATTEMPTS
if [ $TRIES != 0 ]; then
echo "ATTEMPT: ${TRIES}"
fi
# GET API KEY
if [ ! -f ~/youtubeapi ]; then
echo -e "${CYAN}WereBot: Hey guys! I'm WereBot!"
echo -e "WereBot: I need a YouTube Data API Key to function!\n"
echo -e "${RED}https://console.developers.google.com/${NC}\n"
echo -e "Enter YouTube API:"
read YTAPI
echo $YTAPI > ~/youtubeapi
echo "--------------------"
else
YTAPI=$(<~/youtubeapi)
fi
# CHECK API THEN DISPLAY
if [ $YTAPI != "ERR" ]; then
echo -e "${CYAN}API KEY: "$YTAPI
echo -e "--------------------${NC}"
getLink
else
echo -e "${RED}WereBot encountered an error while obtaining your API key."
tryAgain
fi
}
# CHECK FOR INDEX, THEN RUN
welcome
if [ ! -f index.html ]; then
echo -e "${RED}WereBot could not find 'index.html'"
askRestore
else
init
fi