]>
gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - bin/whatever-dl
b9ea02ca79eeab9e4690db8b49792b40d78ac48d
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 # Warn about nonsensical options.
62 if options
[:continue] and not Configuration
::DOWNLOAD_METHOD == :wget
63 log
.warn('The --continue flag does nothing unless DOWNLOAD_METHOD is :wget.')
66 # Note that GetoptLong steals its arguments from ARGV, so we don't need
67 # to take optional arguments into account when figuring out whether or not
68 # we were passed a URL.
69 if (ARGV.length
< 1) then
70 # If the user didn't give us a URL, yell
73 Kernel
.exit(ExitCodes
::EXIT_NO_URL)
77 site
= Website
.create(ARGV[0])
80 log
.error('Invalid URL.')
81 exit(ExitCodes
::EXIT_INVALID_URL)
84 video_url
= site
.get_video_url()
87 msg
= 'Error retrieving video URL: '
88 msg +
= "Site not supported, and the generic parser couldn't find any videos."
90 exit(ExitCodes
::EXIT_COULDNT_GET_VIDEO_URL)
93 # The Downloader class is a factory; it should decide
94 # which subclass we get.
95 downloader
= Downloader
.create(Configuration
::DOWNLOAD_METHOD)
97 # Attempt to download the file, and rescue and report
98 # any (predictable) exceptions. The wget downloader will
99 # naturally not report any of these, since it will die in
102 downloader
.download(video_url
,
103 site
.get_video_filename(),
105 continue
=options
[:continue])
107 rescue Errno
::ECONNREFUSED => e
108 msg
= 'The connection to the server (to download the video file) '
109 msg +
= 'was refused. Check your connection, and try again later.'
111 Kernel
.exit(ExitCodes
::EXIT_CONNECTION_REFUSED)
113 rescue Errno
::EACCES => e
114 msg
= 'Access denied. Check that you have write permission '
115 msg +
= "to the output file/directory. Details: #{e.message}."
117 Kernel
.exit(ExitCodes
::EXIT_ACCESS_DENIED)
119 rescue OpenURI
::HTTPError => e
120 msg
= 'An HTTP error occurred while downloading '
121 msg +
= " the video file: #{e.message}."
123 Kernel
.exit(ExitCodes
::EXIT_HTTP_ERROR)
126 log
.error("Input/Output Error: #{e.message}")
127 Kernel
.exit(ExitCodes
::EXIT_IO_ERROR)
131 # Write an empty line at the end for aesthetic reasons.
134 Kernel
.exit(ExitCodes
::EXIT_SUCCESS)