]> gitweb.michael.orlitzky.com - dead/whatever-dl.git/blob - bin/whatever-dl
79cbbee15afb9785a5f2ff5085b94392894dcdbb
[dead/whatever-dl.git] / bin / whatever-dl
1 #!/usr/bin/ruby -w
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 # We need Pathname to get the real filesystem path
23 # of this script (and not, for example, the path of
24 # a symlink which points to it.
25 require 'pathname'
26
27 # This bit of magic adds the parent directory (the
28 # project root) to the list of ruby load paths.
29 # Thus, our require statements will work regardless of
30 # how or from where the script was run.
31 executable = Pathname.new(__FILE__).realpath.to_s
32 $: << File.dirname(executable) + '/../'
33
34 # We require the UriUtilities class to handle
35 # the download of the video URL.
36 require 'src/uri_utilities'
37
38
39 # All of the website classes are located in one
40 # directory, so we can 'require' them automatically.
41 Dir.glob('src/websites/*.rb').each do |r|
42 require r
43 end
44
45
46 EXIT_SUCCESS = 0
47 EXIT_NO_URL = 1
48 EXIT_INVALID_URL = 2
49 EXIT_COULDNT_GET_VIDEO_URL = 3
50 EXIT_OUTPUT_FILE_ALREADY_EXISTS = 4
51 EXIT_ERROR_READING_FROM_VIDEO_URL = 5
52 EXIT_CONNECTION_REFUSED = 6
53 EXIT_HTTP_ERROR = 7
54 EXIT_ACCESS_DENIED = 8
55
56 # Only actually do something if this script was called
57 # directly (i.e. not from the tests).
58 if (__FILE__ == $0) then
59 if (ARGV.length < 1) then
60 # If the user didn't give us a URL, yell
61 # at him or her.
62 puts 'Usage: whatever-dl <url>'
63 Kernel.exit(EXIT_NO_URL)
64 end
65
66 # Factory method.
67 site = Website.create(ARGV[0])
68
69 if site.nil?
70 puts 'Invalid URL.'
71 exit(EXIT_INVALID_URL)
72 end
73
74 video_url = site.get_video_url()
75
76 if video_url.nil?
77 puts 'Error retrieving video URL.'
78 exit(EXIT_COULDNT_GET_VIDEO_URL)
79 end
80
81 video_uri = URI.parse(video_url)
82 uu = UriUtilities.new()
83
84 if File.exists?(site.get_video_filename())
85 puts "Error: output file already exists. Please remove #{site.get_video_filename()}, and try again."
86 Kernel.exit(EXIT_OUTPUT_FILE_ALREADY_EXISTS)
87 end
88
89
90 # Attempt to download the file, and rescue and report
91 # any (predictable) exceptions.
92 begin
93 puts "Fetching #{video_url}"
94 puts "Saving as #{site.get_video_filename()}."
95 puts ""
96 uu.download_with_progress_bar(video_uri, site.get_video_filename())
97 rescue Errno::ECONNREFUSED => e
98 puts 'The connection to the server (to download the video file) was refused. Check your connection, and try again later.'
99 Kernel.exit(EXIT_CONNECTION_REFUSED)
100 rescue Errno::EACCES => e
101 puts "Access denied. Check that you have write permission to the output file/directory. Details: #{e.message}."
102 rescue OpenURI::HTTPError => e
103 puts "An HTTP error occurred while downloading the video file: #{e.message}."
104 Kernel.exit(EXIT_HTTP_ERROR)
105 end
106
107 # Write an empty line at the end for aesthetic reasons.
108 puts ""
109
110 Kernel.exit(EXIT_SUCCESS)
111 end