Update channelrefresh

This commit is contained in:
Emre AKYÜZ 2024-07-02 01:52:53 +03:00 committed by GitHub
parent d5f31b599d
commit a43657a52a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,79 +1,61 @@
#!/bin/dash #!/bin/dash
while ! ping -c 1 "9.9.9.9"; do sleep "0.5"; done
dd="${XDG_CACHE_HOME}/ychannels"
chl="${XDG_DATA_HOME}/channels"
ctg="${XDG_DATA_HOME}/categories"
n() { notify-send -i "${XDG_CONFIG_HOME}/dunst/yt.png" -u "critical" "${1}"; }
while ! ping -c 1 9.9.9.9 > /dev/null 2>&1; do sleep 0.5; done eh() {
[ ! -s "${chl}" ] && [ ! -s "${ctg}" ] && {
# "~/.local/share/channels.txt" looks like below: printf "%s\n" \
# Luke Smith=https://www.youtube.com/@LukeSmithxyz/videos "Luke Smith=https://www.youtube.com/@LukeSmithxyz/videos" \
# Mental Outlaw=https://www.youtube.com/@MentalOutlaw/videos "Mental Outlaw=https://www.youtube.com/@MentalOutlaw/videos" \
> "${chl}"
# ~/.local/share/categories.txt looks like below: printf "%s\n" "Tech=Luke Smith|Mental Outlaw" > "${ctg}"
# Tech=Luke Smith|Mental Outlaw sudo pacman -S --needed --noconfirm mpv yt-dlp jq || n "Failed to install mpv jq yt-dlp"
}
INSTALLER="sudo pacman -S --noconfirm"
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() { mkdir -p "${dd}" && touch "${chl}"
local channel_name="$1" comd() {
local channel_url="$2" chn="${1}"
local data_file="${DATA_DIR}/${channel_name}.tsv" df="${dd}/${chn}.tsv"
local old_data_file="${DATA_DIR}/${channel_name}_old.tsv" odf="${dd}/${chn}_old.tsv"
[ -f "${odf}" ] && {
mv "$data_file" "$old_data_file" 2>/dev/null ou="$(cut -f2 "${odf}")"
nu="$(cut -f2 "${df}")"
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" printf "%s\n" "${ou}" | sort > "t1"
printf "%s\n" "${nu}" | sort > "t2"
nv="$(comm -13 "t1" "t2" | wc -l)"
rm -f "t1" "t2"
[ "${nv}" -gt "0" ] && n "${chn} | ${nv} videos."
}
} }
ud() {
update_all_channels() { chn="${1}"
while IFS="=" read -r channel_name channel_url; do cu="${2}"
update_data "$channel_name" "$channel_url" & df="${dd}/${chn}.tsv"
done < "$CHANNEL_LIST" odf="${dd}/${chn}_old.tsv"
mv -f "${df}" "${odf}" 2> "/dev/null"
wait yt-dlp -j --flat-playlist --skip-download --extractor-args \
"youtubetab:approximate_date" "${cu}" |
while IFS="=" read -r channel_name channel_url; do jq -r '[.title, .url, .view_count, .duration, .upload_date] | @tsv' > "${df}"
compare_data "$channel_name"
done < "$CHANNEL_LIST"
} }
uac() {
while IFS="=" read -r chn cu; do
ud "${chn}" "${cu}" &
done < "${chl}"
wait
while IFS="=" read -r chn cu; do
comd "${chn}"
done < "${chl}"
error_handling avf="${dd}/all_videos.tsv"
update_all_channels rm -f "${avf}"
while IFS= read -r line; do
cn="${line%%=*}"
cat "${dd}/${cn}.tsv" >> "${avf}"
done < "${chl}"
}
eh
uac