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 # We require the UriUtilities class to handle
23 # the download of the video URL.
24 require 'src/uri_utilities'
27 # All of the website classes are located in one
28 # directory, so we can 'require' them automatically.
29 Dir
.glob('src/websites/*.rb').each
do |r
|
37 EXIT_COULDNT_GET_VIDEO_URL
= 3
38 EXIT_OUTPUT_FILE_ALREADY_EXISTS
= 4
39 EXIT_ERROR_READING_FROM_VIDEO_URL
= 5
40 EXIT_CONNECTION_REFUSED
= 6
42 EXIT_ACCESS_DENIED
= 8
44 # Only actually do something if this script was called
45 # directly (i.e. not from the tests).
46 if (__FILE__
== $0) then
47 if (ARGV.length
< 1) then
48 # If the user didn't give us a URL, yell
50 puts
'Usage: whatever-dl <url>'
51 Kernel
.exit(EXIT_NO_URL
)
55 site
= Website
.create(ARGV[0])
59 exit(EXIT_INVALID_URL
)
62 video_url
= site
.get_video_url()
65 puts
'Error retrieving video URL.'
66 exit(EXIT_COULDNT_GET_VIDEO_URL
)
69 video_uri
= URI
.parse(video_url
)
70 uu
= UriUtilities
.new()
72 if File
.exists
?(site
.get_video_filename())
73 puts
"Error: output file already exists. Please remove #{site.get_video_filename()}, and try again."
74 Kernel
.exit(EXIT_OUTPUT_FILE_ALREADY_EXISTS
)
78 # Attempt to download the file, and rescue and report
79 # any (predictable) exceptions.
81 puts
"Fetching #{video_url}"
82 puts
"Saving as #{site.get_video_filename()}."
84 uu
.download_with_progress_bar(video_uri
, site
.get_video_filename())
85 rescue Errno
::ECONNREFUSED => e
86 puts
'The connection to the server (to download the video file) was refused. Check your connection, and try again later.'
87 Kernel
.exit(EXIT_CONNECTION_REFUSED
)
88 rescue Errno
::EACCES => e
89 puts
"Access denied. Check that you have write permission to the output file/directory. Details: #{e.message}."
90 rescue OpenURI
::HTTPError => e
91 puts
"An HTTP error occurred while downloading the video file: #{e.message}."
92 Kernel
.exit(EXIT_HTTP_ERROR
)
95 # Write an empty line at the end for aesthetic reasons.
98 Kernel
.exit(EXIT_SUCCESS
)