Class: Vidload::Mp2t::Api::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/vidload/mp2t/api.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(video_url:, video_name:, hls_url:, master_playlist_name:, playwright_cli_path:, video_referer:, ts_seg_pattern:, hls_index_pattern:, author_name:) ⇒ Downloader

Returns a new instance of Downloader.

Raises:

  • (ArgumentError)


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
# File 'lib/vidload/mp2t/api.rb', line 19

def initialize(
  video_url:,
  video_name:,
  hls_url:,
  master_playlist_name:,
  playwright_cli_path:,
  video_referer:,
  ts_seg_pattern:,
  hls_index_pattern:,
  author_name:
)
  raise ArgumentError, "video_url must be provided" unless video_url
  raise ArgumentError, "hls_url must be provided" unless hls_url
  raise ArgumentError, "master_playlist_name must be provided" unless master_playlist_name
  raise ArgumentError, "playwright_cli_path must be provided" unless playwright_cli_path
  raise ArgumentError, "video_referer must be provided" unless video_referer
  raise ArgumentError, "ts_seg_pattern must be provided" unless ts_seg_pattern
  raise ArgumentError, "hls_index_pattern must be provided" unless hls_index_pattern

  @video_url = video_url
  @hls_url = hls_url
  @master_playlist_name = master_playlist_name
  @playwright_cli_path = playwright_cli_path
  @video_referer = video_referer
  @ts_seg_pattern = ts_seg_pattern
  @hls_index_pattern = hls_index_pattern
  @max_lines = IO.console.winsize[0]
  @author_name = author_name
  @video_name = if @author_name then "#{@author_name}_#{video_name}" else nil end
end

Instance Attribute Details

#hls_urlObject (readonly)

Returns the value of attribute hls_url.



17
18
19
# File 'lib/vidload/mp2t/api.rb', line 17

def hls_url
  @hls_url
end

#master_playlist_nameObject (readonly)

Returns the value of attribute master_playlist_name.



17
18
19
# File 'lib/vidload/mp2t/api.rb', line 17

def master_playlist_name
  @master_playlist_name
end

#playwright_cli_pathObject (readonly)

Returns the value of attribute playwright_cli_path.



17
18
19
# File 'lib/vidload/mp2t/api.rb', line 17

def playwright_cli_path
  @playwright_cli_path
end

#video_nameObject (readonly)

Returns the value of attribute video_name.



17
18
19
# File 'lib/vidload/mp2t/api.rb', line 17

def video_name
  @video_name
end

#video_refererObject (readonly)

Returns the value of attribute video_referer.



17
18
19
# File 'lib/vidload/mp2t/api.rb', line 17

def video_referer
  @video_referer
end

#video_urlObject (readonly)

Returns the value of attribute video_url.



17
18
19
# File 'lib/vidload/mp2t/api.rb', line 17

def video_url
  @video_url
end

Class Method Details

.display_with_spinner(loading_msg = "Loading...") ⇒ Object



106
107
108
109
110
111
# File 'lib/vidload/mp2t/api.rb', line 106

def self.display_with_spinner(loading_msg = "Loading...")
  spinner = TTY::Spinner.new("[:spinner] #{loading_msg}")
  spinner.auto_spin
  yield
  spinner.success("(done)")
end

.from_argvObject



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/vidload/mp2t/api.rb', line 50

def self.from_argv
  new(
    video_url: ARGV[0],
    video_name: ARGV[1],
    hls_url: ARGV[2],
    master_playlist_name: ARGV[3],
    playwright_cli_path: ARGV[4],
    video_referer: ARGV[5],
    ts_seg_pattern: ARGV[6],
    hls_index_pattern: ARGV[7],
    author_name: ARGV[8],
  )
end

.from_hash(hash) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vidload/mp2t/api.rb', line 64

def self.from_hash(hash)
  new(
    video_url: hash[:video_url],
    author_name: hash[:author_name],
    video_name: hash[:video_name],
    hls_url: hash[:hls_url],
    master_playlist_name: hash[:master_playlist_name],
    playwright_cli_path: hash[:playwright_cli_path],
    video_referer: hash[:video_referer],
    ts_seg_pattern: hash[:ts_seg_pattern],
    hls_index_pattern: hash[:hls_index_pattern],
  )
end

Instance Method Details

#display_calling_argsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vidload/mp2t/api.rb', line 91

def display_calling_args
  puts "Constants:"
  puts "\tDEMUXER_PATH=#{DEMUXER_PATH}"
  puts "Called with:"
  puts "\tvideo_url=#{@video_url}"
  puts "\tvideo_name=#{@video_name}"
  puts "\thls_url=#{@hls_url}"
  puts "\tmaster_playlist_name=#{@master_playlist_name}"
  puts "\tplaywright_cli_path=#{@playwright_cli_path}"
  puts "\tvideo_referer=#{@video_referer}"
  puts "\tts_seg_pattern=#{@ts_seg_pattern}"
  puts "\thls_index_pattern=#{@hls_index_pattern}"
  puts "\tauthor_name=#{@author_name}"
end

#download_video(video_starter_callbacks: []) ⇒ Object

main func to be called in your own scripts defined under web/



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/vidload/mp2t/api.rb', line 79

def download_video(video_starter_callbacks: [])
  Playwright.create(playwright_cli_executable_path: @playwright_cli_path) do |playwright|
    browser = playwright.chromium.launch
    page = browser.new_page

    manage_video_download(page, *video_starter_callbacks)
    wait_until_video_downloaded

    browser.close
  end
end