You are not logged in.

#1 2019-02-27 17:42:07

cyaniventer
Member
From: Moon
Registered: 2019-02-27
Posts: 3
Website

bashtube-dl > shell script which is a frontend for youtube-dl

this is my first shell script ever and I've never written any program better than this so that script might be very messy (idk, it works).  also let me know if this is not the right place to post this, I'm new here.

here it is: https://github.com/cyaniventer/bashtube-dl

#!/bin/bash

# bashtube-dl
# -----------------------------------
# author    : Cyaniventer
# project   : https://github.com/cyaniventer/bashtube-dl
# url       : https://cyaniventer.keybase.pub
# license   : GPLv3 (https://choosealicense.com/licenses/gpl-3.0/)

btdlcv=0.2.3 # current version
configDir=~/.config/bashtube-dl # config directory
cacheDir=~/.cache/bashtube-dl # cache directory

# only for testing - COMMENT IN PRODUCTION
#rm $configDir/initial-config
#btdlcv=9.9.9

mkdir -p $configDir
mkdir -p $cacheDir

# checks for help flag
if [ "$1" = "--help" ] || [ "$1" = "help" ] || [ "$2" = "--help" ] || [ "$2" = "help" ]; then
    dialog  --backtitle "bashtube-dl v"$btdlcv \
            --title "bashtube-dl help" \
            --msgbox "\nthis is bashtube-dl help section and you're seeing this because you've used help flag (help/--help). \nfor bashtube-dl to function properly you should have these packages installed: dialog, youtube-dl, wget, make & tar. \n\n--help (or help            to display this message\n--reset (or reset)         to reset bashtube-dl config\n--update (or update)       to check for update" 15 60
    clear
    exit 0
fi

# checks for reset flag
if [ "$1" = "--reset" ] || [ "$1" = "reset" ] || [ "$2" = "--reset" ] || [ "$2" = "reset" ]; then
    dialog  --backtitle "bashtube-dl v"$btdlcv \
            --title "bashtube-dl reset wizard" \
            --msgbox "\nbashtube-dl config folder will be deleted. bashtube-dl will guide you through initial configuration again next time you run bashtube-dl." 8 60
    rm $configDir/initial-config
    clear
    exit 0
fi

# checks for update flag - updater
if [ "$1" = "--update" ] || [ "$1" = "update" ] || [ "$2" = "--update" ] || [ "$2" = "update" ]; then
    btdllv=$(curl -sO "https://cyaniventer.github.io/bashtube-dl/bashtube-dl-version" && cat bashtube-dl-version)
    if [ "$btdlcv" == "$btdllv" ]; then
        dialog  --backtitle "bashtube-dl v"$btdlcv \
            --title "bashtube-dl updater" \
            --msgbox "\nbashtube-dl is up-to-date" 7 60
        clear
        exit 0
    else
        rm -rf $cacheDir
        mkdir -p $cacheDir/update_extracted
        wget -c --directory-prefix=$cacheDir "https://github.com/cyaniventer/bashtube-dl/archive/v$btdllv.tar.gz"
        tar -xzf $cacheDir/v$btdllv.tar.gz -C $cacheDir/update_extracted
        cd $cacheDir/update_extracted/bashtube-dl-$btdllv && dialog --backtitle "bashtube-dl v"$btdlcv --title "bashtube-dl updater" --msgbox "\n You can check Makefile which will be printed after this menu. Installing bashtube-dl requires sudo permission which uses that Makefile so you can check for malicious script." 10 60 && clear && cat Makefile && sudo make install
        dialog  --backtitle "bashtube-dl v"$btdlcv \
                --title "bashtube-dl updater" \
                --msgbox "\n bashtube-dl was updated to v$btdllv." 7 60
        clear
        exit 0
    fi
fi

# this if block checks if initial config was completed
if ! cat $configDir/initial-config | grep initial_config_complete-true >/dev/null 2>&1; then
    dialog  --backtitle "bashtube-dl v"$btdlcv \
            --title "bashtube-dl initial config (~/.config/bashtube-dl)" \
            --msgbox "\nyou have to fill this only once and you can change it anytime by editing ~/.config/bashtube-dl/ files.\n(I will make this easier later)" 10 60

    # this first checks if youtube-dl_check is present if it is then it will skip this if block
    if ! cat $configDir/initial-config | grep "youtube-dl_check">/dev/null 2>&1; then
        dialog  --backtitle "bashtube-dl v"$btdlcv \
                --title "bashtube-dl initial config (~/.config/bashtube-dl)" \
                --yesno "\nshould bashtube-dl check if youtube-dl is installed everytime it runs?\n\nThis option wouldn't slow down the program because bashtube-dl already checks lots of stuff using the same command so you should probably chose ok." 12 60

        # return status of zero indicates okay
        if [ "$?" = "0" ]; then
            echo "youtube-dl_check-true" >> $configDir/initial-config
        else
            echo "youtube-dl_check-false" >> $configDir/initial-config
        fi
    fi
    # youtube-dl_check ends here

    # this first checks if show_title is present if it is then it will skip this if block
    if ! cat $configDir/initial-config | grep "show_title">/dev/null 2>&1; then
        dialog  --backtitle "bashtube-dl v"$btdlcv \
                --title "bashtube-dl initial config (~/.config/bashtube-dl)" \
                --yesno "\nbashtube-dl will not show title in download wizard. chose no to change this. \n\n*THIS WILL SLOW DOWN bashtube-dl SO CHOSE YES*" 12 60

        # return status of zero indicates okay
        if [ "$?" = "0" ]; then
            echo "show_title-false" >> $configDir/initial-config
        else
            echo "show_title-true" >> $configDir/initial-config
        fi
    fi
    # show_title ends here

    # this first checks if defaultItem is present if it is then it will skip this if block
    if ! cat $configDir/initial-config | grep "defaultItem">/dev/null 2>&1; then
        defaultItem=$(dialog --default-item '1' --backtitle "bashtube-dl v"$btdlcv --title "bashtube-dl initial config (~/.config/bashtube-dl)" --menu "what shoule be the default option when downloading? \n\nNote: you will be able to select other options while downloading but this will be selected by default, just like this menu default is 1 but you can select 2 & 3." 15 60 10 "1" "download (default)" "2" "extract audio" "3" "select video format" --output-fd 1)
        case $defaultItem in
        01 | 1)
            echo "defaultItem 1" >> $configDir/initial-config
            ;;
        02 | 2)
            echo "defaultItem 2" >> $configDir/initial-config
            ;;
        03 | 3)
            echo "defaultItem 3" >> $configDir/initial-config
            ;;
        esac
    fi
    # defaultItem ends here

    echo "initial_config_complete-true" >> $configDir/initial-config # this saves that initial-config was completed and bashtube-dl shouldn't run it again
    dialog  --backtitle "bashtube-dl v"$btdlcv \
            --title "bashtube-dl initial config (~/.config/bashtube-dl)" \
            --msgbox "\n*try bashtube-dl --help*\n\nInitial configuration was completed and you can look at it here: $configDir \n\nYou shouldn't change those manually unless you know what you're doing." 12 60
    clear
fi

# check if youtube-dl is installed (optional)
if ! cat $configDir/initial-config | grep youtube-dl_check-false >/dev/null 2>&1; then
    if ! type youtube-dl >/dev/null 2>&1; then
        youtube-dlError=$(dialog --default-item '$defaultItem' --backtitle "bashtube-dl v"$btdlcv --title "youtube-dl not installed" --menu "youtube-dl is not installed, you'll have to install it before running bashtube-dl. \n\nselect an option" 15 60 10 "1" "abort" "2" "download youtube-dl" "3" "ignore forever" --output-fd 1)
        case $youtube-dlError in
            01 | 1)
                clear
                exit 0
                ;;
            02 | 2)
                dialog  --backtitle "bashtube-dl v"$btdlcv \
                --title "bashtube-dl help with youtube-dl installation" \
                --msgbox "\nyou should follow this link: https://github.com/rg3/youtube-dl" 10 60
                ;;
            03 | 3)
                echo "youtube-dl_check-false" >> $configDir/initial-config
                ;;
        esac
    fi
fi

# checks if user has given url in flags
if ! echo $1 | grep http | grep :// >/dev/null 2>&1; then # checks if user has given url, if no then follow below - if yes then go to download section
    linkToDownload=$(dialog --backtitle "bashtube-dl v"$btdlcv --title "bashtube-dl - a frontend for youtube-dl" --inputbox "hint: ctrl+shift+v instead of ctrl+v. enter video url: " 10 60  --output-fd 1)
else
    linkToDownload=$1
fi

# gets title for linkToDownload (optional)
if ! cat $configDir/initial-config | grep show_title-false >/dev/null 2>&1; then
    titleForLink=$(wget --quiet -O - $linkToDownload | sed -n -e 's!.*<title>\(.*\)</title>.*!\1!p')
fi

# sets defaultItem for download wizard
if ! cat $configDir/initial-config | grep defaultItem >/dev/null 2>&1; then
    defaultItem="1" # if defaultItem is not set then set is as 1
else
    if cat $configDir/initial-config | grep "defaultItem 1" >/dev/null 2>&1; then
    defaultItem="1" # if defaultItem is 1 then set is as 1
    fi
    if cat $configDir/initial-config | grep "defaultItem 2" >/dev/null 2>&1; then
    defaultItem="2" # if defaultItem is 2 then set is as 2
    fi
    if cat $configDir/initial-config | grep "defaultItem 3" >/dev/null 2>&1; then
    defaultItem="3" # if defaultItem is 3 then set is as 3
    fi
fi

# download section - now we have the download link stored at $linkToDownload
if ! cat $configDir/initial-config | grep show_title-false >/dev/null 2>&1; then
    whatToDoWithLink=$(dialog --default-item '$defaultItem' --backtitle "bashtube-dl v"$btdlcv --title "bashtube-dl download wizard" --menu "Title\n$titleForLink \n\nselect an option" 15 60 10 "1" "download" "2" "extract audio" "3" "select video format" --output-fd 1)
    else
    whatToDoWithLink=$(dialog --default-item '$defaultItem' --backtitle "bashtube-dl v"$btdlcv --title "bashtube-dl download wizard" --menu "\nselect an option" 15 60 10 "1" "download" "2" "extract audio" "3" "select video format" --output-fd 1)
fi

case $whatToDoWithLink in
        01 | 1)
            youtube-dl --ignore-config --continue --console-title --no-warnings --ignore-errors --no-playlist --format bestvideo+bestaudio/best --output "%(title)s.%(ext)s" "$linkToDownload"
            ;;
        02 | 2)
            youtube-dl --ignore-config --continue --console-title --no-warnings --no-playlist --format bestaudio/best --extract-audio --audio-format "mp3" --audio-quality 0 --output "%(title)s.%(ext)s" "$linkToDownload"
            ;;
        03 | 3)
            youtube-dl -F "$linkToDownload" > $cacheDir/format_codes
            formatCode=$(dialog --backtitle "bashtube-dl v"$btdlcv --title "bashtube-dl download wizard" --inputbox "$(cat $cacheDir/format_codes)\n\n enter format code below" 50 100  --output-fd 1)
            youtube-dl -f $formatCode --ignore-config --continue --console-title --no-warnings --ignore-errors --no-playlist --output "%(title)s.%(ext)s" "$linkToDownload"
            ;;
esac

dialog  --backtitle "bashtube-dl v"$btdlcv \
            --title "bashtube-dl download complete" \
            --msgbox "\nyour file has been downloaded (I hope so)." 7 60
clear
# end program

Offline

#2 2019-02-27 19:43:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: bashtube-dl > shell script which is a frontend for youtube-dl

The first 50 or so lines can be reduced to a dozen with a loop through all the arguments and a case statement, or by using getopt(s).

After that, you have a useless use of cat and silly use of grep.  The following two lines are equivalent:

if ! cat $configDir/initial-config | grep initial_config_complete-true >/dev/null 2>&1; then

if ! grep -q initial_config_complete-true $configDir/initial-config; then

Then later you pipe grep to grep which is pointless, just use `grep -q` and the actual pattern you are looking for.  Also, in that line you are piping in from echo $1 in which case you should just use bash string manipulation instead.

Overall, though, I have no idea what this is supposed to do.  What's the end goal, or what purpose does this serve?

EDIT: you seem to frequently do a lot of gymnastics piping cat through multiple greps for your config file.  Just use a sane config file format to start with.  Perhaps just a file with shell variables that you can source at the start of the script would be best, then you can just test the variables rather than using all those other tools and crazy pipelines.

Last edited by Trilby (2019-02-27 19:46:37)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2019-02-28 10:15:50

cyaniventer
Member
From: Moon
Registered: 2019-02-27
Posts: 3
Website

Re: bashtube-dl > shell script which is a frontend for youtube-dl

Trilby wrote:

After that, you have a useless use of cat and silly use of grep.  The following two lines are equivalent:

if ! cat $configDir/initial-config | grep initial_config_complete-true >/dev/null 2>&1; then
if ! grep -q initial_config_complete-true $configDir/initial-config; then

Oh! I didn't know about that.

Trilby wrote:

Then later you pipe grep to grep which is pointless, just use `grep -q` and the actual pattern you are looking for.  Also, in that line you are piping in from echo $1 in which case you should just use bash string manipulation instead.

I did that double pipe to catch both http:// & https://, so first grep looks for `http` then second grep looks for `://`. This way I catch both https:// & http:// in single command, can we do that with one grep? (maybe like `grep http:// || https://`)

Trilby wrote:

Overall, though, I have no idea what this is supposed to do.  What's the end goal, or what purpose does this serve?

this makes downloading with youtube-dl easier (no lol, its same. so just a wrapper for youtube-dl).

Trilby wrote:

EDIT: you seem to frequently do a lot of gymnastics piping cat through multiple greps for your config file.  Just use a sane config file format to start with.  Perhaps just a file with shell variables that you can source at the start of the script would be best, then you can just test the variables rather than using all those other tools and crazy pipelines.

yes I should try to improve it. thank you for checking it.

Offline

#4 2019-02-28 10:34:13

ayekat
Member
Registered: 2011-01-17
Posts: 1,589

Re: bashtube-dl > shell script which is a frontend for youtube-dl

cyaniventer wrote:

can we do that with one grep? (maybe like `grep http:// || https://`)

grep 'https\?://'

Maybe learning about regex would help. But this is really just for this particular question—overall, I also get the feeling that this script does a lot of things in an overly complicated/cumbersome way. But it's hard to tell without knowing its purpose.

this makes downloading with youtube-dl easier (no lol, its same. so just a wrapper for youtube-dl).

youtube-dl seems already pretty easy to me. It's:

youtube-dl {url}

(plus maybe a few options like `-x` for only audio, or others—but the man page is IMHO rather extensive)

configDir=~/.config/bashtube-dl # config directory
cacheDir=~/.cache/bashtube-dl # cache directory

I appreciate you don't drop your files directly into the user's home directory, but I urge you to read up on the XDG base directory specification variables. ~/.config and ~/.cache are only the default directories if XDG_CONFIG_HOME and XDG_CACHE_HOME are not set. I'd suggest something like this:

configDir=${XDG_CONFIG_HOME:-~/.config}/bashtube-dl
cacheDir=${XDG_CACHE_HOME:-~/.cache}/bashtube-dl

pkgshackscfgblag

Offline

#5 2019-02-28 13:49:36

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: bashtube-dl > shell script which is a frontend for youtube-dl

cyaniventer wrote:
Trilby wrote:

Overall, though, I have no idea what this is supposed to do.  What's the end goal, or what purpose does this serve?

this makes downloading with youtube-dl easier

Easier in what way?  It looks *far far* more complicated.  Many dialog screens and prompts and questions and a configuration file.  This is far from easy.

Complicated can be ok if necessary to add some new behavior.  So what I was getting at is what features, options, or behaviors does this add to youtube-dl?  Or what aspect of using youtube-dl is made easier with this script?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2019-02-28 15:11:53

cyaniventer
Member
From: Moon
Registered: 2019-02-27
Posts: 3
Website

Re: bashtube-dl > shell script which is a frontend for youtube-dl

Trilby wrote:

Easier in what way?  It looks *far far* more complicated.  Many dialog screens and prompts and questions and a configuration file.  This is far from easy.

Complicated can be ok if necessary to add some new behavior.  So what I was getting at is what features, options, or behaviors does this add to youtube-dl?  Or what aspect of using youtube-dl is made easier with this script?

you are actually right, it doesn't add anything nor using youtube-dl is easier with this, idk I had some idea in my mind to make it easier for new users & for me but it failed and now after removing useless and broken functions this doesn't do anything. I think I'll archive it and move on hmm

Edit: or maybe I'll remove all those config thing and make it easy by just asking download url and showing user bunch of options so that they don't have to manually search youtube-dl man page for some functions, is that a good idea?

Last edited by cyaniventer (2019-02-28 15:13:45)

Offline

#7 2019-02-28 15:24:27

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,390

Re: bashtube-dl > shell script which is a frontend for youtube-dl

Trillby wrote:

Easier in what way?  It looks *far far* more complicated

...which does not necessarily mean it is not easier :-P

Last edited by kokoko3k (2019-02-28 15:25:13)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#8 2019-02-28 16:22:20

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: bashtube-dl > shell script which is a frontend for youtube-dl

kokoko3k, I agree, but if we are going to add complexity, we should be able to show some benefit.  That's why I was curious what this complexity bought for us.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#9 2019-03-01 06:09:14

Docbroke
Member
From: India
Registered: 2015-06-13
Posts: 1,433

Re: bashtube-dl > shell script which is a frontend for youtube-dl

cyaniventer wrote:

Edit: or maybe I'll remove all those config thing and make it easy by just asking download url and showing user bunch of options so that they don't have to manually search youtube-dl man page for some functions, is that a good idea?

I have a tiny script that does some simple operations without needing to look at youtube-dl man pages. This may help in getting started.

#!/bin/bash

[[ ! $1 ]] && exit

shopt -s lastpipe

url="$1"
youtube-dl -F --no-playlist "$url"
echo "choose quality (from first column), to merge use x+y"
read quality
echo choose "p) play d) download"
read -n 1 choice 
case $choice in
    p) mpv --ytdl-format="$quality" --no-resume-playback "$url" ;;
    d) youtube-dl -f "$quality"  "$url" ;;
esac

Offline

Board footer

Powered by FluxBB