forked from alansarkar/Youtube-viewer-Bash-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yt_backup.sh
executable file
·273 lines (211 loc) · 6.88 KB
/
yt_backup.sh
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
#!/bin/env bash
umask 0034
unalias -a
#unset $GROUPS
unset PATH
export PATH=$PATH:/usr/bin/:~/.local/bin:~/my\ scripts
rm -rf $imputdir
player="/usr/bin/mpv --really-quiet -border=no"
#player="/bin/vlc"
imageviewer="tiv" #-h 40 -w 40 "
#sandbox=/bin/firejail
#f='--noroot --private-cache --quiet --noroot --nonewprivs --seccomp ' ## firejail config for this script
scraper="/bin/wget"
scrap_flag='--user-agent="$useragent" -qO - ' #scrap_flag=' -s -H "User-Agent: $useragent" -A "$useragent" '
#useragent="`random.exe`"
useragent="Mozilla/5.0 (X11; Linux x86_64; rv:52.9) Gecko/20100101 Firefox/52.9 (Pale Moon)"
sandbox_flag="$sandbox $f "
scraper_flag="$scraper $scrap_flag"
searchlink="https://youtube.com/results?search_query="
watchlink="https://www.youtube.com/watch?v="
#####################################################################################
case "$@" in
--help|-h)
echo "Usage: yt.sh
Youtube Viewer
Just execute this script and Enter search terms at prompt
And select the number to play when the search results shown
Options:
--help Show this message and exit
--history Shows previous search history
--clear clears search history
--image enables image
"
;;
--history)
if [ -d ~/.config/youtube\ bash\ script ] && [ -f ~/.config/youtube\ bash\ script/history ] ; then
cat ~/.config/"youtube bash script/history"
else
echo " No history to show "
fi
;;
--clear)
if [ -f ~/.config/youtube\ bash\ script/history ] ; then
rm ~/.config/"youtube bash script/history"
fi
;;
""|--image)
########################### variables ##################
color1="tput setaf 15" #white
color2='tput setaf 05' #pink
color3='tput setaf 6' #blue
color4='tput setaf 11' #yelow
color5='tput setaf 1' #red
color6='tput setaf 14'
## 0 disable 1 to enable
if [ "$1" == "--image" ] ; then imageenable=0 ; else imageenable=0; fi
#### image folder #####
imputdir=$HOME/.ytimg
imputfile=$HOME/.ytimgs
cleaning_cache
clear
re=1
#redo"
###################################################
########### function #############
function parsing()
{
## name
cat /tmp/.ytcache | grep '{"accessibilityData":{"label":"' -F | sed 's/"}],"accessibility":{"accessibilityData":{"label":"/\n/g' | cut -d' ' -f1-10 | tail -n +2 > /tmp/.ytname
## link
cat /tmp/.ytcache | grep '","webPageType' | sed 's/\",\"webPageType/\n/g' | grep watch?v | sed 's/.*\/watch?v=//g' | cut -d' ' -f1 > /tmp/.ytlink
}
function clearing_cache()
{
rm -rf /tmp/.ytcache
rm -rf /tmp/.ytlink
rm -rf /tmp/.ytname
rm -rf $imputfile
}
function startup_name() {
echo $($color1) "*********youtube script **********$(tput sgr 0)"
echo $($color2) " by alan sarkar$(tput sgr 0)"
echo $($color1) "*********************************$(tput sgr 0)"
echo $($color3) " Enter what you want to search:$(tput sgr 0)"
}
function image_parsing()
{
if [ -d $imputdir ]
then
ypeg=$(expr $y - 1 )
if [ $y -eq 1 ]; then $imageviewer "$imputdir/$(cat $imputfile | cut -c 36-50 | head -1 | tail -1)";
else $imageviewer "$imputdir/$(cat $imputfile | cut -c 36-50 | head -$y | tail -1).$ypeg"; fi
fi
}
function history() {
[ ! -d $HOME/.config/"youtube bash script" ] && touch $HOME/.config/"youtube bash script" ;
echo "$@" >> $HOME/.config/"youtube bash script/history"
}
#########################
while [ $re != q ]
do
# startup_name
read -p "Insert query: " input1 ;
x="$input1"
clear
########## pulling name ######
xyz="`echo "$x" | sed 's/ /+/g'`"
echo "$( $sandbox_flag $scraper_flag "$searchlink"$xyz"&spfreeload=10")" > /tmp/.ytcache
#### imagescrape
#if [ $imageenable -eq 1 ] ; then mkdir $imputdir ; fi
cat /tmp/.ytcache | sed 's/thumb/\n/g' | grep jpg | cut -c 17-64 | uniq -u | grep http |sed 's/jpg.*/jpg/g' > "$imputfile"
if [ $imageenable -eq 1 ] ; then $sandbox_flag /usr/bin/wget --user-agent="$useragent" -q --input-file=$imputfile -P $imputdir/ ; fi
re2=1
while [ $re2 -eq 1 ]
do
y=1
parsing
z=$(cat /tmp/.ytname | wc -l )
while [ $y -le $z ]
do
printf " $($color3) $y. "
## echo name
echo $($color4) "$(cat /tmp/.ytname |head -$y | tail -1)$(tput sgr 0)"
## echo link :
cat /tmp/.ytlink | head -$y | tail -1
# image_parsing
y=$( expr $y + 1 )
done
echo $($color3) "Enter the number you want to watch or enter q to exit $(tput sgr 0)"
echo "$($color3) Enter n to go to other pages"$(tput sgr 0)
read input2 ;
p="$input2" ;
if [ "$p" != n ] ;then clear ; fi
if [[ "$p" != q ]] && [[ "$p" != n ]] && [[ "$p" =~ ^[0-9]+$ ]]
then
q=$(cat /tmp/.ytlink | head -$p | tail -1 )
clear
show_title="`cat /tmp/.ytname | head -$p | tail -1`"
echo $($color1) " Now Playing: "
echo " "
echo $($color4) "$show_title$(tput sgr 0)"
echo $($color4) "$(echo "Link: "$watchlink"$q")$(tput sgr 0)"
echo " "
echo $($color1) " Description: $(tput sgr 0)"
################## discreaption ######################
echo $($color6) "$( $sandbox_flag $scraper_flag "$watchlink$q" | grep '},\"description\":{\"simpleText\":\"' | sed 's/.*},\"description\":{\"simpleText\":\"//g;s/"},"lengthSeconds":".*//g;s/\\n/\n/g' )$(tput sgr 0)"
#fi
echo ""
##### video play ###########
#$HOME/my\ scripts/mpdl.sh "$watchlink$q"
# list quality
echo ""
$sandbox youtube-dl -F "$watchlink$q"
echo ""
read -p "Choose quality number: " qual ;
$sandbox youtube-dl -f $qual -q --user-agent "$useragent" -c "$watchlink$q" -o - | $player -
history "$show_title: $watchlink$q "
mpv=1 # for conflict
#clear
fi
########################
## exit ##
if [ "$p" = q ]
then
clear
re2=0
clearing_cache
fi
######## next page ######
if [ "$p" = n ]
then
echo $(tput sgr 15)"Enter the page number$(tput sgr 0)"
read input3
xx="$input3"
clear
if [ "$xx" != 1 ] && [[ "$xx" =~ ^[0-9]+$ ]]
then
xyz="`echo "$x" | sed 's/ /+/g'`"
echo "$( $sandbox_flag $scraper_flag "$searchlink"$xyz"&pbjreload=101&page=$xx" )" > /tmp/.ytcache
parsing
# image_parsing
page=$(cat /tmp/.ytname | head -$xx | tail -1 )
rage="$x&sp$page"
echo $($color2)"page no $(expr $xx + 1)$(tput sgr 0)"
echo "URL: $searchlink="$rage" "
echo ""
fi
fi
#######################
############# search again ##############
if (([ "$p" != n ] && [ "$p" != q ]) && [ "$mpv" != 1 ])
then
pqr=="`echo "$p" | sed 's/ /+/g'`"
echo "$( $sandbox_flag $scraper_flag "$searchlink"$pqr"&spfreeload=10")" > /tmp/.ytcache
rm -rf $imputdir
#### imagescrape
#if [ $imageenable -eq 1 ] ; then mkdir $imputdir ; fi
cat /tmp/.ytcache | sed 's/thumb/\n/g' | grep jpg | cut -c 17-64 | uniq -u | grep http |sed 's/jpg.*/jpg/g' > "$imputfile"
if [ $imageenable -eq 1 ] ; then $sandbox_flag $scraper_flag -q --input-file=$imputfile -P $imputdir/ ; fi
#########
x="$p"
fi
#######################
mpv=0 # reset conflict var
#clear
done
clear
done
clearing_cache
;;
esac