3 # whatever-dl, a script to download online (web-based) videos.
5 # Copyright Michael Orlitzky
7 # http://michael.orlitzky.com/
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # http://www.fsf.org/licensing/licenses/gpl.html
22 # This should load everything we need for us.
25 # And getoptlong to check for our one option, --continue.
31 EXIT_COULDNT_GET_VIDEO_URL
= 3
33 EXIT_ERROR_READING_FROM_VIDEO_URL
= 5
34 EXIT_CONNECTION_REFUSED
= 6
36 EXIT_ACCESS_DENIED
= 8
41 Usage: whatever-dl [options] <url>
44 -c, --continue Continue downloading a previously-attempted file.
50 # Only actually do something if this script was called
51 # directly (i.e. not from the tests).
52 if (__FILE__
== $0) then
54 options
= { :continue => false }
56 # Parse the command-line options into the options hash.
57 opts
= GetoptLong
.new(["--continue", "-c", GetoptLong
::NO_ARGUMENT],
58 ["--help", "-h", GetoptLong
::NO_ARGUMENT])
60 opts
.each
do |opt
, arg
|
64 Kernel
.exit(EXIT_SUCCESS
)
66 options
[:continue] = true
70 # Warn about nonsensical options.
71 if options
[:continue] and not Configuration
::DOWNLOAD_METHOD == :wget
72 puts
'WARNING: The --continue flag does nothing unless DOWNLOAD_METHOD is :wget.'
75 # Note that GetoptLong steals its arguments from ARGV, so we don't need
76 # to take optional arguments into account when figuring out whether or not
77 # we were passed a URL.
78 if (ARGV.length
< 1) then
79 # If the user didn't give us a URL, yell
82 Kernel
.exit(EXIT_NO_URL
)
86 site
= Website
.create(ARGV[0])
90 exit(EXIT_INVALID_URL
)
93 video_url
= site
.get_video_url()
96 puts
'Error retrieving video URL:'
97 puts
"Site not supported, and the generic parser couldn't find any videos."
98 exit(EXIT_COULDNT_GET_VIDEO_URL
) end
100 # The Downloader class is a factory; it should decide
101 # which subclass we get.
102 downloader
= Downloader
.create(Configuration
::DOWNLOAD_METHOD)
104 # Attempt to download the file, and rescue and report
105 # any (predictable) exceptions. The wget downloader will
106 # naturally not report any of these, since it will die in
109 downloader
.download(video_url
,
110 site
.get_video_filename(),
112 continue
=options
[:continue])
114 rescue Errno
::ECONNREFUSED => e
115 msg
= 'The connection to the server (to download the video file) '
116 msg +
= 'was refused. Check your connection, and try again later.'
118 Kernel
.exit(EXIT_CONNECTION_REFUSED
)
120 rescue Errno
::EACCES => e
121 msg
= 'Access denied. Check that you have write permission '
122 msg +
= "to the output file/directory. Details: #{e.message}."
124 Kernel
.exit(EXIT_ACCESS_DENIED
)
126 rescue OpenURI
::HTTPError => e
127 msg
= 'An HTTP error occurred while downloading '
128 msg +
= " the video file: #{e.message}."
130 Kernel
.exit(EXIT_HTTP_ERROR
)
133 puts
"Input/Output Error: #{e.message}"
134 Kernel
.exit(EXIT_IO_ERROR
)
138 # Write an empty line at the end for aesthetic reasons.
141 Kernel
.exit(EXIT_SUCCESS
)