Class: AsobiStore

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

Instance Method Summary collapse

Methods inherited from WebRadio

#dump, instance

Constructor Details

#initialize(params, options) ⇒ AsobiStore

Returns a new instance of AsobiStore.



6
7
8
9
# File 'lib/asobistore.rb', line 6

def initialize(params, options)
  super
  @offset = 0
end

Instance Method Details

#downloadObject



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
# File 'lib/asobistore.rb', line 11

def download
  player = find_player(@url)
  html = Nokogiri(URI.open("https:#{player}").read)
  src_m3u8 = html.css('source').first.attr('src')
  m3u8 = "#{File.dirname(src_m3u8)}/#{URI.open(src_m3u8).read.match(/^[^#].*/)[0]}"

  serial = html.title.scan(/#(\d+)/).flatten.first.to_i
  @cover = "https:#{html.css('audio,video').first.attr('poster')}" unless @cover
  ts_file = "#{@label}##{serial}.ts"
  mp3_file = "#{@label}##{serial}.mp3"

  begin
    agent = Mechanize.new
    agent.get(m3u8)
    body = agent.page.body
  rescue ArgumentError
    body = URI.open(m3u8, &:read)
  end
  tses = body.scan(/.*\.ts.*/)
  key_url = body.scan(/URI="(.*)"/).flatten.first

  if key_url
    key = agent.get_file(key_url)
    decoder = OpenSSL::Cipher.new('aes-128-cbc')
    decoder.key = key
    decoder.decrypt
  else
    decoder = ''
    def decoder.update(s); return s; end
  end

  mp3nize(ts_file, mp3_file) do
    open(ts_file, 'wb:ASCII-8BIT') do |ts|
      tses.each_with_index do |file, count|
        print "." if count % 10 == 0
        ts.write(decoder.update(agent.get_file(file)))
      end
      ts.write(decoder.final)
    end
  end
end