]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - bin/whatever-dl
Whitespace cleanup.
[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 cfg = Configuration.new()
62
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.")
66 end
67
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
73 # at him or her.
74 usage()
75 Kernel.exit(ExitCodes::EXIT_NO_URL)
76 end
77
78
79 # Factory method.
80 site = Website.create(ARGV[0])
81
82 if site.nil?
83 log.error('Invalid URL.')
84 exit(ExitCodes::EXIT_INVALID_URL)
85 end
86
87 video_url = site.get_video_url()
88
89 if video_url.nil?
90 msg = 'Error retrieving video URL: '
91 msg += "Site not supported, and the generic parser couldn't find any videos."
92 log.error(msg)
93 exit(ExitCodes::EXIT_COULDNT_GET_VIDEO_URL)
94 end
95
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)
100
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
104 # its own process.
105 begin
106 downloader.download(video_url,
107 site.get_video_filename(),
108 site.headers(),
109 continue=options[:continue])
110
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.'
114 log.error(msg)
115 Kernel.exit(ExitCodes::EXIT_CONNECTION_REFUSED)
116
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}."
120 log.error(msg)
121 Kernel.exit(ExitCodes::EXIT_ACCESS_DENIED)
122
123 rescue OpenURI::HTTPError => e
124 msg = 'An HTTP error occurred while downloading '
125 msg += " the video file: #{e.message}."
126 log.error(msg)
127 Kernel.exit(ExitCodes::EXIT_HTTP_ERROR)
128
129 rescue IOError => e
130 log.error("Input/Output Error: #{e.message}")
131 Kernel.exit(ExitCodes::EXIT_IO_ERROR)
132
133 end
134
135 # Write an empty line at the end for aesthetic reasons.
136 puts ''
137
138 Kernel.exit(ExitCodes::EXIT_SUCCESS)