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.



30
31
32
33
# File 'lib/rbtune/radio.rb', line 30

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

Instance Attribute Details

#area_enObject (readonly)

for Radiko(Premium)



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

def area_en
  @area_en
end

#area_idObject (readonly)

for Radiko(Premium)



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

def area_id
  @area_id
end

#area_jaObject (readonly)

for Radiko(Premium)



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

def area_ja
  @area_ja
end

#extObject (readonly)

Returns the value of attribute ext.



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

def ext
  @ext
end

#outdirObject

Returns the value of attribute outdir.



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

def outdir
  @outdir
end

Class Method Details

.bandsObject



51
52
53
# File 'lib/rbtune/radio.rb', line 51

def self.bands
  @@bands
end

.channelsObject



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

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

.dbObject



36
37
38
# File 'lib/rbtune/radio.rb', line 36

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

.find(id) ⇒ Object

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



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

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



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

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

.match(name) ⇒ Object

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



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

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



56
57
58
# File 'lib/rbtune/radio.rb', line 56

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

.stationsObject



41
42
43
# File 'lib/rbtune/radio.rb', line 41

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

Instance Method Details

#agentObject



87
88
89
# File 'lib/rbtune/radio.rb', line 87

def agent
  @agent ||= Mechanize.new
end

#channel_to_uriObject



117
118
119
# File 'lib/rbtune/radio.rb', line 117

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

#closeObject



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

def close
end

#convert(tmpfile, recfile) ⇒ Object



167
168
169
# File 'lib/rbtune/radio.rb', line 167

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

#convert_ffmpeg(tmpfile, recfile) ⇒ Object



172
173
174
175
176
177
178
179
# File 'lib/rbtune/radio.rb', line 172

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



92
93
94
95
96
97
98
# File 'lib/rbtune/radio.rb', line 92

def create_player(uri)
  # rtmpdumpのコマンドラインを生成する(playから呼ばれる)
  FFMpeg.new( {
    i: uri,
    # acodec: 'copy', # acodecオプションはiオプションのあとに置かないとエラー
  } )
end

#datetime(dt) ⇒ Object



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

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

#fetch_stationsObject



201
202
203
204
# File 'lib/rbtune/radio.rb', line 201

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

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



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

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

#make_recfile(title, datetime) ⇒ Object



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

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

#make_tmpfile(channel, datetime) ⇒ Object



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

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

#openObject



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

def open
end

#out_extObject



192
193
194
# File 'lib/rbtune/radio.rb', line 192

def out_ext
  @out_ext || ext
end

#playObject



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

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



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
151
152
153
154
155
156
# File 'lib/rbtune/radio.rb', line 122

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



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

def tune(channel)
  @channel = channel
end