]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - bin/whatever-dl
Use the logger class to log warnings and errors.
[dead/whatever-dl.git] / bin / whatever-dl
1 #!/usr/bin/ruby -wKU
2 #
3 # whatever-dl, a script to download online (web-based) videos.
4 #
5 # Copyright Michael Orlitzky
6 #
7 # http://michael.orlitzky.com/
8 #
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.
13 #
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.
18 #
19 # http://www.fsf.org/licensing/licenses/gpl.html
20 #
21
22 # This should load everything we need for us.
23 require 'whatever-dl'
24
25 # And getoptlong to check for our one option, --continue.
26 require 'getoptlong'
27
28 log = Logger.new(STDOUT)
29 log.level = Logger::WARN
30
31 def usage()
32 puts <<EOF
33
34 Usage: whatever-dl [options] <url>
35
36 Options:
37 -c, --continue Continue downloading a previously-attempted file.
38
39 EOF
40
41 end
42
43
44 # Default options.
45 options = { :continue => false }
46
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])
50
51 opts.each do |opt, arg|
52 case opt
53 when '--help'
54 usage()
55 Kernel.exit(ExitCodes::EXIT_SUCCESS)
56 when '--continue'
57 options[:continue] = true
58 end
59 end
60
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.')
64 end
65
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
71 # at him or her.
72 usage()
73 Kernel.exit(ExitCodes::EXIT_NO_URL)
74 end
75
76 # Factory method.
77 site = Website.create(ARGV[0])
78
79 if site.nil?
80 log.error('Invalid URL.')
81 exit(ExitCodes::EXIT_INVALID_URL)
82 end
83
84 video_url = site.get_video_url()
85
86 if video_url.nil?
87 msg = 'Error retrieving video URL: '
88 msg += "Site not supported, and the generic parser couldn't find any videos."
89 log.error(msg)
90 exit(ExitCodes::EXIT_COULDNT_GET_VIDEO_URL)
91 end
92
93 # The Downloader class is a factory; it should decide
94 # which subclass we get.
95 downloader = Downloader.create(Configuration::DOWNLOAD_METHOD)
96
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
100 # its own process.
101 begin
102 downloader.download(video_url,
103 site.get_video_filename(),
104 site.headers(),
105 continue=options[:continue])
106
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.'
110 log.error(msg)
111 Kernel.exit(ExitCodes::EXIT_CONNECTION_REFUSED)
112
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}."
116 log.error(msg)
117 Kernel.exit(ExitCodes::EXIT_ACCESS_DENIED)
118
119 rescue OpenURI::HTTPError => e
120 msg = 'An HTTP error occurred while downloading '
121 msg += " the video file: #{e.message}."
122 log.error(msg)
123 Kernel.exit(ExitCodes::EXIT_HTTP_ERROR)
124
125 rescue IOError => e
126 log.error("Input/Output Error: #{e.message}")
127 Kernel.exit(ExitCodes::EXIT_IO_ERROR)
128
129 end
130
131 # Write an empty line at the end for aesthetic reasons.
132 puts ''
133
134 Kernel.exit(ExitCodes::EXIT_SUCCESS)