/
youtube.rb
executable file
·68 lines (55 loc) · 2.01 KB
/
youtube.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Youtube < PluginBase
#TODO: TEST THIS: https://www.youtube.com/watch?v=Qapou-3-fM8&list=PL_Z529zmzNGcOVBJA0MgjjQoKiBcmMQWh
# this will be called by the main app to check whether this plugin is responsible for the url passed
def self.matches_provider?(url)
url.include?("youtube.com") || url.include?("youtu.be")
end
def self.get_urls_and_filenames(url, options = {})
initialize_components(options)
urls = @url_resolver.get_all_urls(url, options[:filter])
videos = get_videos(urls)
return_value = videos.map do |video|
format = @format_picker.pick_format(video)
make_url_filname_hash(video, format)
end
return_value.empty? ? download_error("No videos could be downloaded.") : return_value
end
def self.initialize_components(options)
@cipher_io = CipherIO.new
coordinator = DecipherCoordinator.new(Decipherer.new(@cipher_io), CipherGuesser.new)
@video_resolver = VideoResolver.new(coordinator)
@url_resolver = UrlResolver.new
@format_picker = FormatPicker.new(options)
end
def self.notify(message)
puts "[YOUTUBE] #{message}"
end
def self.download_error(message)
raise CouldNotDownloadVideoError, message
end
def self.get_videos(urls)
videos = urls.map do |url|
begin
@video_resolver.get_video(url)
rescue VideoResolver::VideoRemovedError
notify "The video #{url} has been removed."
nil
rescue => e
notify "Error getting the video: #{e.message}"
nil
end
end
videos.reject(&:nil?)
end
def self.make_url_filname_hash(video, format)
url = video.get_download_url(format.itag)
name = PluginBase.make_filename_safe(video.title) + ".#{format.extension}"
{url: url, name: name, on_downloaded: make_downloaded_callback(video)}
end
def self.make_downloaded_callback(video)
return nil unless video.signature_guess?
lambda do |success|
@cipher_io.add_cipher(video.cipher_version, video.cipher_operations) if success
end
end
end