Class: Radiko

Inherits:
Radio show all
Defined in:
lib/rbtune/radiko.rb

Direct Known Subclasses

RadikoPremium

Instance Attribute Summary collapse

Attributes inherited from Radio

#area_en, #area_id, #area_ja, #ext, #outdir

Instance Method Summary collapse

Methods inherited from Radio

#agent, bands, channels, #close, #convert, #convert_ffmpeg, #datetime, db, #fetch_stations, find, inherited, #initialize, #login, #make_recfile, #make_tmpfile, match, #out_ext, #play, #record, search, stations, #tune

Constructor Details

This class inherits a constructor from Radio

Instance Attribute Details

#authtokenObject (readonly)

Returns the value of attribute authtoken.



11
12
13
# File 'lib/rbtune/radiko.rb', line 11

def authtoken
  @authtoken
end

Instance Method Details

#authenticate1(url) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rbtune/radiko.rb', line 60

def authenticate1(url)
  res = agent.post url, {}, {
      'pragma'               => 'no-cache',
      'X-Radiko-App'         => 'pc_ts',
      'X-Radiko-App-Version' => '4.0.0',
      'X-Radiko-User'        => 'test-stream',
      'X-Radiko-Device'      => 'pc',
    }
  s = res.body
  s.sub! /\r\n\r\n.*/m, ''
  arr = s.split(/\r\n/).map{|s| s.split('=')}.flatten
  auth1 = Hash[*arr]
  authtoken = auth1['X-Radiko-AuthToken'] || auth1['X-RADIKO-AUTHTOKEN']
  offset = auth1['X-Radiko-KeyOffset'].to_i
  length = auth1['X-Radiko-KeyLength'].to_i
  partialkey = read_partialkey keyfile, offset, length
  [authtoken, partialkey]
end

#authenticate2(url, authtoken, partialkey) ⇒ Object

return: area info



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rbtune/radiko.rb', line 87

def authenticate2(url, authtoken, partialkey)
  # pp [url, authtoken, partialkey]
  agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
  res = agent.post url, {}, {
    'pragma'               => 'no-cache',
    'X-Radiko-App'         => 'pc_ts',
    'X-Radiko-App-Version' => '4.0.0',
    'X-Radiko-User'        => 'test-stream',
    'X-Radiko-Device'      => 'pc',
    'X-Radiko-Authtoken'   => authtoken,
    'X-Radiko-Partialkey'  => partialkey,
  }
  body = res.body
  body.force_encoding 'utf-8'
  body.split(',').map(&:strip)
end

#channel_to_uriObject



45
46
47
48
# File 'lib/rbtune/radiko.rb', line 45

def channel_to_uri
  xml = agent.get "http://radiko.jp/v2/station/stream/#{@channel}.xml"
  xml.at('//url/item').text
end

#create_player(uri) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rbtune/radiko.rb', line 51

def create_player(uri)
  rtmpdump           = RtmpDump.new
  rtmpdump['rtmp']   = uri
  rtmpdump['swfVfy'] = playerurl
  rtmpdump['conn']   = %Q(S:"" --conn S:""  --conn S:""  --conn S:#{authtoken})
  rtmpdump
end

#fetch_file(url, file = nil) ⇒ Object



122
123
124
125
126
# File 'lib/rbtune/radiko.rb', line 122

def fetch_file(url, file=nil)
  content = agent.get_file(url)
  File.open(file, "wb") { |fout| fout.write content } if file
  content
end

#keyfileObject



24
25
26
# File 'lib/rbtune/radiko.rb', line 24

def keyfile
  "authkey.png"
end

#openObject

get_auth2 の返り値により @area_id, @area_ja, @area_en が設定される



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbtune/radiko.rb', line 30

def open
  unless File.exists? playerfile
    $stderr.puts 'fetching player...'
    fetch_file playerurl, playerfile
  end
  unless File.exists? keyfile
    swfextract playerfile, 12, keyfile
  end

  @authtoken, partialkey = authenticate1 'https://radiko.jp/v2/api/auth1_fms'
  @area_id, @area_ja, @area_en = authenticate2 'https://radiko.jp/v2/api/auth2_fms', authtoken, partialkey
  puts "area: #{area_id} (#{area_ja}: #{area_en})"
end

#parse_stations(body) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/rbtune/radiko.rb', line 110

def parse_stations(body)
  stations = body.search '//station'
  stations.map do |station|
    id         = station.at('id').text
    name       = station.at('name').text
    uri        = id
    ascii_name = station.at('ascii_name').text
    Station.new(id, uri, name: name, ascii_name: ascii_name)
  end
end

#playerfileObject



19
20
21
# File 'lib/rbtune/radiko.rb', line 19

def playerfile
  "player.swf"
end

#playerurlObject



14
15
16
# File 'lib/rbtune/radiko.rb', line 14

def playerurl
  "http://radiko.jp/apps/js/flash/myplayer-release.swf"
end

#read_partialkey(file, offset, length) ⇒ Object



80
81
82
83
# File 'lib/rbtune/radiko.rb', line 80

def read_partialkey(file, offset, length)
  key = File.open(file, "rb") { |io| io.read(offset + length) }
  Base64.encode64(key[offset,length]).chomp
end

#stations_uriObject



105
106
107
# File 'lib/rbtune/radiko.rb', line 105

def stations_uri
  "http://radiko.jp/v3/station/list/#{area_id}.xml"
end

#swfextract(swffile, character_id, out_file) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rbtune/radiko.rb', line 129

def swfextract(swffile, character_id, out_file)
  swf = SwfRuby::SwfDumper.new
  swf.open(swffile)
  swf.tags.each_with_index do |tag, i|
    tag = swf.tags[i]
    if tag.character_id && tag.character_id == character_id
      offset = swf.tags_addresses[i]
      len = tag.length
      File.open(out_file, 'wb') { |out| out.print tag.data[6..-1] }
      break
    end
  end
end