From 3d0eea4ff36d2fd4ed2ecee0f1fed0d85f869120 Mon Sep 17 00:00:00 2001 From: "i.ortega" Date: Fri, 8 May 2020 15:54:00 +0200 Subject: [PATCH] Scripts folder and updated wlvfs --- scripts/channel-videos-after.sh | 19 +++++++++++ scripts/no-response-timeout.sh | 58 ++++++++++++++++++++++++++++++++ scripts/play-videos-from-file.sh | 24 +++++++++++++ scripts/video-upload-date.sh | 22 ++++++++++++ wlvfs | 49 +++++++++++++-------------- 5 files changed, 147 insertions(+), 25 deletions(-) create mode 100644 scripts/channel-videos-after.sh create mode 100644 scripts/no-response-timeout.sh create mode 100644 scripts/play-videos-from-file.sh create mode 100644 scripts/video-upload-date.sh diff --git a/scripts/channel-videos-after.sh b/scripts/channel-videos-after.sh new file mode 100644 index 0000000..eaf2fd0 --- /dev/null +++ b/scripts/channel-videos-after.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env sh + +channel="$1" +last_watched_video="$2" +scripts_folder=$(dirname "$(readlink -f "$0")") + +[ -n "$3" ] && dateafter="--dateafter $3" + +dash "$scripts_folder/no-response-timeout.sh" -f 128 -r 10 \ + youtube-dl --get-id "$channel" $dateafter 2>/dev/null | + while read -r line && [ "$line" != "$last_watched_video" ]; do + echo "$line" +done + +# youtube-dl --get-id "$channel" | grep -B 4 "$last_watched_video" | tee /home/initega/test/test -a & + +# child_pid=$! + +# echo test/test | entr -p kill $child_pid diff --git a/scripts/no-response-timeout.sh b/scripts/no-response-timeout.sh new file mode 100644 index 0000000..27bbbdd --- /dev/null +++ b/scripts/no-response-timeout.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env sh + +usage() { + printf "HELP ME" +} + +while getopts ":t:r:f:" option; do + case "${option}" in + t) + time="${OPTARG}" + ;; + r) + time4rest="${OPTARG}" + ;; + f) + time4first="${OPTARG}" + ;; + *) + usage + ;; + esac +done +shift $((OPTIND-1)) + +[ -z "$time" ] && time="30" +[ -z "$time4rest" ] && time4rest="$time" +[ -z "$time4first" ] && time4first="$time" + +{ + sleep "$time4first" + kill "$$" 2>/dev/null +} & +checkpoint_pid="$!" + +fifo="/tmp/no-response-timout-fifo$(date +%s%N)" +mkfifo "$fifo" +sh -c "$*" > "$fifo" & +command_pid="$!" + +cleanup() { + kill "$command_pid" 2>/dev/null + rm "$fifo" +} +trap cleanup TERM INT PIPE + +while read -r line; do + kill "$checkpoint_pid" + { + sleep "$time4rest" + kill "$$" 2>/dev/null + } & + checkpoint_pid="$!" + + echo "$line" +done < "$fifo" + +rm "$fifo" +kill "$checkpoint_pid" 2>/dev/null diff --git a/scripts/play-videos-from-file.sh b/scripts/play-videos-from-file.sh new file mode 100644 index 0000000..7368a12 --- /dev/null +++ b/scripts/play-videos-from-file.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env sh + +[ -z "$1" -o ! -f "$1" ] && exit 1 + +length="$(wc -l "$1" | awk '{print $1}')" + +current_video=0 +while [ "$length" -ne $current_video ] +do + current_video="$(( $current_video + 1 ))" + + video="$(sed "$current_video q;d" "$1")" + + if [ -n "$VIDEOPLAYER" ]; then + $VIDEOPLAYER "$video" + else + mpv "$video" + fi + + length="$(wc -l "$1" | awk '{print $1}')" + # if [ "$length" -eq "$current_video" ]; then + # sleep 5m + # fi +done diff --git a/scripts/video-upload-date.sh b/scripts/video-upload-date.sh new file mode 100644 index 0000000..6ace23d --- /dev/null +++ b/scripts/video-upload-date.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +secs="$(date +%s%N)" +file_prefix="/tmp/wlvfs$secs-" + +fifo="/tmp/fifo-vup$secs" +mkfifo "$fifo" +youtube-dl -o "$file_prefix%(upload_date)s" -- "$1" 2>/dev/null > "$fifo" & +pid="$!" + +while read -r line; do + file=$(echo "$line" | grep 'Destination:') + [ -n "$file" ] && { + file=$(echo "$file" | cut -d " " -f 3) + echo "$file" | sed -E "s|($file_prefix)([0-9]+)(\..+)?|\2|" + break + } +done < "$fifo" + +kill "$pid" +rm "$fifo" +echo "$file" | grep -q "^$file_prefix" && rm "$file"* 2>/dev/null diff --git a/wlvfs b/wlvfs index 2d2bcc8..2ca9e60 100644 --- a/wlvfs +++ b/wlvfs @@ -41,6 +41,7 @@ while getopts ":upef:" option; do done shift $((OPTIND-1)) +scripts_folder="$(dirname "$(readlink -f "$0")")/scripts" videos_file="$HOME/.local/share/wlvfs_videos" if [ -z "$subs_file" ]; then @@ -51,12 +52,6 @@ if [ -z "$subs_file" ]; then fi fi -already_updated=$(grep "^!updated" "$subs_file") - -if [ "$(echo "$(date +%j) % 14" | bc)" -ne 0 ] && [ "$already_updated" ]; then - sed -i '/!updated/d' "$subs_file" -fi - # In order to print it too on a file, so that I can track where it is { @@ -69,6 +64,7 @@ fi upload_frequency=$(echo "$line" | awk '{print $4}') prev_upload_frequency=$(echo "$line" | awk '{print $5}') manual_preference=$(echo "$line" | awk '{print $6}') + already_updated=$(echo "$line" | awk '{print $7}') new_last_video="$last_video" new_update_date="$update_date" @@ -93,11 +89,11 @@ fi new_last_video="-" new_update_date="-" else - new_update_date=$(dash $TSCRIPTS/video-upload-date.sh \ + new_update_date=$(dash "$scripts_folder/video-upload-date.sh" \ "$new_last_video") while [ -z "$new_update_date" ]; do new_update_date=$(dash \ - $TSCRIPTS/video-upload-date.sh \ + "$scripts_folder/video-upload-date.sh" \ "$new_last_video") done fi @@ -111,22 +107,22 @@ fi "https://invidio.us/channel/$channel" 2>/dev/null \ | head -n 1) [ -z "$new_last_video" ] && new_last_video="-" - new_update_date=$(dash $TSCRIPTS/video-upload-date.sh \ + new_update_date=$(dash "$scripts_folder/video-upload-date.sh" \ "$new_last_video") while [ -z "$new_update_date" ]; do new_update_date=$(dash \ - $TSCRIPTS/video-upload-date.sh \ + "$scripts_folder/video-upload-date.sh" \ "$new_last_video") done new_upload_frequency=0 new_prev_upload_frequency=0 new_manual_preference=0 else - new_update_date=$(dash $TSCRIPTS/video-upload-date.sh \ + new_update_date=$(dash "$scripts_folder/video-upload-date.sh" \ "$new_last_video") while [ -z "$new_update_date" ]; do new_update_date=$(dash \ - $TSCRIPTS/video-upload-date.sh \ + "$scripts_folder/video-upload-date.sh" \ "$new_last_video") done new_upload_frequency=0 @@ -153,6 +149,12 @@ fi sed -E "s|($channel)(\t$last_video)?(\t$update_date)?(\t$upload_frequency)?(\t$prev_upload_frequency)?(\t$manual_preference)?|$channel\t$new_last_video\t$new_update_date\t$new_upload_frequency\t$new_prev_upload_frequency\t$new_manual_preference|" "$subs_file" -i } + + [ "$(echo "$(date +%j) % 14" | bc)" -ne 0 ] && + [ -n "$already_updated" ] && { + echo "Preprocessing: $channel" + sed -E 's|(.*)(\t!updated)$|\1|' "$subs_file" -i + } done @@ -160,7 +162,7 @@ fi if [ -z "$preserve_oldvideos" ]; then rm "$videos_file" 2> /dev/null else - (dash $TSCRIPTS/play-videos-from-file.sh "$videos_file" && + (dash "$scripts_folder/play-videos-from-file.sh" "$videos_file" && rm "$videos_file") & fi @@ -171,6 +173,7 @@ fi upload_frequency=$(echo "$line" | awk '{print $4}') prev_upload_frequency=$(echo "$line" | awk '{print $5}') manual_preference=$(echo "$line" | awk '{print $6}') + already_updated=$(echo "$line" | awk '{print $7}') echo "Processing: $channel on Preference: $manual_preference - $prev_upload_frequency" @@ -178,7 +181,7 @@ fi dateafter="$update_date" [ "$update_date" = "-" ] && dateafter="" - dash $TSCRIPTS/channel-videos-after.sh \ + dash "$scripts_folder/channel-videos-after.sh" \ "https://youtube.com/channel/$channel" "$last_video" "$dateafter" | tee "$temp" | while read video; do @@ -186,7 +189,7 @@ fi if [ ! -f "$videos_file" ]; then echo "ytdl://$video" >> $videos_file if [ -z "$play_at_the_end" ]; then - (dash $TSCRIPTS/play-videos-from-file.sh "$videos_file"&& + (dash "$scripts_folder/play-videos-from-file.sh" "$videos_file"&& rm "$videos_file") & fi else @@ -204,19 +207,19 @@ fi new_upload_frequency="$upload_frequency" new_update_date="$update_date" else - new_update_date=$(dash "$TSCRIPTS/video-upload-date.sh" "$new_last_video") + new_update_date=$(dash "$scripts_folder/video-upload-date.sh" "$new_last_video") while [ -z "$new_update_date" ]; do - new_update_date=$(dash "$TSCRIPTS/video-upload-date.sh" "$new_last_video") + new_update_date=$(dash "$scripts_folder/video-upload-date.sh" "$new_last_video") done new_upload_frequency=$(( $(wc -l "$temp" | \ awk '{print $1}') + $upload_frequency )) fi - if [ "$update_now" ] || \ - { [ "$(echo "$(date +%j) % 14" | bc)" -eq 0 ] && [ ! "$already_updated" ]; } + if [ -z "$already_updated" ] && + { [ "$update_now" ]||[ "$(echo "$(date +%j) % 14" | bc)" -eq 0 ];} then - sed \ - "s|$channel\t$last_video\t$update_date\t$upload_frequency\t$prev_upload_frequency|$channel\t$new_last_video\t$new_update_date\t0\t$new_upload_frequency|" \ + sed -E \ + "s|$channel\t$last_video\t$update_date\t$upload_frequency\t$prev_upload_frequency|$channel\t$new_last_video\t$new_update_date\t0\t$new_upload_frequency|;s|(^$channel.*$)|\1\t!updated|" \ "$subs_file" -i else sed \ @@ -229,8 +232,4 @@ fi } | tee "/tmp/wlvfs-log" -if [ "$(echo "$(date +%j) % 14" | bc)" -eq 0 ] && [ ! "$already_updated" ]; then - echo "!updated" >> "$subs_file" -fi - notify-send "Watch Subs" "Done"