change Org Mode indentation
This commit is contained in:
parent
79964ec0dd
commit
3f5211e0b1
1 changed files with 160 additions and 160 deletions
320
README.org
320
README.org
|
|
@ -1,210 +1,210 @@
|
||||||
* tyt
|
* tyt
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
:header-args: :tangle tyt :shebang "#!/bin/sh"
|
:header-args: :tangle tyt :shebang "#!/bin/sh"
|
||||||
:END:
|
:END:
|
||||||
|
|
||||||
Terminal YouTube (*tyt*) is a small bash script that lets you play YouTube videos by query from the command line.
|
Terminal YouTube (*tyt*) is a small bash script that lets you play YouTube videos by query from the command line.
|
||||||
It is created via literate programming, you can find the code below.
|
It is created via literate programming, you can find the code below.
|
||||||
|
|
||||||
** Script
|
** Script
|
||||||
*** Dependencies
|
*** Dependencies
|
||||||
|
|
||||||
The dependencies of the script are:
|
The dependencies of the script are:
|
||||||
|
|
||||||
- [[https://stedolan.github.io/jq/][jq]]
|
- [[https://stedolan.github.io/jq/][jq]]
|
||||||
- [[https://mpv.io/][mpv]]
|
- [[https://mpv.io/][mpv]]
|
||||||
- [[https://ytdl-org.github.io/youtube-dl/][youtube-dl]]
|
- [[https://ytdl-org.github.io/youtube-dl/][youtube-dl]]
|
||||||
|
|
||||||
Please make sure those are available on your system.
|
Please make sure those are available on your system.
|
||||||
If you are using the [[https://nixos.org/][Nix]] package manager, you can run =nix-shell= in the project directory.
|
If you are using the [[https://nixos.org/][Nix]] package manager, you can run =nix-shell= in the project directory.
|
||||||
This will drop you into a shell with all requirements fulfilled.
|
This will drop you into a shell with all requirements fulfilled.
|
||||||
|
|
||||||
On the start of the script, it is checked if the dependencies are fulfilled.
|
On the start of the script, it is checked if the dependencies are fulfilled.
|
||||||
|
|
||||||
#+begin_src sh
|
#+begin_src sh
|
||||||
if ! command -v mpv &> /dev/null
|
if ! command -v mpv &> /dev/null
|
||||||
then
|
then
|
||||||
echo -ne "\e[1mmpv\e[0m was not found, please install it"
|
echo -ne "\e[1mmpv\e[0m was not found, please install it"
|
||||||
exit 1
|
exit 1
|
||||||
elif ! command -v youtube-dl &> /dev/null
|
elif ! command -v youtube-dl &> /dev/null
|
||||||
then
|
then
|
||||||
echo -ne "\e[1myoutube-dl\e[0m was not found, please install it"
|
echo -ne "\e[1myoutube-dl\e[0m was not found, please install it"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
*** Usage
|
*** Usage
|
||||||
|
|
||||||
This function prints the usage of the script.
|
This function prints the usage of the script.
|
||||||
|
|
||||||
#+begin_src sh
|
#+begin_src sh
|
||||||
function print_usage {
|
function print_usage {
|
||||||
echo "tyt [ (-a* | --alternative) | (-i | --interactive) ] \"QUERY\""
|
echo "tyt [ (-a* | --alternative) | (-i | --interactive) ] \"QUERY\""
|
||||||
}
|
}
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
*** Arguments
|
*** Arguments
|
||||||
|
|
||||||
At first we parse the arguments.
|
At first we parse the arguments.
|
||||||
We have the following flags:
|
We have the following flags:
|
||||||
|
|
||||||
- =a*|alternative= :: Alternative video (optional); You can parse any amount of alternatives (e.g. =-aaa=)
|
- =a*|alternative= :: Alternative video (optional); You can parse any amount of alternatives (e.g. =-aaa=)
|
||||||
- =i|interactive= :: Interactive mode; Shows the first 10 results and queries for a selection; If this flag is set, =-a= is ignored
|
- =i|interactive= :: Interactive mode; Shows the first 10 results and queries for a selection; If this flag is set, =-a= is ignored
|
||||||
|
|
||||||
Additionally we have exacly one mandatory quoted string as query.
|
Additionally we have exacly one mandatory quoted string as query.
|
||||||
|
|
||||||
#+begin_src sh
|
#+begin_src sh
|
||||||
alternative=0
|
alternative=0
|
||||||
format="flac"
|
format="flac"
|
||||||
interactive=false
|
interactive=false
|
||||||
help=false
|
help=false
|
||||||
|
|
||||||
for arg in "$@"
|
for arg in "$@"
|
||||||
do
|
do
|
||||||
case $arg in
|
case $arg in
|
||||||
-a*)
|
-a*)
|
||||||
alternative="${arg:1}"
|
alternative="${arg:1}"
|
||||||
alternative="${#alternative}"
|
alternative="${#alternative}"
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
--alternative)
|
--alternative)
|
||||||
alternative=1
|
alternative=1
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
-i|--interactive)
|
-i|--interactive)
|
||||||
interactive=true
|
interactive=true
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
-h|--help)
|
-h|--help)
|
||||||
help=true
|
help=true
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
,*)
|
,*)
|
||||||
other_arguments+=("$1")
|
other_arguments+=("$1")
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ "$help" = true ]
|
if [ "$help" = true ]
|
||||||
then
|
then
|
||||||
print_usage
|
print_usage
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${#other_arguments[@]}" != "1" ]
|
if [ "${#other_arguments[@]}" != "1" ]
|
||||||
then
|
then
|
||||||
print_usage
|
print_usage
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
query="${other_arguments[0]}"
|
query="${other_arguments[0]}"
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
*** Greeter
|
*** Greeter
|
||||||
|
|
||||||
If the arguments match, print a greeter.
|
If the arguments match, print a greeter.
|
||||||
|
|
||||||
#+begin_src sh
|
#+begin_src sh
|
||||||
echo -ne "\n \e[1m\ /\e[0m\n"
|
echo -ne "\n \e[1m\ /\e[0m\n"
|
||||||
echo -ne " \e[1m=======\e[0m\n"
|
echo -ne " \e[1m=======\e[0m\n"
|
||||||
echo -ne " \e[1m| \e[31mtyt\e[0m \e[1m|\e[0m\n"
|
echo -ne " \e[1m| \e[31mtyt\e[0m \e[1m|\e[0m\n"
|
||||||
echo -ne " \e[1m=======\e[0m\n\n"
|
echo -ne " \e[1m=======\e[0m\n\n"
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
*** Get URL
|
*** Get URL
|
||||||
|
|
||||||
To play a video, we need to get a valid URL.
|
To play a video, we need to get a valid URL.
|
||||||
Since there are sometimes parsing errors of the JSON response, we use an endless loop to try until we get a valid response.
|
Since there are sometimes parsing errors of the JSON response, we use an endless loop to try until we get a valid response.
|
||||||
The first /n/ URLs are saved if an alternative download is requested.
|
The first /n/ URLs are saved if an alternative download is requested.
|
||||||
|
|
||||||
#+begin_src sh
|
#+begin_src sh
|
||||||
i=0
|
i=0
|
||||||
|
|
||||||
if [ "$interactive" = true ]
|
if [ "$interactive" = true ]
|
||||||
then
|
then
|
||||||
n=10
|
n=10
|
||||||
else
|
else
|
||||||
n=$((alternative+1))
|
n=$((alternative+1))
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -ne "Searching for: \e[34m\e[1m$query\e[0m \r"
|
echo -ne "Searching for: \e[34m\e[1m$query\e[0m \r"
|
||||||
|
|
||||||
until results=$(youtube-dl --default-search "ytsearch" -j "ytsearch$n:$query") &> /dev/null
|
until results=$(youtube-dl --default-search "ytsearch" -j "ytsearch$n:$query") &> /dev/null
|
||||||
do
|
do
|
||||||
|
|
||||||
case $i in
|
case $i in
|
||||||
0)
|
0)
|
||||||
appendix=" "
|
appendix=" "
|
||||||
;;
|
;;
|
||||||
1)
|
1)
|
||||||
appendix=". "
|
appendix=". "
|
||||||
;;
|
;;
|
||||||
2)
|
2)
|
||||||
appendix=".. "
|
appendix=".. "
|
||||||
;;
|
;;
|
||||||
,*)
|
,*)
|
||||||
appendix="..."
|
appendix="..."
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
echo -ne "Searching for: \e[34m\e[1m$query\e[0m $appendix\r"
|
echo -ne "Searching for: \e[34m\e[1m$query\e[0m $appendix\r"
|
||||||
|
|
||||||
i=$(((i + 1) % 4))
|
i=$(((i + 1) % 4))
|
||||||
sleep 1
|
sleep 1
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
echo -ne "Searching for: \e[34m\e[1m$query\e[0m \n"
|
echo -ne "Searching for: \e[34m\e[1m$query\e[0m \n"
|
||||||
|
|
||||||
urls=$(echo $results | jq '.webpage_url' | tr -d '"')
|
urls=$(echo $results | jq '.webpage_url' | tr -d '"')
|
||||||
titles=$(echo $results | jq '.fulltitle' | tr -d '"')
|
titles=$(echo $results | jq '.fulltitle' | tr -d '"')
|
||||||
uploaders=$(echo $results | jq '.uploader' | tr -d '"')
|
uploaders=$(echo $results | jq '.uploader' | tr -d '"')
|
||||||
|
|
||||||
OLDIFS=$IFS
|
OLDIFS=$IFS
|
||||||
IFS=$'\n'
|
IFS=$'\n'
|
||||||
urls=($urls)
|
urls=($urls)
|
||||||
titles=($titles)
|
titles=($titles)
|
||||||
uploaders=($uploaders)
|
uploaders=($uploaders)
|
||||||
IFS=$OLDIFS
|
IFS=$OLDIFS
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
*** Interactive selection
|
*** Interactive selection
|
||||||
|
|
||||||
If the interactive flag is present, show the first ten results and query for a video to play.
|
If the interactive flag is present, show the first ten results and query for a video to play.
|
||||||
|
|
||||||
#+begin_src sh
|
#+begin_src sh
|
||||||
if [ "$interactive" = true ]
|
if [ "$interactive" = true ]
|
||||||
then
|
then
|
||||||
echo ""
|
echo ""
|
||||||
selections=(0 1 2 3 4 5 6 7 8 9)
|
selections=(0 1 2 3 4 5 6 7 8 9)
|
||||||
for i in ${selections[@]}
|
for i in ${selections[@]}
|
||||||
do
|
do
|
||||||
echo -ne " \e[1m$i\e[0m: ${titles[$i]} (\e[33m\e[1m${uploaders[$i]}\e[0m)\n"
|
echo -ne " \e[1m$i\e[0m: ${titles[$i]} (\e[33m\e[1m${uploaders[$i]}\e[0m)\n"
|
||||||
done
|
done
|
||||||
echo -ne "\nSelection: "
|
echo -ne "\nSelection: "
|
||||||
read selection
|
read selection
|
||||||
while [[ ! "${selections[@]}" =~ "${selection}" ]]
|
while [[ ! "${selections[@]}" =~ "${selection}" ]]
|
||||||
do
|
do
|
||||||
echo -ne "Not valid, try again: "
|
echo -ne "Not valid, try again: "
|
||||||
read selection
|
read selection
|
||||||
done
|
done
|
||||||
echo ""
|
echo ""
|
||||||
url=${urls[$selection]}
|
url=${urls[$selection]}
|
||||||
title=${titles[$selection]}
|
title=${titles[$selection]}
|
||||||
uploader=${uploaders[$selection]}
|
uploader=${uploaders[$selection]}
|
||||||
else
|
else
|
||||||
url=${urls[$alternative]}
|
url=${urls[$alternative]}
|
||||||
title=${titles[$alternative]}
|
title=${titles[$alternative]}
|
||||||
uploader=${uploaders[$alternative]}
|
uploader=${uploaders[$alternative]}
|
||||||
fi
|
fi
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
*** Play video
|
*** Play video
|
||||||
|
|
||||||
Finally the video is played via mpv.
|
Finally the video is played via mpv.
|
||||||
|
|
||||||
#+begin_src sh
|
#+begin_src sh
|
||||||
echo -ne "Playing: \e[32m\e[1m$title\e[0m (\e[33m\e[1m$uploader\e[0m)\n"
|
echo -ne "Playing: \e[32m\e[1m$title\e[0m (\e[33m\e[1m$uploader\e[0m)\n"
|
||||||
mpv $url &> /dev/null
|
mpv $url &> /dev/null
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue