Merge d5f31b599dcbec75ce373af11ab88de0685c4f97 into c95a16916d5c71288e5e49a78d0621943c89a682

This commit is contained in:
Emre AKYÜZ 2024-05-23 13:44:04 -04:00 committed by GitHub
commit 3ef7ecbc9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 334 additions and 0 deletions

79
.local/bin/channelrefresh Normal file
View File

@ -0,0 +1,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="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() {
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

255
.local/bin/ybrowser Normal file
View File

@ -0,0 +1,255 @@
#!/bin/dash
DATA_DIR="$HOME/.cache/youtube_channels"
DOWNLOAD_DIR="$HOME/ytvideos"
CATEGORY_LIST="$HOME/.local/share/categories.txt"
CHANNEL_LIST="$HOME/.local/share/channels.txt"
CUSTOM_LIST_DIR="$DATA_DIR/custom_lists"
mkdir -p "$DATA_DIR" "$DOWNLOAD_DIR" "$CUSTOM_LIST_DIR"
[ "$(find "$DATA_DIR" -maxdepth 1 -type f -name "*.tsv" 2>/dev/null)" ] || {
notify-send "You need to run "channelrefresh" script first."
exit 1
}
DMENU() {
dmenu -i -l "$1" -p "$2"
}
sort_videos() {
data_file="$1"
sort_option="$2"
case $sort_option in
"@@sv") sort -nr -t" " -k3 "$data_file" ;;
"@@sd") sort -nr -t" " -k4 "$data_file" ;;
*) sort -nr -t" " -k5 "$data_file" ;;
esac | cut -f1
}
get_videos() {
channel_name="$1"
sort_option="$2"
data_file="$DATA_DIR/$channel_name.tsv"
sort_videos "$data_file" "$sort_option"
}
video_url() {
channel_name="$1"
video_title="$2"
data_file="$DATA_DIR/$channel_name.tsv"
grep -F "$video_title" "$data_file" | cut -f2
}
rofi_action() {
echo "WATCH\nDOWNLOAD\nSEND TO A LIST" | DMENU 3 "Choose an action for the video"
}
rofi_custom_list_action() {
echo "$(find "$CUSTOM_LIST_DIR" -maxdepth 1 -type f -exec basename {} \;)
#### CREATE A LIST ####
#### DELETE A LIST ####" | DMENU 10 "Choose an action or list"
}
list_video_action() {
echo "WATCH\nDOWNLOAD\nDELETE" | DMENU 3 "Choose an action for the video"
}
add_to_list() {
video_title="$1"
channel_name="$2"
list_name="$3"
echo "$channel_name: $video_title" >> "$CUSTOM_LIST_DIR/$list_name"
}
custom_list_menu() {
while true; do
list="$(rofi_custom_list_action)"
[ -z "$list" ] && return
case "$list" in
*CREATE*)
new_list=$(echo "" | DMENU 0 "Enter the name of the new list")
[ -n "$new_list" ] && touch "$CUSTOM_LIST_DIR/$new_list"
;;
*DELETE*)
delete_list=$(find "$CUSTOM_LIST_DIR" -maxdepth 1 -type f -exec basename {} \; |
DMENU 10 "Choose a list to delete")
[ -n "$delete_list" ] && rm "$CUSTOM_LIST_DIR/$delete_list"
;;
*)
custom_list_video_menu "$list"
;;
esac
done
}
custom_list_video_menu() {
list_name="$1"
while true; do
video_info=$(DMENU 20 "Choose a video" < "$CUSTOM_LIST_DIR/$list_name")
[ -z "$video_info" ] && return
channel_name="${video_info%%: *}"
video_title="${video_info##*: }"
custom_list_video_action_menu "$video_title" "$channel_name" "$list_name"
done
}
custom_list_video_action_menu() {
video_title="$1"
channel_name="$2"
list_name="$3"
action=$(list_video_action)
case $action in
WATCH)
video_process WATCH "$video_title" "$channel_name"
;;
DOWNLOAD)
video_process DOWNLOAD "$video_title" "$channel_name" && notify-send "Downloading has finished."
;;
DELETE)
escaped_video_title="$(echo "$video_title" | sed 's/[][\^$.\/|*+?(){}#]/\\&/g')"
sed "/$escaped_video_title/d" "$CUSTOM_LIST_DIR/$list_name" > "$CUSTOM_LIST_DIR/${list_name}.tmp" &&
mv "$CUSTOM_LIST_DIR/${list_name}.tmp" "$CUSTOM_LIST_DIR/$list_name"
;;
*)
return
;;
esac
}
video_process() {
action="$1"
video_title="$2"
channel_name="$3"
video_url=$(video_url "$channel_name" "$video_title")
case "$action" in
WATCH)
mpv "$video_url"
;;
DOWNLOAD)
local channel_download_dir="$DOWNLOAD_DIR/$channel_name"
mkdir -p "$channel_download_dir"
yt-dlp -o "$channel_download_dir/%(title)s.%(ext)s" "$video_url"
notify-send "Downloading has finished."
;;
esac
}
get_all_videos() {
sort_option="$1"
all_videos_file="$DATA_DIR/all_videos.tsv"
rm -f "$all_videos_file"
while IFS= read -r line
do
channel_name="${line%%=*}"
cat "$DATA_DIR/$channel_name.tsv" >> "$all_videos_file"
done < "$CHANNEL_LIST"
sort_videos "$all_videos_file" "$sort_option"
}
browse_all_channels() {
while video_title=$(get_all_videos | DMENU 20 "Choose a video or enter @@sv or @@sd"); do
[ -z "$video_title" ] && break
[ "$video_title" = "@@sv" ] || [ "$video_title" = "@@sd" ] && video_title=$(get_all_videos "$video_title" | DMENU 20 "Choose a video")
grep -lF "$video_title" "${DATA_DIR}"/*.tsv | head -n 1 | while read -r video_file; do
channel_name=$(basename "$video_file" .tsv)
video_action_menu "$video_title" "$channel_name"
break
done
done
}
category_menu() {
while true; do
category="$(cut -d= -f1 "$CATEGORY_LIST" | DMENU 12 "Choose a category")"
[ -z "$category" ] && return
channel_menu "$category"
done
}
channel_menu() {
category="$1"
IFS="|"
channels=$(sed -n "s/^${category}=\(.*\)$/\1/p" "$CATEGORY_LIST")
set -- $channels
while true; do
channel_name=$(printf '%s\n' "$@" | DMENU 20 "Choose a channel")
[ -z "$channel_name" ] && return
video_menu "$channel_name"
done
}
video_menu() {
channel_name="$1"
while true; do
video_title=$(get_videos "$channel_name" | DMENU 20 "Choose a video")
[ -z "$video_title" ] && return
[ "$video_title" = "@@sv" ] || [ "$video_title" = "@@sd" ] && {
sort_option="$video_title"
video_title=$(get_videos "$channel_name" "$sort_option" | DMENU 20 "Choose a video")
}
video_action_menu "$video_title" "$channel_name"
done
}
video_action_menu() {
video_title="$1"
channel_name="$2"
while [ -n "$video_title" ] && [ "$video_title" != "@@sv" ] && [ "$video_title" != "@@sd" ]; do
action=$(rofi_action)
case $action in
WATCH)
video_process WATCH "$video_title" "$channel_name"
;;
DOWNLOAD)
video_process DOWNLOAD "$video_title" "$channel_name" && notify-send "Downloading has finished."
;;
"SEND TO A LIST")
list_name=$(find "$CUSTOM_LIST_DIR" -maxdepth 1 -type f -exec basename {} \; | DMENU 10 "Choose a list")
[ -n "$list_name" ] && add_to_list "$video_title" "$channel_name" "$list_name" &&
notify-send "$video_title is added to the list: $list_name"
;;
*)
return
;;
esac
done
}
main_menu() {
options=" #### ALL CHANNELS ####
#### CATEGORIES ####
#### CUSTOM LISTS ####
$(cut -d= -f1 "$CHANNEL_LIST")"
echo "$options" | DMENU 20 "Choose an Option or a Category"
}
while true; do
main_choice=$(main_menu)
[ -z "$main_choice" ] && exit
case "$main_choice" in
*ALL\ CHANNELS*) browse_all_channels ;;
*CATEGORIES* ) category_menu ;;
*CUSTOM\ LISTS*) custom_list_menu ;;
*) video_menu "$main_choice" ;;
esac
done