-
Notifications
You must be signed in to change notification settings - Fork 0
/
yt
executable file
·135 lines (103 loc) · 3.53 KB
/
yt
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
#!/bin/sh
# dependencies: mpv youtube-dl dmenu
# optional: sxiv (for thumbnails)
# search videos on youtube and play them in mpv, without an API
# usage:
# yt asks for input in stdin, prompts using dmenu
#
# Thanks to sayan01
# Vouivre
# Todo
# Playlist support ?
# - in that goal remember the regexes
# - for video: '"videoRenderer":{"videoId":"\K.{11}".+?"text":".+?[^\\](?=")'
# - for playlist:'"playlistRenderer":{"playlistId":"\K.{34}?","title":{"simpleText":".+?[^\"](?=")'
# Option to download best quality ?
# Set Working directory
WK_DIR="/tmp/yt"
# Set variable DMENU if undefined
DMENU=${DMENU:-dmenu -l 15}
guicmd="$DMENU"
# regex expression to match video and playlist entries from yt result page
# grep the id and title
# return them in format id (type) title
get_results() {
echo $response | sed 's/"\}\],"accessibility"/\n/g' | grep -o '"videoRenderer":{"videoId":".\{11\}".*' | sed 's/"videoRenderer":{"videoId":"//g' |\
awk -F\" '{ print $1 "\t" $NF}'
}
get_video(){
id="$1"
# Download the file, play it and then delete it
youtube-dl -f best "$1" &
# Wait for the download to begin
sleep 1
file_downloaded=$(youtube-dl -f best --get-filename "$1")
# Play part which has already been downloaded
mpv "${file_downloaded%%.*}"*
}
get_thumb(){
id="$1"
# Download the thumbnail, display it and delete it
thumb_url=$(youtube-dl --get-thumbnail "$1");
filetype=${thumb_url##*.};
thumb_filename="thumb.$filetype"
# Use "-O" parameter for wget to force download the file, becauses a lot of
# thumbnails have the same filename
wget -O "${thumb_filename}" "$thumb_url";
sxiv "${thumb_filename}";
}
viewvideo() {
# prompt the results to user infinitely until they exit (escape)
while true; do
choice=$(echo "$1" | cut -d' ' -f2 | $guicmd -p "Video": ) # don't show id
# if esc-ed then exit
if [ -z "$choice" ]; then exit; fi
# Store ";" if given by the user Get rid of ";"
last_char=${choice#${choice%?}}
# Get rid of ";" if given
if [ "$last_char" = ";" ]
then
choice=${choice%$last_char}
fi
id=$(echo "$videoids" | grep -F "${choice}" | cut -d' ' -f1) # get id of choice
printf "%s\t(%s)\n" "$choice" "$id"
case "$last_char" in
';') get_thumb "$videolink$id";;
*) get_video "$videolink$id";;
esac
done
}
main(){
query=$(echo | $guicmd -p "Search: ")
# Check if the query is empty.
if [ -z "$query" ]; then exit; fi
# sanitise the query
query=$(echo "$query" | sed \
-e 's|+|%2B|g'\
-e 's|#|%23|g'\
-e 's|&|%26|g'\
-e 's| |+|g')
# fetch the results with the $query and
# delete all escaped characters
response="$(curl -s "https://www.youtube.com/results?search_query=$query" |\
sed 's|\\.||g')"
# If unable to fetch the youtube results page, inform and exit
# Test exit status code of grep, stored in $?
echo "$response" | grep -q "script"
if [ $? -eq 1 ]
then
echo "unable to fetch yt"
exit 1
fi
# get the list of videos/playlists and their ids in videoids and playlistids
videoids=$(get_results)
# url prefix for videos and playlists
videolink="https://youtu.be/"
# Create the directory if it doesn't exists
if [ ! -d $WK_DIR ]; then mkdir $WK_DIR; fi
# Change to the working directory
# Exit the script if the cd fails
cd $WK_DIR || exit
viewvideo "$videoids"
}
main