Replace gendl with curl

This commit is contained in:
Jaidyn Ann 2020-11-27 19:47:48 -06:00
parent 28aac003d2
commit 79201f0975
3 changed files with 2 additions and 281 deletions

155
gendl
View File

@ -1,155 +0,0 @@
#!/bin/sh
##############################
# name: gendl
# lisc: gnu gplv3
# desc: download to stdout or
# file, independent of
# a specific downloader.
# ftp/wget/curl support.
# main: jadedctrl
##############################
# usage: gendl [-o output] url
#---------------------------------------
# backend bits
# NIL --> STRING
# return the download program you're using
function download_program
{
programs="ftp curl wget"
current=""
for program in $programs
do
if whereis $program > /dev/null
then
current=$program
fi
done
if uname -s | grep -e "LibertyBSD" -e "OpenBSD" > /dev/null
then
current="ftp"
fi
echo "$current"
}
# STRING PATH --> NIL
# download URL $1 to stdout
function download_stdout
{
program=$(download_program)
url=$1
case "$program" in
"ftp")
output="$(ftp -VMo- $url)"
;;
"curl")
output="$(curl $url)"
;;
"wget")
output="$(wget --quiet -O- $url)"
;;
esac
return_code=$?
echo "$output"
return $return_code
}
# STRING PATH --> NIL
# download URL $1 to path $2
function download_file
{
program=$(download_program)
url=$1
path=$2
case "$program" in
"ftp")
ftp -VMU "." -o $path $url
;;
"curl")
curl -o $path $url
;;
"wget")
wget --quiet -O $path $url
;;
esac
return_code=$?
if test $return_code -ne 0 2>/dev/null
then
rm $path 2> /dev/null
# for consistency in behavior; wget saves 404s anyway, whereas
# ftp doesn't save anything from 404s, etc.
fi
return $return_code
}
# --------------------------------------
# front-end string-manip
# STRING --> STRING
# return the last word in a string
function last_word
{
string="$1"
echo "$string" \
| rev \
| sed 's% .*%%' \
| rev
}
function usage
{
echo "usage: gendl [-o output] URL"
}
# --------------------------------------
# invocation
args="$(getopt o: $*)"
if test -z "$@" 2>/dev/null
then
usage
exit 2
fi
set -- $args
while test $# -ne 0
do
case "$1"
in
-o)
download_path="$2"; shift; shift;;
--)
shift; break;;
esac
done
url="$(last_word "$@")"
if test -n "$download_path" 2> /dev/null
then
download_file $url $download_path
else
download_stdout "$url"
fi

124
mp
View File

@ -1,124 +0,0 @@
#!/bin/sh
########################################
# name: mp
# desc: front-end to MPV that displays
# lyrics text-files before playing
# a song.
# main: Jenga Phoenix
# <jadedctrl@teknik.io>
# lisc: CC 0
########################################
ARGUMENTS='--vo null --term-osd-bar'
trap "" 2
#---------------------------------------
# generic
# STRING --> STRING
# Return the first 'word' (space-delimiter) of a string.
function car {
local string="$1"
echo "$string" \
| awk '{ print $1 }'
}
# STRING --> STRING
# Return all words after the first word of a string.
function cdr {
local string="$1"
local car="$(car "$string")"
if test $(length "$string") -eq 1; then
echo ""
else
echo "$string" \
| sed 's/^'"$car"' //'
fi
}
# STRING --> NUMBER
# Return the length of a string.
function length {
local string="$1"
echo "$string" \
| wc -w \
| tr -d ' '
}
# NUMBER NUMBER --> NUMBER
# Add two numbers together.
function add {
operator="$1"
operatee="$2"
echo "$1 + $2" \
| bc
}
# --------------------------------------
# NIL --> NUMBER
# Return the amount of total items.
function total_items {
add "$(length "$DISCARD")" "$(length "$ITEMS")"
}
# NIL --> NUMBER
# Return the amount of discarded items.
function discarded_items {
echo "$(length "$DISCARD")"
}
# --------------------------------------
# NUMBER --> NIL
# Handle the return code of `mpv` properly, according
# to the cause.
function handle_return {
local return=$1
case $return in
4) # ^C
# rewind one file <<
ITEMS="$(car "$DISCARD") $ITEMS"
DISCARD="$(cdr "$DISCARD")"
;;
0) # normal
# put current file to discard
DISCARD="$(car "$ITEMS") $DISCARD"
ITEMS="$(cdr "$ITEMS")"
;;
2) # no file
# don't even discard it-- BURN IT
ITEMS="$(cdr "$ITEMS")"
;;
esac
}
# --------------------------------------
# invocation
DISCARD=""
ITEMS="$@"
while test -n "$ITEMS"; do
local item="$(car "$ITEMS")"
cat "${item}.txt" 2> /dev/null
echo "===$(add $(discarded_items) 1)/$(total_items)==="
eval "mpv ${ARGUMENTS} ${item}"
return=$?
handle_return "$return"
done

4
wyrics
View File

@ -71,7 +71,7 @@ function search {
local query="$1"
local query="$(echo "$query" | sed 's/ /+/g')"
gendl "https://songlyrics.com/index.php?section=search&searchW=${query}" \
curl -Ls "https://songlyrics.com/index.php?section=search&searchW=${query}" \
| sed '1,/serpresult/d' \
| grep "title=" \
| grep -v "<h3" \
@ -195,7 +195,7 @@ function blank_lines {
function lyrics {
local url="$1"
gendl "$url" \
curl -Ls "$url" \
| sed '1,/iComment-text">/d' \
| sed '/<\/div>/,$d' \
| blank_lines \