Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[SCRIPT] Install youtube-dl & clipboardtool (tested on xfce)

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
Bas Koning
Posts: 12
Joined: 2011-08-13 03:10

[SCRIPT] Install youtube-dl & clipboardtool (tested on xfce)

#1 Post by Bas Koning »

Save script as installyoutube-dl.sh
As root, in a terminal, allow execution of the script:

Code: Select all

chmod a+x installyoutube-dl.sh
Run script:
(Note: the creation of a desktop launcher part was only tested on wheezy - xfce!)

Code: Select all

bash installyoutube-dl.sh
Complete script:

Code: Select all

#!/bin/bash

if [ "$(id -u)" != "0" ];
then
  echo
  echo "Must be root user to run this script"
  echo
  exit 1
fi

SHA="a618d4951ef353253d828384d51289e24ab00f1266c9d60224e02517eea464b4"
VERSION="2013.06.34.2"

DOWNLOADFROM="http://youtube-dl.org/downloads/"$VERSION"/youtube-dl"
INFOSITE="http://rg3.github.io/youtube-dl/"

#downloads youtube-dl
#checks checksum
#installs to /usr/local/bin
#upgrades with pip
#writes downloadyoutube
#writes desktop launcher

echo 'download youtube-dl to /tmp'
MUSTDOWNLOAD=0
echo 'check if file already exist'
if [ -f /tmp/youtube-dl ] && [ -s /tmp/youtube-dl ]
then
  echo 'file exist'
  echo 'creating checksum for found file'
  SHA3=$(sha256sum /tmp/youtube-dl)
  echo 'checking checksums'
  if [[ "$SHA3" == *"$SHA"* ]]; then
    echo 'checksums matched'
    MUSTDOWNLOAD=1
  else
    echo 'checksums did not match'
  fi
else
  echo 'file does not exist'
fi
if [ $MUSTDOWNLOAD -eq 0 ]
then
  echo 'downloading file'
  wget $DOWNLOADFROM -O /tmp/youtube-dl
  echo 'check if downloaded file exist and has size'
  if [ ! -f /tmp/youtube-dl ] || [ ! -s /tmp/youtube-dl ] 
  then
    echo 
    echo 'could not download file..'
    echo 'are you connected to the internet?'
    echo 'perhaps there is a new version'
    echo 'check '$INFOSITE
    echo 'and update script perhaps'
    echo 
    if [ -f /tmp/youtube-dl ]
    then
      rm /tmp/youtube-dl
    fi
    exit 2
  fi
fi

echo 'creating checksum for downloaded file'
SHA2=$(sha256sum /tmp/youtube-dl)
echo 'checking checksums'
if [[ "$SHA2" == *"$SHA"* ]]; then
  echo 'checksum matched'
  echo 'install to /usr/local/bin'
  cp /tmp/youtube-dl /usr/local/bin/youtube-dl
  chmod a+x /usr/local/bin/youtube-dl

  echo 'installing and upgrading with pip'
  apt-get -y install python-pip
  pip install --upgrade youtube_dl

  echo 'installing xclip tool to grab selection that downloadyoutube uses'
  apt-get -y install xclip

  echo 'writing downloadyoutube to /usr/local/bin'
  DEST='/usr/local/bin/downloadyoutube'
  echo '#!/bin/bash' > $DEST
  echo '' >> $DEST 
  echo 'echo' >> $DEST 
  echo 'echo '\''downloadyoutube'\' >> $DEST 
  echo 'echo' >> $DEST 
  echo 'echo '\''if downloading stops working, as root try'\' >> $DEST 
  echo 'echo '\'' pip install --upgrade youtube_dl'\' >> $DEST 
  echo 'echo '\''to upgrade youtube_dl to the latest version'\' >> $DEST 
  echo 'echo' >> $DEST 
  echo 'CLIPBOARD=$(xclip -o)' >> $DEST 
  echo 'if [ ${#CLIPBOARD} -eq 0 ]' >> $DEST 
  echo 'then' >> $DEST 
  echo '  echo' >> $DEST  
  echo '  echo '\''select youtube-url, then try again'\' >> $DEST 
  echo '  echo' >> $DEST  
  echo '  read -t 10' >> $DEST 
  echo '  exit 1' >> $DEST 
  echo 'fi' >> $DEST 
  echo 'TEST1="https://www.youtube.com/watch?v"' >> $DEST 
  echo 'TEST2="http://youtu.be/"' >> $DEST 
  echo 'TEST3="http://www.youtube.com/watch?v"' >> $DEST 
  echo '' >> $DEST
  echo 'if [[ "$CLIPBOARD" == *"$TEST1"* ]] || [[ "$CLIPBOARD" == *"$TEST2"* ]] || [[ "$CLIPBOARD" == *"$TEST3"* ]]' >> $DEST  
  echo 'then' >> $DEST 
  echo '  OLD=$PWD' >> $DEST 
  echo '  cd ~/Downloads' >> $DEST 
  echo '  youtube-dl $CLIPBOARD' >> $DEST 
  echo '  cd $OLD' >> $DEST 
  echo 'else' >> $DEST 
  echo '  echo' >> $DEST 
  echo '  echo '\''select youtube-url, then try again'\' >> $DEST 
  echo '  echo' >> $DEST  
  echo '  read -t 10' >> $DEST 
  echo '  exit 2' >> $DEST 
  echo 'fi' >> $DEST 
  echo 'echo' >> $DEST 
  echo 'echo '\''Download completed'\' >> $DEST
  echo 'echo '\''check your ~/Downloads folder'\' >> $DEST 
  echo 'echo' >> $DEST  
  echo 'read -t 5' >> $DEST 
  echo 'exit 0' >> $DEST 
  chmod a+x $DEST

  echo 'writing desktop launcher'
  echo 
  echo '  type username:'
  read USERNAME
  DEST1='/home/'$USERNAME'/Desktop/downloadyoutube.desktop'
  echo '[Desktop Entry]' > $DEST1
  echo 'Version=1.0' >> $DEST1
  echo 'Type=Application' >> $DEST1
  echo 'Name=DownloadYT' >> $DEST1
  echo 'Comment=' >> $DEST1
  echo 'Exec=bash downloadyoutube' >> $DEST1
  echo 'Icon=bottom' >> $DEST1
  echo 'Path=' >> $DEST1
  echo 'Terminal=true' >> $DEST1
  echo 'StartupNotify=true' >> $DEST1
  chown $USERNAME:$USERNAME $DEST1 
  chmod 644 $DEST1

  echo 
  echo 'Done'
  echo 
  echo 'to use: type in a terminal:'
  echo 'youtube-dl https://www.youtube.com/somevideo.url'
  echo 
  echo 'or select/copy some youtube-url in your browser,'
  echo 'then click DownloadYT on the desktop to start download'
  echo 
  exit 0
else
  rm /tmp/youtube-dl
  echo 
  echo 'checksums did not match'
  echo 'perhaps there is a new version'
  echo 'check '$INFOSITE
  echo 'and update script perhaps'
  echo 
  exit 3
fi
Explanation of the script:
Tells the system to use bash as the interpreter.

Code: Select all

#!/bin/bash
If the result of 'id -u' is not zero (person running script is not root), then give a message, and exit with an error code.

Code: Select all

if [ "$(id -u)" != "0" ];
then
  echo
  echo "Must be root user to run this script"
  echo
  exit 1
fi
Checksum, version and downloadlink, as found on INFOSITE

Code: Select all

SHA="a618d4951ef353253d828384d51289e24ab00f1266c9d60224e02517eea464b4"
VERSION="2013.06.34.2"

DOWNLOADFROM="http://youtube-dl.org/downloads/"$VERSION"/youtube-dl"
INFOSITE="http://rg3.github.io/youtube-dl/"
Some info in comments about what this script will do

Code: Select all

#downloads youtube-dl
#checks checksum
#installs to /usr/local/bin
#upgrades with pip
#writes downloadyoutube
#writes desktop launcher
This set MUSTDOWNLOAD to zero. Then it checks if the file is already downloaded in /tmp & not empty, in which case the checksum is calculated and checked. If all went well, download is not needed, so sets MUSTDOWNLOAD to one.

Code: Select all

echo 'download youtube-dl to /tmp'
MUSTDOWNLOAD=0
echo 'check if file already exist'
if [ -f /tmp/youtube-dl ] && [ -s /tmp/youtube-dl ]
then
  echo 'file exist'
  echo 'creating checksum for found file'
  SHA3=$(sha256sum /tmp/youtube-dl)
  echo 'checking checksums'
  if [[ "$SHA3" == *"$SHA"* ]]; then
    echo 'checksums matched'
    MUSTDOWNLOAD=1
  else
    echo 'checksums did not match'
  fi
else
  echo 'file does not exist'
fi
If MUSTDOWNLOAD is zero, try to download the file to /tmp. Then it checks if there is a file and the size is not zero. On failure: show messages, remove empty file if it exist, and exit with an error code of 2.

Code: Select all

if [ $MUSTDOWNLOAD -eq 0 ]
then
  echo 'downloading file'
  wget $DOWNLOADFROM -O /tmp/youtube-dl
  echo 'check if downloaded file exist and has size'
  if [ ! -f /tmp/youtube-dl ] || [ ! -s /tmp/youtube-dl ] 
  then
    echo 
    echo 'could not download file..'
    echo 'are you connected to the internet?'
    echo 'perhaps there is a new version'
    echo 'check '$INFOSITE
    echo 'and update script perhaps'
    echo 
    if [ -f /tmp/youtube-dl ]
    then
      rm /tmp/youtube-dl
    fi
    exit 2
  fi
fi
Create sha256 checksum for file

Code: Select all

echo 'creating checksum for downloaded file'
SHA2=$(sha256sum /tmp/youtube-dl)
If the checksums matched, copy youtube-dl to /usr/local/bin, which is in all users path by default, so people can run the command without having to type the full path. Sets flags on file for all users (a) to allow execution (x) with chmod

Code: Select all

echo 'checking checksums'
if [[ "$SHA2" == *"$SHA"* ]]; then
  echo 'checksum matched'
  echo 'install to /usr/local/bin'
  cp /tmp/youtube-dl /usr/local/bin/youtube-dl
  chmod a+x /usr/local/bin/youtube-dl
Installs python-pip and upgrade youtube-dl to latest version

Code: Select all

  echo 'installing and upgrading with pip'
  apt-get -y install python-pip
  pip install --upgrade youtube_dl
Installs xclip, so downloadyoutube is able to read the contents of the clipboard

Code: Select all

  echo 'installing xclip tool to grab selection that downloadyoutube uses'
  apt-get -y install xclip
Writes script called downloadyoutube to /usr/local/bin
Script will:
-set default interpreter to bash
-show some messages to user
-read clipboard contents with xclip
-checks if the content is empty, if so, show messages and wait 10 seconds, then exit with error code 1
-if the content is not empty, create three TEST values
-if the contents contain one of these three values (a valid youtube-url is found), copy current folder (PWD) to OLD, cd to ~/Downloads, run youtube-dl with the url as argument, then cd back to OLD
-if the contents do not contain any of these three TEST values, a message is shown, then exit after 10 seconds with error code 2
-if all went well, show messages, and exit after 5 seconds.

When script is written, sets the execution allowed flag for all users with chmod.

Code: Select all

  echo 'writing downloadyoutube to /usr/local/bin'
  DEST='/usr/local/bin/downloadyoutube'
  echo '#!/bin/bash' > $DEST
  echo '' >> $DEST 
  echo 'echo' >> $DEST 
  echo 'echo '\''downloadyoutube'\' >> $DEST 
  echo 'echo' >> $DEST 
  echo 'echo '\''if downloading stops working, as root try'\' >> $DEST 
  echo 'echo '\'' pip install --upgrade youtube_dl'\' >> $DEST 
  echo 'echo '\''to upgrade youtube_dl to the latest version'\' >> $DEST 
  echo 'echo' >> $DEST 
  echo 'CLIPBOARD=$(xclip -o)' >> $DEST 
  echo 'if [ ${#CLIPBOARD} -eq 0 ]' >> $DEST 
  echo 'then' >> $DEST 
  echo '  echo' >> $DEST  
  echo '  echo '\''select youtube-url, then try again'\' >> $DEST 
  echo '  echo' >> $DEST  
  echo '  read -t 10' >> $DEST 
  echo '  exit 1' >> $DEST 
  echo 'fi' >> $DEST 
  echo 'TEST1="https://www.youtube.com/watch?v"' >> $DEST 
  echo 'TEST2="http://youtu.be/"' >> $DEST 
  echo 'TEST3="http://www.youtube.com/watch?v"' >> $DEST 
  echo '' >> $DEST
  echo 'if [[ "$CLIPBOARD" == *"$TEST1"* ]] || [[ "$CLIPBOARD" == *"$TEST2"* ]] || [[ "$CLIPBOARD" == *"$TEST3"* ]]' >> $DEST  
  echo 'then' >> $DEST 
  echo '  OLD=$PWD' >> $DEST 
  echo '  cd ~/Downloads' >> $DEST 
  echo '  youtube-dl $CLIPBOARD' >> $DEST 
  echo '  cd $OLD' >> $DEST 
  echo 'else' >> $DEST 
  echo '  echo' >> $DEST 
  echo '  echo '\''select youtube-url, then try again'\' >> $DEST 
  echo '  echo' >> $DEST  
  echo '  read -t 10' >> $DEST 
  echo '  exit 2' >> $DEST 
  echo 'fi' >> $DEST 
  echo 'echo' >> $DEST 
  echo 'echo '\''Download completed'\' >> $DEST
  echo 'echo '\''check your ~/Downloads folder'\' >> $DEST 
  echo 'echo' >> $DEST  
  echo 'read -t 5' >> $DEST 
  echo 'exit 0' >> $DEST 
  chmod a+x $DEST
Asks for a USERNAME
Writes a desktop launcher to downloadyoutube
sets owner/group to USERNAME
Sets read/write/execute flags for file to 644 (read/write for owner, read only for rest, no execute)
*tested in xfce only!*
Gnome (and other) users can try to include and run this part, but perhaps will need to create a desktop launcher to downloadyoutube by hand

Code: Select all

  echo 'writing desktop launcher'
  echo 
  echo '  type username:'
  read USERNAME
  DEST1='/home/'$USERNAME'/Desktop/downloadyoutube.desktop'
  echo '[Desktop Entry]' > $DEST1
  echo 'Version=1.0' >> $DEST1
  echo 'Type=Application' >> $DEST1
  echo 'Name=DownloadYT' >> $DEST1
  echo 'Comment=' >> $DEST1
  echo 'Exec=bash downloadyoutube' >> $DEST1
  echo 'Icon=bottom' >> $DEST1
  echo 'Path=' >> $DEST1
  echo 'Terminal=true' >> $DEST1
  echo 'StartupNotify=true' >> $DEST1
  chown $USERNAME:$USERNAME $DEST1 
  chmod 644 $DEST1
If all went well, show some messages, then exit

Code: Select all

  echo 
  echo 'Done'
  echo 
  echo 'to use: type in a terminal:'
  echo 'youtube-dl https://www.youtube.com/somevideo.url'
  echo 
  echo 'or select/copy some youtube-url in your browser,'
  echo 'then click DownloadYT on the desktop to start download'
  echo 
  exit 0
But if the checksums did not match: show messages and exit with error code 3.

Code: Select all

else
  rm /tmp/youtube-dl
  echo 
  echo 'checksums did not match'
  echo 'perhaps there is a new version'
  echo 'check '$INFOSITE
  echo 'and update script perhaps'
  echo 
  exit 3
fi
Last edited by Bas Koning on 2013-07-02 10:24, edited 1 time in total.
Debian-Wheezy amd64 xfce

Bas Koning
Posts: 12
Joined: 2011-08-13 03:10

Re: [SCRIPT] Install youtube-dl & clipboardtool (tested on x

#2 Post by Bas Koning »

Info shown when running downloadyoutube with nothing in clipboard
Info shown when running downloadyoutube with nothing in clipboard
screenshot1.png (24.56 KiB) Viewed 4263 times
Debian-Wheezy amd64 xfce

Bas Koning
Posts: 12
Joined: 2011-08-13 03:10

Re: [SCRIPT] Install youtube-dl & clipboardtool (tested on x

#3 Post by Bas Koning »

Running downloadyoutube with youtube-url in clipboard
Running downloadyoutube with youtube-url in clipboard
screenshot4.png (114.2 KiB) Viewed 4257 times
Debian-Wheezy amd64 xfce

Post Reply