Class: Simul

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

Instance Attribute Summary

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, #login, #make_recfile, #make_tmpfile, match, #open, #out_ext, #play, #record, search, stations, #tune

Constructor Details

#initializeSimul



9
10
11
12
# File 'lib/rbtune/simul.rb', line 9

def initialize
	super
	@ext     = 'asf'
end

Instance Method Details

#channel_to_uriObject



15
16
17
18
19
20
21
22
# File 'lib/rbtune/simul.rb', line 15

def channel_to_uri
	ch = super
	if ch.end_with?('.asx')
		parse_asx ch
	else
		ch
	end
end

#create_player(uri) ⇒ Object



39
40
41
42
43
# File 'lib/rbtune/simul.rb', line 39

def create_player(uri)
	mplayer = Mplayer.new uri
	mplayer.merge! 'benchmark' => '',	'vo' => 'null'
	mplayer
end


46
47
48
49
# File 'lib/rbtune/simul.rb', line 46

def link_to_station_id(link)
	link =~ %r{(/asx/([\w-]+).asx|nkansai.tv/(\w+)/?\Z|(flower|redswave|fm-tanba|darazfm|AmamiFM|comiten|fm-shimabara|fm-kitaq))}
	id = ($2 || $3 || $4).sub(/fm[-_]/, 'fm').sub(/[-_]fm/, 'fm')
end

#parse_asx(uri) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rbtune/simul.rb', line 25

def parse_asx(uri)
	asx = Net::HTTP::get URI::parse(uri)
	asx.force_encoding "Shift_JIS"
	asx.encode! 'utf-8'
	asx.downcase!
	if asx.gsub!(%r(</ask>), '</asx>')
		$stderr.puts 'fix!! </ask> to <asx>'
	end
	doc = REXML::Document.new(asx)
	ref = doc.get_elements('//entry/ref')[0]
	ref.attribute('href').value
end

#parse_stations(body) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rbtune/simul.rb', line 57

def parse_stations(body)
	radioboxes = body / 'div.radiobox'
	radioboxes.map do |station|
		rows   = station / 'tr'
		cols   = rows[1] / 'td'
		ankers = station / 'a'

		player = ankers.select {|a|
			img = a.at 'img'
			img && img['alt'] == '放送を聴く'
		}

		title  = station.at('td > p > strong > a').text.strip
		links0 = player.map! {|e| e['href']}
		links  = links0.select { |uri| uri =~ %r{\.asx\Z|nkansai.tv} }
		if links.empty?
			# $stderr.puts "#{title}: #{links0 * ', '}"
		else
			link = links[0]
			id   = link_to_station_id(link)
			if id
				Station.new(id, link, name: title, ascii_name: id)
			else
				$stderr.puts "!!! #{title}: #{link}"
			end
		end
	end.compact
end

#stations_uriObject



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

def stations_uri
	'http://www.simulradio.info'
end