Friday, September 18, 2009

Zentube

Another variation on the theme of zenity. I honestly like the way you can make simple front-ends. In addition, I'm doing something with youtube again, or more precisely, I'm doing stuff with youtube-dl.

So, the problem with youtube is that if you don't have Internet access, you obviously can't really use it, and there are certain instances where it'd come in handy. One such instance is when you're doing language teaching in an Internet-bereft classroom.

So there's youtube-dl to get some videos downloaded, but a person is not always in the mood for fiddling with the shell when preparing their lesson material.

Hence, this script provides the simples of interfaces to download videos via youtube-dl. That's pretty much it. Anyway, I think it's simple and does its job.

Oh, yeah, I played around with the idea of automatically installing a package if it is not available at the time of execution. It's a sort of experiment, to see if it can be done at all. I'm not sure how effective this is though. And it depends on apt and gksudo.

The code:
 
1 #!/bin/bash
2 #
3 # Zentube
4 #
5 # A simple GUI front-end to youtube-dl. All you need to do is run it,
6 # and put in the address of the video, and the back-end tries to
7 # download the video.
8 #
9 # Parameters:
10 # None
11 #
12 # Requires:
13 # youtube-dl
14 # zenity
15 # gksudo & apt (if you want youtube-dl installed automatically)
16 #
17 # Author:
18 # Konrad Siek <konrad.siek@gmail.com>
19 #
20 # License:
21 #
22 # Copyright 2008 Konrad Siek.
23 #
24 # This program is free software: you can redistribute it and/
25 # or modify it under the terms of the GNU General Public
26 # License as published by the Free Software Foundation, either
27 # version 3 of the License, or (at your option) any later
28 # version.
29 #
30 # This program is distributed in the hope that it will be
31 # useful, but WITHOUT ANY WARRANTY; without even the implied
32 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
33 # PURPOSE. See the GNU General Public License for more
34 # details.
35 #
36 # You should have received a copy of the GNU General Public
37 # License along with this program. If not, see
38 # <http://www.gnu.org/licenses/>.
39
40 # The downloader backend.
41 PACKAGE=youtube-dl
42
43 # Output information.
44 OUTPUT_DIR=~/Videos/
45 EXTENSION=.flv
46 TEMP_FILE=/tmp/$(basename $0).XXXXXXXXXX
47
48 # The quality of the output file can be adjusted here, or you can comment
49 # out this setting altogether, to get the default.
50 QUALITY=--best-quality
51
52 # Exit code constants.
53 SUCCESS=0
54 INSTALLATION_ABORTED=1
55 INSTALLATION_FAILED=2
56 INVALID_VIDEO_ADDRESS=4
57 INVALID_OUTPUT_DIRECTORY=8
58 BACKEND_ERROR=16
59
60 # This is a convenience installer for apt-using distros, e.g. Ubuntu.
61 if [ -z "$(which $PACKAGE)" ]
62 then
63 # Ask whether to attempt automatic install of the missing package.
64 # If the answer is no, then quit with an error.
65 zenity --question \
66 --title="Automatic installation" \
67 --text="Can't find <b>$PACKAGE</b>. Should I try installing it?" \
68 || exit $INSTALLATION_ABORTED
69
70 # Try installing the missing package, or quit with an error if the
71 # attempt is failed.
72 gksudo "apt-get install $PACKAGE" || exit $INSTALLATION_FAILED
73 fi
74
75 # Ask user for the URL of the video.
76 url=$(\
77 zenity --entry \
78 --title="Video address" \
79 --text="What is the address of the video?" \
80 )
81 # If no URL is given, then quit.
82 [ -z "$url" ] && exit $INVALID_VIDEO_ADDRESS
83
84 # Move to the output directory, create it i necessary.
85 mkdir -p "$OUTPUT_DIR" || exit $INVALID_OUTPUT_DIRECTORY
86 cd "$OUTPUT_DIR"
87
88 # Make a temporary file to collect error messages from the downloader.
89 temp_file=$(mktemp $TEMP_FILE)
90
91 # Run the downloader.
92 $PACKAGE $QUALITY --title "$url" 2>"$temp_file" | \
93 zenity --progress --pulsate --auto-kill --auto-close --text="Downloading..."
94
95 # Check for errors, and display a success of error dialog at the end.
96 errors=$(cat $temp_file)
97
98 if [ -z "$errors" ]
99 then
100 # Display successful info.
101 zenity --info --text="Download successful!"
102
103 # Remove temporary file.
104 unlink "$temp_file"
105
106 # Exit successfully.
107 exit $SUCCESS
108 else
109 # Display error dialog.
110 zenity --error --text="$errors"
111
112 # Remove temporary file.
113 unlink "$temp_file"
114
115 # Exit with an error code.
116 exit $BACKEND_ERROR
117 fi


The code is also available at GitHub as bash/zentube.

No comments: