Compare commits

...

5 Commits

Author SHA1 Message Date
Emre AKYÜZ
3b1ad02473
Merge 4fc941b9f12e297909f08c2fe33db0932de3d287 into 708d6c67317ca90138681d4a7892cc5db2e092cc 2023-10-05 05:53:32 +00:00
Emre AKYÜZ
4fc941b9f1
Update Channels & Notify 2023-09-29 11:53:53 +03:00
Emre AKYÜZ
9e2bf97977
Remove IFs & Nested Loops | Use DASH | Add Custom Lists 2023-09-29 10:58:29 +03:00
Emre AKYÜZ
395a277120
Add Approximate Date as Default 2023-05-30 10:59:59 +03:00
Emre AKYÜZ
8e7c55f586
The Ultimate Way of Browsing Channels
The script works extremely fast except the first time to update the whole data. It takes about 2 minutes to update the whole database with 80 different channels. You can set a cronjob for this. It's not a heavy work for the PC. It justs fetch the text data with yt-dlp. Video example below:

This script is a sophisticated and ingenious tool designed to streamline your YouTube experience by organizing and managing your favorite YouTube channels, allowing you to browse and watch videos directly within the script without ever visiting its website. You can assign the channels inside various categories such as "Tech", "Science", "Sports", etc. The videos can be played using the 'mpv' media player. Moreover, the script allows you to sort videos based on view count or duration; download videos; and even maintain a "Watch Later" list. If you combine this script with "SponsorBlock" lua script created for "mpv", then you will have the ultimate experience. SponsorBlock removes all sponsored segments in a video including intros, outros or similar unnecessary parts. It's normally a browser extension but is also available for "mpv".

No browsers, accounts, distractions, crappy algorithm and recommendations, advertisements, sponsors, intros, outros, fillers or empty spaces. We eliminate them all.

Required Programs: dmenu | mpv | jq | yt-dlp

FEATURES
1. Browse all videos from all channels you set at the same time. You can filter titles through dmenu.
2. Browse a channel's videos.
3. Select a channel either from the main menu or inside a Category.
4. Watch, Download or Put videos in a "Watch Later List".
5. Sort videos by view or duration. The default sort is upload date. The only problem is, we can't have the exact upload date, so we can't apply much more advanced filtering. It can be done but it makes fetching the data for the first time too slow.
6. The menus have a complex loop system. It always continues where you left off. The script doesn't close itself when you make a selection. So you don't have to run the script over and over again and get to where you left off. You can also press Escape to return to a prior menu.
7. You won't see the URLs or any unnecessary things inside dmenu. Just the titles.



JUSTIFICATION
This script is incredibly beneficial for those who seek a minimalist and focused approach to consuming content on YouTube. By providing a CLI-based interface (dmenu), the script reduces distractions and clutter that are commonly encountered on the Youtube website. It allows users to personalize their content consumption and manage channels more effectively. The script is also remarkably efficient and easy to navigate, providing a user-friendly experience that saves time and promotes productivity.

The script is organized into functions that each perform a specific task, such as updating channel data, retrieving video titles, playing videos, downloading videos, adding videos to the watch later list, and browsing all channels. These functions are called by the main script to provide the user with various options for navigating and interacting with the videos.

The script makes use of various Bash features such as associative arrays, shell redirection and piping, to simplify and streamline the code. It also uses conditionals and loops to handle different user input and error cases. Overall, this script is a powerful and flexible tool for browsing, watching, organizing YouTube channels, and it provides a great example of Bash usage to automate and streamline complex tasks.

DETAILED EXPLANATION
- The script begins by defining two associative arrays, CHANNELS and CATEGORIES, which store the YouTube channel names along with their respective URLs and categories. It then sets the directories for storing data and videos, and creates them if they do not already exist.
- The 'update_data' function updates the metadata for a given channel, while the 'update_all_channels' function updates metadata for all channels. The metadata includes the video title, URL, view count, and duration, which are extracted using 'yt-dlp' and 'jq' utilities.
- The 'get_videos' function retrieves the video titles from the metadata of a given channel, sorted by the specified criteria (if any). The 'video_url' function returns the URL of a video based on its title and channel name. The 'play_video' and 'download_video' functions use 'mpv' and 'yt-dlp', respectively, to play or download a video given its title and channel name.
- The 'add_to_watch_later' function appends the video title and channel name to a watch later list, while the 'play_watch_later' and 'delete_from_watch_later' functions play a video from the list or remove it, respectively.
- The 'get_all_videos' function retrieves all video titles from the metadata of all channels, sorted by the specified criteria. The 'browse_all_channels' function lets you browse through all channels and select a video to watch, download, or add to the watch later list.
- The main part of the script first prompts the user to update the database of channels. If the user chooses to do so, the 'update_all_channels' function is called. The script then presents the user with options to browse all channels, browse channels by category, or browse the watch later list. The script loops through these options until the user decides to exit.
2023-05-02 21:49:46 +00:00
2 changed files with 293 additions and 0 deletions

50
.local/bin/channelrefresh Normal file
View File

@ -0,0 +1,50 @@
#!/bin/dash
sleep 16
DATA_DIR="$HOME/.cache/youtube_channels"
CHANNEL_LIST="$HOME/.local/share/channels.txt"
mkdir -p "$DATA_DIR"
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"
}
update_all_channels

243
.local/bin/yt-browser Normal file
View File

@ -0,0 +1,243 @@
#!/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"
sort_videos() {
local data_file="$1"
local 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() {
local channel_name="$1"
local sort_option="$2"
local data_file="$DATA_DIR/$channel_name.tsv"
sort_videos "$data_file" "$sort_option"
}
video_url() {
local channel_name="$1"
local video_title="$2"
local data_file="$DATA_DIR/$channel_name.tsv"
sed -n "s/$video_title\t\([^\t]*\)\t.*$/\1/p" "$data_file"
}
rofi_action() {
echo "Watch\nDownload\nSend To a List" | dmenu -i -l 3 -p "Choose an action for the video"
}
rofi_custom_list_action() {
echo "$(ls "$CUSTOM_LIST_DIR")\nCreate a List\nDelete a List" | dmenu -i -l 10 -p "Choose an action or list"
}
list_video_action() {
echo "Watch\nDownload\nDelete" | dmenu -i -l 3 -p "Choose an action for the video"
}
add_to_list() {
local video_title="$1"
local channel_name="$2"
local list_name="$3"
echo "$channel_name: $video_title" >> "$CUSTOM_LIST_DIR/$list_name"
}
custom_list_menu() {
while true; do
local list="$(rofi_custom_list_action)"
[ -z "$list" ] && return
case "$list" in
"Create a List")
local new_list=$(dmenu -i -l 0 -p "Enter the name of the new list")
[ -n "$new_list" ] && touch "$CUSTOM_LIST_DIR/$new_list"
;;
"Delete a List")
local delete_list=$(ls "$CUSTOM_LIST_DIR" | dmenu -i -l 10 -p "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() {
local list_name="$1"
local video_info
local video_title
local channel_name
while true; do
video_info=$(cat "$CUSTOM_LIST_DIR/$list_name" | dmenu -i -l 20 -p "Choose a video")
[ -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() {
local video_title="$1"
local channel_name="$2"
local list_name="$3"
local action
action=$(list_video_action)
case $action in
Watch)
play_video "$video_title" "$channel_name"
;;
Download)
download_video "$video_title" "$channel_name" && notify-send "Downloading has finished."
;;
Delete)
escaped_video_title="$(echo "$video_title" | tr -d '|')"
sed -i "/$escaped_video_title/d" "$CUSTOM_LIST_DIR/$list_name"
;;
*)
return
;;
esac
}
play_video() {
local video_title="$1"
local channel_name="$2"
local video_url="$(video_url "$channel_name" "$video_title")"
mpv "$video_url"
}
download_video() {
local video_title="$1"
local channel_name="$2"
local video_url=$(video_url "$channel_name" "$video_title")
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"
}
get_all_videos() {
local sort_option="$1"
local all_videos_file="$DATA_DIR/all_videos.tsv"
rm -f "$all_videos_file"
while IFS= read -r line
do
local 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 true; do
video_title=$(get_all_videos | dmenu -i -l 20 -p "Choose a video or enter @@sv or @@sd")
[ -z "$video_title" ] && break || {
[ "$video_title" = "@@sv" -o "$video_title" = "@@sd" ] && sort_option="$video_title" && video_title=$(get_all_videos "$sort_option" | i -l 20 -dmenu -i -p "Choose a video")
}
[ -n "$video_title" -a "$video_title" != "@@sv" -a "$video_title" != "@@sd" ] && {
while read -r line; do
channel_name="${line%%=*}"
grep -qF "$video_title" "${DATA_DIR}/${channel_name}.tsv" && break
done < "$CHANNEL_LIST"
video_action_menu "$video_title" "$channel_name"
}
done
}
category_menu() {
while true; do
local category=$(echo "$(cut -d= -f1 "$CATEGORY_LIST")" | dmenu -i -l 12 -p "Choose a category")
[ -z "$category" ] && return
channel_menu "$category"
done
}
channel_menu() {
local category="$1"
local channels
local channel_name
local IFS="|"
channels=$(sed -n "s/^${category}=\(.*\)$/\1/p" "$CATEGORY_LIST")
set -- $channels
while true; do
channel_name=$(printf '%s\n' "$@" | dmenu -i -l 20 -p "Choose a channel")
[ -z "$channel_name" ] && return
video_menu "$channel_name"
done
}
video_menu() {
local channel_name="$1"
local video_title
local sort_option
while true; do
video_title=$(get_videos "$channel_name" | dmenu -i -l 20 -p "Choose a video")
[ -z "$video_title" ] && return
[ "$video_title" = "@@sv" -o "$video_title" = "@@sd" ] && {
sort_option="$video_title"
video_title=$(get_videos "$channel_name" "$sort_option" | dmenu -i -l 20 -p "Choose a video")
}
video_action_menu "$video_title" "$channel_name"
done
}
video_action_menu() {
local video_title="$1"
local channel_name="$2"
while [ -n "$video_title" ] && [ "$video_title" != "@@sv" ] && [ "$video_title" != "@@sd" ]; do
local action
action=$(rofi_action)
case $action in
Watch)
play_video "$video_title" "$channel_name"
;;
Download)
download_video "$video_title" "$channel_name" && notify-send "Downloading has finished."
;;
"Send To a List")
list_name=$(ls "$CUSTOM_LIST_DIR" | dmenu -i -l 10 -p "Choose a list")
[ -n "$list_name" ] && add_to_list "$video_title" "$channel_name" "$list_name"
;;
*)
return
;;
esac
done
}
while true; do
main_choice=$(echo "All Channels\nCategories\nCustom Lists\n$(cut -d= -f1 "$CHANNEL_LIST")" | dmenu -i -l 20 -p "Choose an Option or a Category")
[ -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