Class: YouTube

Inherits:
WebRadio show all
Defined in:
lib/youtube.rb

Instance Method Summary collapse

Methods inherited from WebRadio

#initialize, instance

Constructor Details

This class inherits a constructor from WebRadio

Instance Method Details

#download(name) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/youtube.rb', line 6

def download(name)
  list = {}
  playlist = Nokogiri(open(@url, &:read))
  playlist.css('#pl-video-list tr').each do |tr|
    title = tr.attr('data-title')
    serial = title.scan(/(?:[#第]| EP)(\d+)|/).flatten.compact[0].to_i
    video_url = tr.css('a').attr('href').to_s
    list[serial] = video_url
  end
  serial = list.keys.sort.last
  @src = "#{name}##{'%02d' % serial}.mp4"
  @dst = "#{name}##{'%02d' % serial}.mp3"
  mp3ize(@src, @dst) do
    player_url = "https://www.youtube.com#{list[serial]}".sub(/&.*/, '')
        result = Open3.capture3("viddl-rb -u -q '*:*:mp4' '#{player_url}'")
    video_url = result[0].split.last.chomp
    open(@src, 'wb:ASCII-8BIT') do |o|
      o.write open(video_url, 'r:ASCII-8BIT', &:read)
    end
  end
end