Class: Radio

Inherits:
Object
  • Object
show all
Defined in:
lib/rbtune/radio.rb

Direct Known Subclasses

Agqr, ListenRadio, Radiko, Radiru, Simul

Defined Under Namespace

Classes: HTTPBadRequestException, HTTPForbiddenException

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRadio

Returns a new instance of Radio.



28
29
30
31
# File 'lib/rbtune/radio.rb', line 28

def initialize
  @outdir = '.'
  @ext = 'm4a'
end

Instance Attribute Details

#area_enObject (readonly)

for Radiko(Premium)



20
21
22
# File 'lib/rbtune/radio.rb', line 20

def area_en
  @area_en
end

#area_idObject (readonly)

for Radiko(Premium)



18
19
20
# File 'lib/rbtune/radio.rb', line 18

def area_id
  @area_id
end

#area_jaObject (readonly)

for Radiko(Premium)



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

def area_ja
  @area_ja
end

#extObject (readonly)

Returns the value of attribute ext.



17
18
19
# File 'lib/rbtune/radio.rb', line 17

def ext
  @ext
end

#outdirObject

Returns the value of attribute outdir.



16
17
18
# File 'lib/rbtune/radio.rb', line 16

def outdir
  @outdir
end

Class Method Details

.bandsObject



49
50
51
# File 'lib/rbtune/radio.rb', line 49

def self.bands
  @@bands
end

.channelsObject



44
45
46
# File 'lib/rbtune/radio.rb', line 44

def self.channels
  @channels ||= self.stations.map {|st| [st.id, st.uri]}.to_h
end

.dbObject



34
35
36
# File 'lib/rbtune/radio.rb', line 34

def self.db
  @@db ||= Station::pstore_db
end

.find(id) ⇒ Object

Radioクラスのリストから、id と一致する放送局を探すreturn: [Radioクラス, 放送局] or nil



61
62
63
64
65
66
67
68
69
# File 'lib/rbtune/radio.rb', line 61

def self.find(id)
  Radio.bands.each do |tuner|
    if tuner.stations
      station = tuner.stations.find {|station| station.id == id}
      return [tuner, station] if station
    end
  end
  nil
end

.inherited(subclass) ⇒ Object



22
23
24
25
# File 'lib/rbtune/radio.rb', line 22

def Radio.inherited(subclass)
  @@bands ||= []
  @@bands << subclass
end

.match(name) ⇒ Object

Radioクラスのリストから、name を含む放送局を探すreturn: [Radioクラス, 放送局] or nil



74
75
76
77
78
79
80
81
82
# File 'lib/rbtune/radio.rb', line 74

def self.match(name)
  matcher = /#{name}/i
  Radio.bands.each do |tuner|
    next unless tuner.stations
    found = tuner.stations.find { |station| station.name.match?(matcher) || station.ascii_name.match?(matcher) }
    return [tuner, found] if found
  end
  nil
end

.search(channel) ⇒ Object



54
55
56
# File 'lib/rbtune/radio.rb', line 54

def self.search(channel)
  radio_class, station = self.find(channel) || self.match(channel)
end

.stationsObject



39
40
41
# File 'lib/rbtune/radio.rb', line 39

def self.stations
  @stations ||= self.db.transaction(true) { self.db[name] }
end

Instance Method Details

#agentObject



85
86
87
# File 'lib/rbtune/radio.rb', line 85

def agent
  @agent ||= Mechanize.new
end

#channel_to_uriObject



111
112
113
# File 'lib/rbtune/radio.rb', line 111

def channel_to_uri
  self.class::channels[@channel] || @channel
end

#closeObject



102
103
# File 'lib/rbtune/radio.rb', line 102

def close
end

#convert(tmpfile, recfile) ⇒ Object



161
162
163
# File 'lib/rbtune/radio.rb', line 161

def convert(tmpfile, recfile)
  FileUtils.mv tmpfile, recfile
end

#convert_ffmpeg(tmpfile, recfile) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/rbtune/radio.rb', line 166

def convert_ffmpeg(tmpfile, recfile)
  ffmpeg = FFMpeg.new
  ffmpeg['loglevel'] = 'quiet'
  ffmpeg['i'] = %Q("#{tmpfile}")
  ffmpeg['b:a'] = '70k'
  stdout, stderr, status = ffmpeg.rec recfile, nil
  FileUtils.rm tmpfile if status.success?
end

#create_player(uri) ⇒ Object



90
91
92
# File 'lib/rbtune/radio.rb', line 90

def create_player(uri)
  # rtmpdumpのコマンドラインを生成する(playから呼ばれる)
end

#datetime(dt) ⇒ Object



176
177
178
# File 'lib/rbtune/radio.rb', line 176

def datetime(dt)
  dt.to_s[0..15].gsub(/:/, '=')
end

#fetch_stationsObject



195
196
197
198
# File 'lib/rbtune/radio.rb', line 195

def fetch_stations
  body = agent.get stations_uri
  stations = parse_stations body
end

#login(account = nil, password = nil) ⇒ Object



95
96
97
# File 'lib/rbtune/radio.rb', line 95

def (=nil, password=nil)
  # ラジオサービスにログイン
end

#make_recfile(title, datetime) ⇒ Object



190
191
192
# File 'lib/rbtune/radio.rb', line 190

def make_recfile(title, datetime)
  File.join outdir, "#{title}.#{datetime}.#{out_ext}"
end

#make_tmpfile(channel, datetime) ⇒ Object



181
182
183
# File 'lib/rbtune/radio.rb', line 181

def make_tmpfile(channel, datetime)
  File.join outdir, "#{channel}.#{datetime}.#{$$}.#{ext}"
end

#openObject



99
100
# File 'lib/rbtune/radio.rb', line 99

def open
end

#out_extObject



186
187
188
# File 'lib/rbtune/radio.rb', line 186

def out_ext
  @out_ext || ext
end

#playObject



153
154
155
156
157
158
# File 'lib/rbtune/radio.rb', line 153

def play
  uri = channel_to_uri
  raise 'not tuned yet.' unless uri
  puts "play: #{uri}"
  create_player(uri).play
end

#record(filename, sec, quiet: false, dt: DateTime.now) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rbtune/radio.rb', line 116

def record(filename, sec, quiet: false, dt: DateTime.now)
  begin
    uri = channel_to_uri
    raise 'not tuned yet.' unless uri

    puts "record: #{uri}"
    # $stderr.puts "play: #{sec}, #{filename}, #{quiet}"
    player      = create_player uri
    remain_sec  = sec
    rtime       = 0
    minimum_sec = 60   # 残り録音時間がこれ以下ならば、録音が中断してもやり直さない
    datetimes   = []
    begin
      rtime += Benchmark.realtime do
        dt = datetime dt
        datetimes << dt
        tmpfile = make_tmpfile @channel, dt
        player.rec tmpfile, remain_sec, quiet
      end
      remain_sec -= rtime
      dt = DateTime.now
    end while remain_sec >= minimum_sec

  rescue Interrupt, Errno::EPIPE
    # do nothing

  ensure
    # 最後にまとめて convert する
    datetimes.each do |dt|
      tmpfile = make_tmpfile @channel, dt
      recfile = make_recfile(filename, dt)
      convert tmpfile, recfile
    end
  end
end

#tune(channel) ⇒ Object



106
107
108
# File 'lib/rbtune/radio.rb', line 106

def tune(channel)
  @channel = channel
end