-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrefresh_channels.sh
79 lines (63 loc) · 2.24 KB
/
refresh_channels.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
#!/bin/dash
while ! ping -c 1 9.9.9.9 > /dev/null 2>&1; do sleep 0.5; done
# "~/.local/share/channels.txt" looks like below:
# Luke Smith=https://www.youtube.com/@LukeSmithxyz/videos
# Mental Outlaw=https://www.youtube.com/@MentalOutlaw/videos
# ~/.local/share/categories.txt looks like below:
# Tech=Luke Smith|Mental Outlaw
INSTALLER="emerge"
DATA_DIR="$HOME/.cache/youtube_channels"
CHANNEL_LIST="$HOME/.local/share/channels.txt"
mkdir -p "$DATA_DIR" && touch "$CHANNEL_LIST"
error_handling() {
[ -s "$CHANNEL_LIST" ] || {
notify-send "You don't have any channels in 'channels.txt'."
exit 1
}
grep -q "^.*=https://www.youtube.com/@[[:alnum:]]*/videos$" "$CHANNEL_LIST" || {
notify-send "'channels.txt' formatting is wrong."
exit 1
}
for pkg in yt-dlp mpv jq; do
command -v "$pkg" >/dev/null || {
notify-send "$pkg is not installed. Installing..."
$INSTALLER "$pkg" || {
notify-send "Failed to install $pkg."
exit 1
}
}
done
}
compare_data() {
local channel_name="$1"
local data_file="${DATA_DIR}/${channel_name}.tsv"
local old_data_file="${DATA_DIR}/${channel_name}_old.tsv"
[ -e "$old_data_file" ] && {
old_urls=$(cut -f2 "$old_data_file")
new_urls=$(cut -f2 "$data_file")
echo "$old_urls" | sort > temp1
echo "$new_urls" | sort > temp2
new_videos=$(comm -13 temp1 temp2 | wc -l)
rm temp1 temp2
[ "$new_videos" -gt 0 ] && notify-send -u critical "$channel_name | $new_videos videos"
}
}
update_data() {
local channel_name="$1"
local channel_url="$2"
local data_file="${DATA_DIR}/${channel_name}.tsv"
local old_data_file="${DATA_DIR}/${channel_name}_old.tsv"
mv "$data_file" "$old_data_file" 2>/dev/null
yt-dlp -j --flat-playlist --skip-download --extractor-args youtubetab:approximate_date "$channel_url" | jq -r '[.title, .url, .view_count, .duration, .upload_date] | @tsv' > "$data_file"
}
update_all_channels() {
while IFS="=" read -r channel_name channel_url; do
update_data "$channel_name" "$channel_url" &
done < "$CHANNEL_LIST"
wait
while IFS="=" read -r channel_name channel_url; do
compare_data "$channel_name"
done < "$CHANNEL_LIST"
}
error_handling
update_all_channels