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.



8
9
10
# File 'lib/rbtune/radiko.rb', line 8

def authtoken
  @authtoken
end

Instance Method Details

#authenticate1(url) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rbtune/radiko.rb', line 35

def authenticate1(url)
	agent.request_headers = {
			'X-Radiko-App'         => 'pc_html5',
			'X-Radiko-App-Version' => '0.0.1',
			'X-Radiko-Device'      => 'pc',
			'X-Radiko-User'        => 'dummy_user',
		}
	res = agent.get url
	auth1 = res.response
	authtoken = auth1['x-radiko-authtoken']
	offset = auth1['x-radiko-keyoffset'].to_i
	length = auth1['x-radiko-keylength'].to_i
	partialkey = read_partialkey offset, length
	[authtoken, partialkey]
end

#authenticate2(url, authtoken, partialkey) ⇒ Object

return: area info



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

def authenticate2(url, authtoken, partialkey)
	# pp [url, authtoken, partialkey]
	agent.verify_mode = OpenSSL::SSL::VERIFY_NONE
	agent.request_headers = {
		'X-Radiko-Device'      => 'pc',
		'X-Radiko-User'        => 'dummy_user',
		'X-Radiko-Authtoken'   => authtoken,
		'X-Radiko-Partialkey'  => partialkey,
	}
	res = agent.get url
	body = res.body
	body.force_encoding 'utf-8'
	body.split(',').map(&:strip)
end

#channel_to_uriObject



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

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

#create_player(uri) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/rbtune/radiko.rb', line 26

def create_player(uri)
	FFMpeg.new( {
		headers: %Q("X-Radiko-Authtoken: #{authtoken}"),
		i: %Q("#{uri}"),
		# acodec: 'copy', # acodecオプションはiオプションのあとに置かないとエラー
	} )
end

#openObject

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



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

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

#parse_stations(body) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/rbtune/radiko.rb', line 80

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

#read_partialkey(offset, length) ⇒ Object



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

def read_partialkey(offset, length)
	key = "bcd151073c03b352e1ef2fd66c32209da9ca0afa"
	Base64.encode64(key[offset,length]).chomp
end

#stations_uriObject



75
76
77
# File 'lib/rbtune/radiko.rb', line 75

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