class Youtube

Public Class Methods

new(params, options) click to toggle source
Calls superclass method WebRadio::new
# File lib/youtube.rb, line 6
  def initialize(params, options)
  super
      @offset = 0
      @target_content = []
end

Private Instance Methods

first_video(html) { |first| ... } click to toggle source
# File lib/youtube.rb, line 35
def first_video(html)
    json_str = html.scan(/ytInitialData = (.*);<\/script>/).flatten.first
    json = JSON.parse(json_str)
    contents = json.dig(*@target_content)
    yield contents.first
end
serial_file(title, label, serial, ext) click to toggle source
# File lib/youtube.rb, line 42
def serial_file(title, label, serial, ext)
    if serial > 0
        "#{@label}##{serial}.#{ext}"
    else
        "#{title}.#{ext}"
    end
end
youtube_download(mp4_url, mp4_file, mp3_file) click to toggle source
# File lib/youtube.rb, line 14
def youtube_download(mp4_url, mp4_file, mp3_file)
    begin
        mp3nize(mp4_file, mp3_file) do
            loop do
                print '.'
                _, err, status = Open3.capture3("youtube-dl -f mp4 -o '#{mp4_file}' --netrc '#{mp4_url}'")
                break if status == 0
                next if err =~ /403: Forbidden/
                raise ForbiddenError.new("Could not access to #{mp4_url}") if err =~ /TypeError|AssertionError/
                raise DownloadError.new(err)
            end
        end
rescue ForbiddenError
  puts "#{$!.message}, try next."
  @offset += 1
  retry
rescue NotFoundError
  raise DownloadError.new('video not found')
    end
end