shell cleanup/reordering

This commit is contained in:
2022-02-15 17:14:47 -06:00
parent 49657509d6
commit 07054947d7
21 changed files with 826 additions and 317 deletions

373
src/Shell/Utils/ani-cli Executable file
View File

@@ -0,0 +1,373 @@
#!/bin/sh
# dependencies: grep sed curl video_player
# video_player ( needs to be able to play urls )
player_fn="mpv"
prog="ani-cli"
logfile="${XDG_CACHE_HOME:-$HOME/.cache}/ani-hsts"
c_red="\033[1;31m"
c_green="\033[1;32m"
c_yellow="\033[1;33m"
c_blue="\033[1;34m"
c_magenta="\033[1;35m"
c_cyan="\033[1;36m"
c_reset="\033[0m"
help_text () {
while IFS= read line; do
printf "%s\n" "$line"
done <<-EOF
USAGE: $prog <query>
-h show this help text
-d download episode
-H continue where you left off
-q set video quality (best/worst/360/480/720/..)
EOF
}
die () {
printf "$c_red%s$c_reset\n" "$*" >&2
exit 1
}
err () {
printf "$c_red%s$c_reset\n" "$*" >&2
}
search_anime () {
# get anime name along with its id
search=$(printf '%s' "$1" | tr ' ' '-' )
titlepattern='<a href="/category/'
curl -s "https://gogoanime.pe//search.html" \
-G \
-d "keyword=$search" |
sed -n -E '
s_^[[:space:]]*<a href="/category/([^"]*)" title="([^"]*)".*_\1_p
'
}
search_eps () {
# get available episodes for anime_id
anime_id=$1
curl -s "https://gogoanime.pe/category/$anime_id" |
sed -n -E '
/^[[:space:]]*<a href="#" class="active" ep_start/{
s/.* '\''([0-9]*)'\'' ep_end = '\''([0-9]*)'\''.*/\2/p
q
}
'
}
get_embedded_video_link() {
# get the download page url
anime_id=$1
ep_no=$2
# credits to fork: https://github.com/Dink4n/ani-cli for the fix
curl -s "https://gogoanime.pe/$anime_id-episode-$ep_no" |
sed -n -E '
/^[[:space:]]*<a href="#" rel="100"/{
s/.*data-video="([^"]*)".*/https:\1/p
q
}'
}
get_video_quality() {
embedded_video_url=$1
video_url=$2
video_file=$(curl -s --referer "$embedded_video_url" "$video_url")
available_qualities=$(printf '%s' "$video_file" | sed -n -E 's/.*NAME="([^p]*)p"/\1/p')
case $quality in
best)
printf '%s' "$available_qualities" | tail -n 1
;;
worst)
printf '%s' "$available_qualities" | head -n 1
;;
*)
is_quality_avail=$(printf '%s' "$available_qualities" | grep "$quality")
video_quality="$quality"
if [ -z "$is_quality_avail" ]; then
printf "$c_red%s$c_reset\n" "Current video quality is not available (defaulting to highest quality)" >&2
quality=best
video_quality=$(printf '%s' "$available_qualities" | tail -n 1)
fi
printf '%s' "$video_quality"
;;
esac
}
get_links () {
embedded_video_url="$1"
video_url=$(curl -s "$embedded_video_url" |
sed -n -E '
/^[[:space:]]*sources:/{
s/.*(https[^'\'']*).*/\1/p
q
}
')
video_quality=$(get_video_quality "$embedded_video_url" "$video_url")
# Replace the video with highest quality video
printf '%s' "$video_url" | sed -n -E "s/(.*)\.m3u8/\1.$video_quality.m3u8/p"
}
dep_ch () {
for dep; do
if ! command -v "$dep" >/dev/null ; then
die "Program \"$dep\" not found. Please install it."
fi
done
}
# get query
get_search_query () {
if [ -z "$*" ]; then
printf "Search Anime: "
read -r query
else
query=$*
fi
}
# create history file
[ -f "$logfile" ] || : > "$logfile"
#####################
## Anime selection ##
#####################
anime_selection () {
search_results=$*
menu_format_string='[%d] %s\n'
menu_format_string_c1="$c_blue[$c_cyan%d$c_blue] $c_reset%s\n"
menu_format_string_c2="$c_blue[$c_cyan%d$c_blue] $c_yellow%s$c_reset\n"
count=1
while read anime_id; do
# alternating colors for menu
[ $((count % 2)) -eq 0 ] &&
menu_format_string=$menu_format_string_c1 ||
menu_format_string=$menu_format_string_c2
printf "$menu_format_string" "$count" "$anime_id"
count=$((count+1))
done <<-EOF
$search_results
EOF
# User input
printf "$c_blue%s$c_green" "Enter number: "
read choice
printf "$c_reset"
# Check if input is a number
[ "$choice" -eq "$choice" ] 2>/dev/null || die "Invalid number entered"
# Select respective anime_id
count=1
while read anime_id; do
if [ $count -eq $choice ]; then
selection_id=$anime_id
break
fi
count=$((count+1))
done <<-EOF
$search_results
EOF
[ -z "$selection_id" ] && die "Invalid number entered"
read last_ep_number <<-EOF
$(search_eps "$selection_id")
EOF
}
##################
## Ep selection ##
##################
episode_selection () {
[ $is_download -eq 1 ] &&
printf "Range of episodes can be specified: start_number end_number\n"
printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number
read ep_choice_start ep_choice_end
printf "$c_reset"
}
open_episode () {
anime_id=$1
episode=$2
# Cool way of clearing screen
tput reset
if [ $episode -lt 1 ] || [ $episode -gt $last_ep_number ]; then
err "Episode out of range"
printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number
read episode
printf "$c_reset"
fi
printf "Getting data for episode %d\n" $episode
embedded_video_url=$(get_embedded_video_link "$anime_id" "$episode")
video_url=$(get_links "$embedded_video_url")
case $video_url in
*streamtape*)
# If direct download not available then scrape streamtape.com
BROWSER=${BROWSER:-firefox}
printf "scraping streamtape.com\n"
video_url=$(curl -s "$video_url" | sed -n -E '
/^<script>document/{
s/^[^"]*"([^"]*)" \+ '\''([^'\'']*).*/https:\1\2\&dl=1/p
q
}
');;
esac
if [ $is_download -eq 0 ]; then
# write anime and episode number
sed -E "
s/^${selection_id}\t[0-9]+/${selection_id}\t$((episode+1))/
" "$logfile" > "${logfile}.new" && mv "${logfile}.new" "$logfile"
setsid -f $player_fn --http-header-fields="Referer: $embedded_video_url" "$video_url" >/dev/null 2>&1
else
printf "Downloading episode $episode ...\n"
printf "%s\n" "$video_url"
# add 0 padding to the episode name
episode=$(printf "%03d" $episode)
{
curl -L -# -C - "$video_url" -G -e 'https://streamani.io/' \
-o "${HOME}/Downloads/${anime_id}-${episode}.mp4" "$video_url" >/dev/null 2>&1 &&
printf "${c_green}Downloaded episode: %s${c_reset}\n" "$episode" ||
printf "${c_red}Download failed episode: %s${c_reset}\n" "$episode"
}
fi
}
############
# Start Up #
############
# to clear the colors when exited using SIGINT
trap "printf '$c_reset'" INT HUP
dep_ch "$player_fn" "curl" "sed" "grep"
# option parsing
is_download=0
quality=best
scrape=query
while getopts 'hdHq:' OPT; do
case $OPT in
h)
help_text
exit 0
;;
d)
is_download=1
;;
H)
scrape=history
;;
q)
quality=$OPTARG
;;
esac
done
shift $((OPTIND - 1))
########
# main #
########
case $scrape in
query)
get_search_query "$*"
search_results=$(search_anime "$query")
[ -z "$search_results" ] && die "No search results found"
anime_selection "$search_results"
episode_selection
;;
history)
search_results=$(sed -n -E 's/\t[0-9]*//p' "$logfile")
[ -z "$search_results" ] && die "History is empty"
anime_selection "$search_results"
ep_choice_start=$(sed -n -E "s/${selection_id}\t//p" "$logfile")
;;
esac
{ # checking input
[ "$ep_choice_start" -eq "$ep_choice_start" ] 2>/dev/null || die "Invalid number entered"
episodes=$ep_choice_start
if [ -n "$ep_choice_end" ]; then
[ "$ep_choice_end" -eq "$ep_choice_end" ] 2>/dev/null || die "Invalid number entered"
# create list of episodes to download/watch
episodes=$(seq $ep_choice_start $ep_choice_end)
fi
}
# add anime to history file
grep -q -w "${selection_id}" "$logfile" ||
printf "%s\t%d\n" "$selection_id" $((episode+1)) >> "$logfile"
for ep in $episodes
do
open_episode "$selection_id" "$ep"
done
episode=${ep_choice_end:-$ep_choice_start}
while :; do
printf "\n${c_green}Currently playing %s episode ${c_cyan}%d/%d\n" "$selection_id" $episode $last_ep_number
printf "$c_blue[${c_cyan}%s$c_blue] $c_yellow%s$c_reset\n" "n" "next episode"
printf "$c_blue[${c_cyan}%s$c_blue] $c_magenta%s$c_reset\n" "p" "previous episode"
printf "$c_blue[${c_cyan}%s$c_blue] $c_yellow%s$c_reset\n" "s" "select episode"
printf "$c_blue[${c_cyan}%s$c_blue] $c_magenta%s$c_reset\n" "r" "replay current episode"
printf "$c_blue[${c_cyan}%s$c_blue] $c_red%s$c_reset\n" "q" "exit"
printf "${c_blue}Enter choice:${c_green} "
read choice
printf "$c_reset"
case $choice in
n)
episode=$((episode + 1))
;;
p)
episode=$((episode - 1))
;;
s) printf "${c_blue}Choose episode $c_cyan[1-%d]$c_reset:$c_green " $last_ep_number
read episode
printf "$c_reset"
[ "$episode" -eq "$episode" ] 2>/dev/null || die "Invalid number entered"
;;
r)
episode=$((episode))
;;
q)
break;;
*)
die "invalid choice"
;;
esac
open_episode "$selection_id" "$episode"
done

View File

@@ -7,11 +7,31 @@
# set -o errunset ## To exit if a variable is referenced but not set
function main() {
SCRIPTPATH="$( cd "$(dirname "")" >/dev/null 2>&1 ; pwd -P )"
domain=''
path=''
verbose='false'
cd "${SCRIPTPATH}"
echo "Working Dir: " $(pwd)
while getopts 'dp:v' flag; do
case "${flag}" in
d) domain="${OPTARG}" ;;
p) path="${OPTARG}" ;;
v) verbose='true' ;;
*) print_usage
exit 1 ;;
esac
done
if [[ -z "${domain}" || -z "${path}" ]]; then
print_usage
exit 1
fi
wget \
--recursive \
--no-clobber \
@@ -19,8 +39,13 @@ function main() {
--html-extension \
--convert-links \
--restrict-file-names=windows \
--domains schlechtwetterfront.github.io \
--domains "${domain}" \
--no-parent \
"schlechtwetterfront.github.io/ze_filetypes/index.html"
"${path}"
}
print_usage() {
printf "Usage: -d for domain and -p for the starting path..."
}
main $@;

View File

@@ -0,0 +1,11 @@
#!/bin/bash
# set -o xtrace ## To debug scripts
# set -o errexit ## To exit on error
# set -o errunset ## To exit if a variable is referenced but not set
function main() {
pip3 install flask simplejson watchdog
}
main $@;

View File

@@ -1,3 +1,4 @@
#!/bin/bash
#
# By Maxim F. Stewart
# Contact: [1itdominator@gmail.com]
@@ -17,7 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#---------------------------------------------------------------------------------------#
#!/bin/bash
# . CONFIG.sh

View File

@@ -1,3 +1,4 @@
#!/bin/bash
#
# By Maxim F. Stewart
# Contact: [1itdominator.com]
@@ -17,7 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#---------------------------------------------------------------------------------------#
#!/bin/bash
# . CONFIG.sh
# set -o xtrace ## To debug scripts

View File

200
src/Shell/Utils/yt-cli Executable file
View File

@@ -0,0 +1,200 @@
#!/bin/bash
# dependencies: mpv youtube-dl fzf rofi/dmenu gnu-grep
# NOTE: if you dont have gnu grep you can replace grep with rg
# explain usage
function usage () {
echo "usage: yt"
echo " -h help"
echo " -c channels/subscriptions"
echo " -s query search"
echo " -g / -r gui mode (rofi/dmenu)"
echo " -m music mode (audio only) [dont use with -r]"
echo " nothing use defaults (search from prompt)"
echo
echo "add channel names to the file $sublistpath to show them"
echo "in yt -c option. First word should be channel url, optionally"
echo "followed by tab and then anything else (channel name/description)"
echo "channels not in sublist can be viewed by typing their url in the prompt"
echo
echo "example file format:"
echo "markrober Mark Rober"
echo "vsauce1 VSauce Michael Steven's Channel"
echo "BlackGryph0n Black Gryph0n Gabriel Brown signs stuff"
echo "TomScottGo Tom Scott"
echo "danielthrasher Daniel Thrasher"
exit 0
}
# dont use defaults
useDefaults="f"
# no args -> use defaults
if [[ ${#} -eq 0 ]]; then
useDefaults="t"
fi
# available flags
optstring=":s:cgrhm"
defcmd="fzf"
defaction="s"
guicmd="rofi -dmenu -i" #uncomment next line for dmenu
#guicmd="dmenu -i -l 15"
#Defaults
promptcmd="$defcmd"
action="$defaction"
isGui="f"
query=""
mpv_options=""
# subscription list
mkdir -p "${HOME:-}/.config/yt"
sublistpath="${HOME:-}/.config/yt/sublist"
sublist=""
[ -f "$sublistpath" ] && sublist=$(cat "$sublistpath")
# if not using defaults search for flags
if [[ $useDefaults = "f" ]]; then
while getopts ${optstring} arg; do
case "${arg}" in
s)
# search in youtube for a query
action="s"
query="${OPTARG}" ;;
c)
# search in subscriptions for specific channel
action="c"
query="${OPTARG}" ;;
g|r)
# set gui mode to true and change the prompt to gui prompt
isGui="t"
promptcmd="$guicmd" ;;
m)
# make the mpv headless
mpv_options+="--no-video" ;;
h)
# display help / usage
usage ;;
\?)
# wrong args -> exit with explanation of usage
echo "invalid option: -${OPTARG}"
echo
usage
exit 1 ;;
:)
# missing args -> exit with explanation of usage
echo "Option -${OPTARG} needs an argument"
echo
usage
exit 1 ;;
esac
done
fi
# if no query is set with flags then ask for one
if [ -z "$query" ]; then
# ask for a channel
if [[ $action = "c" ]]; then
# if in gui mode use gui prompt
if [[ $isGui = "t" ]]; then
query=$($promptcmd -p "Channel: " <<< "$sublist")
promptcmd="$promptcmd -p Video:"
else
query=$($promptcmd --print-query <<< "$sublist" | tail -n1)
fi
query=$(echo "$query" | awk '{print $1}')
else
# ask for a query
# if in gui mode use gui prompt
if [[ $isGui = "t" ]]; then
query=$(echo | $promptcmd -p "Search: ")
promptcmd="$promptcmd -p Video:"
else
echo -n "Search: "
read -r query
fi
fi
fi
# program cancelled -> exit
if [ -z "$query" ]; then exit; fi
# clean query / channel
query=$(sed \
-e 's|+|%2B|g'\
-e 's|#|%23|g'\
-e 's|&|%26|g'\
-e 's| |+|g' <<< "$query")
# if channel look for channel vids
if [[ $action = "c" ]]; then
response=$(curl -s "https://www.youtube.com/c/$query/videos" |\
sed "s/{\"gridVideoRenderer/\n\n&/g" |\
sed "s/}]}}}]}}/&\n\n/g" |\
awk -v ORS="\n\n" '/gridVideoRenderer/')
# if unable to fetch the youtube results page, inform and exit
if ! grep -q "gridVideoRenderer" <<< "$response"; then echo "unable to fetch yt"; exit 1; fi
# regex expression to match video entries from yt channel page
# get the list of videos and their ids to ids
ids=$(awk -F '[""]' '{print $6 "\t" $50;}' <<< "$response" | grep "^\S")
# url prefix for videos
videolink="https://youtu.be/"
# prompt the results to user infinitely until they exit (escape)
while true; do
choice=$(echo -e "$ids" | cut -d' ' -f2 | $promptcmd) # dont show id
if [ -z "$choice" ]; then exit; fi # if esc-ed then exit
id=$(echo -e "$ids" | grep -Fwm1 "$choice" | cut -d' ' -f1) # get id of choice
echo -e "$choice\t($id)"
case $id in
???????????) mpv "$videolink$id" "$mpv_options" ;;
*) exit ;;
esac
done
else
# if in search show query result vids
response="$(curl -s "https://www.youtube.com/results?search_query=$query" |\
sed 's|\\.||g')"
# if unable to fetch the youtube results page, inform and exit
if ! grep -q "script" <<< "$response"; then echo "unable to fetch yt"; exit 1; fi
# regex expression to match video and playlist entries from yt result page
vgrep='"videoRenderer":{"videoId":"\K.{11}".+?"text":".+?[^\\](?=")'
pgrep='"playlistRenderer":{"playlistId":"\K.{34}?","title":{"simpleText":".+?[^\"](?=")'
# grep the id and title
# return them in format id (type) title
getresults() {
grep -oP "$1" <<< "$response" |\
awk -F\" -v p="$2" '{ print $1 "\t" p " " $NF}'
}
# get the list of videos/playlists and their ids in videoids and playlistids
videoids=$(getresults "$vgrep")
playlistids=$(getresults "$pgrep" "(playlist)")
# if there are playlists or videos, append them to list
[ -n "$playlistids" ] && ids="$playlistids\n"
[ -n "$videoids" ] && ids="$ids$videoids"
# url prefix for videos and playlists
videolink="https://youtu.be/"
playlink="https://youtube.com/playlist?list="
# prompt the results to user infinitely until they exit (escape)
while true; do
choice=$(echo -e "$ids" | cut -d' ' -f2 | $promptcmd) # dont show id
if [ -z "$choice" ]; then exit; fi # if esc-ed then exit
id=$(echo -e "$ids" | grep -Fwm1 "$choice" | cut -d' ' -f1) # get id of choice
echo -e "$choice\t($id)"
case $id in
# 11 digit id = video
???????????) mpv "$videolink$id" "$mpv_options" ;;
# 34 digit id = playlist
??????????????????????????????????) mpv "$playlink$id" "$mpv_options" ;;
*) exit ;;
esac
done
fi