]>
gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - bin/whatever-dl
a40af28fab676f14b3ac36084c3bbdb7ca7cb731
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.
28 log
= Logger
.new(STDOUT)
29 log
.level
= Logger
::WARN
34 Usage: whatever-dl [options] <url>
37 -c, --continue Continue downloading a previously-attempted file.
45 options
= { :continue => false }
47 # Parse the command-line options into the options hash.
48 opts
= GetoptLong
.new(["--continue", "-c", GetoptLong
::NO_ARGUMENT],
49 ["--help", "-h", GetoptLong
::NO_ARGUMENT])
51 opts
.each
do |opt
, arg
|
55 Kernel
.exit(ExitCodes
::EXIT_SUCCESS)
57 options
[:continue] = true
61 cfg
= Configuration
.new()
63 # Warn about nonsensical options.
64 if options
[:continue] and not cfg
.download_method
== 'wget'
65 log
.warn("The --continue flag does nothing unless download_method is wget.")
68 # Note that GetoptLong steals its arguments from ARGV, so we don't need
69 # to take optional arguments into account when figuring out whether or not
70 # we were passed a URL.
71 if (ARGV.length
< 1) then
72 # If the user didn't give us a URL, yell
75 Kernel
.exit(ExitCodes
::EXIT_NO_URL)
80 site
= Website
.create(ARGV[0])
83 log
.error('Invalid URL.')
84 exit(ExitCodes
::EXIT_INVALID_URL)
87 video_url
= site
.get_video_url()
90 msg
= 'Error retrieving video URL: '
91 msg +
= "Site not supported, and the generic parser couldn't find any videos."
93 exit(ExitCodes
::EXIT_COULDNT_GET_VIDEO_URL)
96 # The Downloader class is a factory; it should decide
97 # which subclass we get.
98 puts
"download_method: #{cfg.download_method}"
99 downloader
= Downloader
.create(cfg
.download_method
)
101 # Attempt to download the file, and rescue and report
102 # any (predictable) exceptions. The wget downloader will
103 # naturally not report any of these, since it will die in
106 downloader
.download(video_url
,
107 site
.get_video_filename(),
109 continue
=options
[:continue])
111 rescue Errno
::ECONNREFUSED => e
112 msg
= 'The connection to the server (to download the video file) '
113 msg +
= 'was refused. Check your connection, and try again later.'
115 Kernel
.exit(ExitCodes
::EXIT_CONNECTION_REFUSED)
117 rescue Errno
::EACCES => e
118 msg
= 'Access denied. Check that you have write permission '
119 msg +
= "to the output file/directory. Details: #{e.message}."
121 Kernel
.exit(ExitCodes
::EXIT_ACCESS_DENIED)
123 rescue OpenURI
::HTTPError => e
124 msg
= 'An HTTP error occurred while downloading '
125 msg +
= " the video file: #{e.message}."
127 Kernel
.exit(ExitCodes
::EXIT_HTTP_ERROR)
130 log
.error("Input/Output Error: #{e.message}")
131 Kernel
.exit(ExitCodes
::EXIT_IO_ERROR)
135 # Write an empty line at the end for aesthetic reasons.
138 Kernel
.exit(ExitCodes
::EXIT_SUCCESS)