/
repomenue_youtube_subs
executable file
·269 lines (235 loc) · 6.97 KB
/
repomenue_youtube_subs
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
#!/usr/bin/env bash
#-*-coding:utf-8 -*-
#Auto updated?
# Yes
#File:
# repomenue_youtube_subs
#Author:
# The-Repo-Club [wayne6324@gmail.com]
#Github:
# https://github.com/The-Repo-Club/
#
#Created:
# Sun 03 January 2021, 05:09:33 PM [GMT]
#Modified:
# Fri 09 September 2022, 08:14:11 PM [GMT+1]
#
#Description:
# Watch your youtube subscriptions without a youtube account
# via curl, repomenu, browser and basic unix commands.
#
# The $SUBS_FILE is a text file containing usernames or channel IDs
# comments and blank lines are ignored.
#
#
#Dependencies:
# repomenu
#
repomenus() {
repomenu -wm -c -w 750 -l 20 -p "Youtube Subs:"
}
# -/-/-/-/- Settings -/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/
: "${SUBS_FILE:=${HOME}/.config/repomenu/configs/subs.ini}"
: "${SUBS_MENU_PROG:=repomenus}"
: "${SUBS:=${HOME}/.cache/subs}"
: "${SUBS_LINKS:=$SUBS/links}"
: "${SUBS_OPEN:=$(repo-exec videoplayer)}"
: "${SUBS_CACHE:=$SUBS/cache}"
: "${SUBS_SLEEP_VALUE:=0.05}" # raise this if you experience problems
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
SEP=^^^^^ # shouldn't need to change this
# -/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/
die() {
printf >&2 '%s\n' "$*"
exit 1
}
usage() {
die 'Usage: repomenue_youtube_subs [-c cat_subs] [-g gen_links] [-u update_subs] [-d daemonize]'
}
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
# Synopsis: $SUBS_FILE [txt] -> $SUBS_LINKS [xml links]
#
# Updates local cache of xml subscription links from the
# subscription file containing either usernames or channel ids.
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
gen_links() {
: >"$SUBS_LINKS"
count=0
total=$(sed -e '/^$/d' -e '/^#/d' <"$SUBS_FILE" | wc -l)
while read -r line; do
# ignore comments and blank lines
case $line in '' | ' ' | '#'*) continue ;; esac
# strip off in-line comments and any trailing whitespace
line=${line%%#*}
line=${line%% *}
count=$((count + 1))
case $line in
UC*)
# YT channel IDs always begin with 'UC' and are 24 chars long
printf "[%s/%s] using channel ID '%s' for xml link\n" "$count" "$total" "$line"
[ ${#line} -eq 24 ] &&
printf 'https://youtube.com/feeds/videos.xml?%s\n' \
"channel_id=$line" >>"$SUBS_LINKS"
;;
*)
# otherwise we are given a username, we must find out its channel ID
printf "fetching channel ID for %s...\n" "$line"
curl -sfL --retry 10 "https://youtube.com/user/$line/about" |
while read -r line; do
case $line in
*channel/UC??????????????????????*)
line=${line##*channel/}
line=${line%%\"*}
printf "[%s/%s] using channel ID '%s' for xml link\n" "$count" "$total" "$line"
printf 'https://youtube.com/feeds/videos.xml?channel_id=%s\n' \
"$line" >>"$SUBS_LINKS"
break
;;
esac
done &
sleep "${SUBS_SLEEP_VALUE:-0}"
;;
esac
done <"$SUBS_FILE"
count=0
while [ "$count" -ne "$total" ]; do
count=$(wc -l <"$SUBS_LINKS")
printf "[%s/%s] waiting for jobs to complete...\n" "$count" "$total"
sleep 0.5
done
}
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
# Synopsis: $1 [LINK] -> $SUBS_CACHE/$chan_name/concat [CHANNEL INFO]
#
# Takes a channel rss feed link and creates a file
# with a line of its videos dates, titles, and urls.
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
get_vids() {
data=$(curl -sfL --retry 15 "$1")
# hide the first <published> tag which is the channel
# creation date
data=${data#*\<\/published\>}
# trim off outer <name> tags
chan=${data%%</name*}
chan=${chan##*name>}
printf "%s\n" "$data" |
while read -r line; do
case $line in
*'link rel='*)
line=${line#*href=\"}
line=${line%\"/\>}
line=https://${line#*www.}
url=$line
;;
*'<published>'*)
line=${line%+00:*}
line=${line#*<published>}
date=$line
;;
*'<media:title>'*)
line=${line%</*}
line=${line#*:title>}
title=$line
printf '%s\n' \
"${date}${SEP}${chan}${SEP}${title}${SEP}${url}" \
>>"$SUBS_CACHE/$chan"
;;
esac
done
}
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
# Updates the local cache of subscriptions. ([-u] flag)
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
update_subs() {
[ -f "$SUBS_LINKS" ] || die 'Subs links have not been generated.'
rm -r "${SUBS_CACHE:-?}" 2>/dev/null || :
mkdir -p "$SUBS_CACHE"
total=$(wc -l <"$SUBS_LINKS")
count=0
while read -r link; do
count=$((count + 1))
printf 'starting job [%s/%s] for %s\n' "$count" "$total" "$link"
get_vids "$link" &
sleep "${SUBS_SLEEP_VALUE:-0}"
done <"$SUBS_LINKS"
count=0
while [ "$count" -ne "$total" ]; do
count=$(printf '%s\n' "$SUBS_CACHE"/* | wc -l)
printf "[%s/%s] waiting for fetch jobs to complete...\n" "$count" "$total"
sleep 0.5
done
printf '%s\n\n' 'done!'
}
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
# Grab current cache of subscriptions, sort by date uploaded
# -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
cat_subs() {
sort -r "$SUBS_CACHE"/* |
while read -r line; do
chan=${line#*$SEP}
chan=${chan%%$SEP*}
title=${line#*$chan$SEP}
title=${title%%$SEP*}
date=${line%%$SEP*}
date=${date#*-}
date=${date%T*}
printf '[%s %s] %s\n' "$date" "$chan" "$title"
done
}
# Split the concatenated lines into entities, send to menu program.
# Finally, play the result with mpv.
get_sel() {
if [ -d "$SUBS_CACHE" ]; then
sel=$(cat_subs | $SUBS_MENU_PROG)
else
die 'Subs cache has not been retrieved.'
fi
[ "$sel" ] || die Interrupted
chan="${sel#* }"
chan="${chan%%] *}"
title=${sel#*"$chan"\] }
while read -r line; do
case $line in
*"$SEP$title$SEP"*)
url=${line##*$SEP}
if [ "$url" ]; then
printf 'playing: %s\n' "$url"
# Play the selection.
# shellcheck disable=2086
exec $SUBS_OPEN "$url"
fi
break
;;
esac
done <"$SUBS_CACHE/$chan"
}
daemonize() {
# create a cached copy of the subs file to check for changes
# if changes occur, re-generate links automatically
daemon_file=${HOME}/.cache/subs_daemon.cache
if [ ! -f "$daemon_file" ]; then
cp -f "${SUBS_FILE:=${HOME}/.config/repomenu/subs.ini}" "$daemon_file"
fi
while true; do
if ! cmp "${SUBS_FILE:=${HOME}/.config/repomenu/subs.ini}" "$daemon_file"; then
cp -f "${SUBS_FILE:=${HOME}/.config/repomenu/subs.ini}" "$daemon_file"
fi
gen_links
update_subs
interval=${SUBS_DAEMON_INTERVAL:-$((10 * 60))}
printf 'Sleeping for %s seconds...\n' "$interval"
sleep "$interval"
done
}
main() {
mkdir -p "$SUBS"
case ${1#-} in
h) usage ;;
g) gen_links ;;
u) update_subs ;;
c) cat_subs ;;
d) daemonize ;;
*) get_sel ;;
esac
}
main "$@"