Class: Kaede::Notifier
- Inherits:
-
Object
- Object
- Kaede::Notifier
- Defined in:
- lib/kaede/notifier.rb
Instance Method Summary collapse
- #available_disk ⇒ Object
- #format_title(program) ⇒ Object
- #in_gigabyte(size) ⇒ Object
-
#initialize ⇒ Notifier
constructor
A new instance of Notifier.
- #notify_after_record(program) ⇒ Object
- #notify_before_record(program) ⇒ Object
- #notify_duration_error(program, got_duration) ⇒ Object
- #notify_exception(exception, program) ⇒ Object
- #notify_redo_error(program) ⇒ Object
-
#record_path(program) ⇒ Object
FIXME: duplicate.
- #ts_filesize(program) ⇒ Object
- #tweet(text) ⇒ Object
Constructor Details
#initialize ⇒ Notifier
Returns a new instance of Notifier.
6 7 8 9 |
# File 'lib/kaede/notifier.rb', line 6 def initialize @twitter = Kaede.config.twitter @twitter_target = Kaede.config.twitter_target end |
Instance Method Details
#available_disk ⇒ Object
68 69 70 71 |
# File 'lib/kaede/notifier.rb', line 68 def available_disk _, avail = `#{Kaede.config.statvfs} #{Kaede.config.record_dir}`.chomp.split(/\s/, 2).map(&:to_i) in_gigabyte(avail) end |
#format_title(program) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/kaede/notifier.rb', line 50 def format_title(program) buf = "#{program.channel_name}で「#{program.title}" if program.count buf += " ##{program.count}" end buf += " #{program.subtitle}」" buf end |
#in_gigabyte(size) ⇒ Object
73 74 75 |
# File 'lib/kaede/notifier.rb', line 73 def in_gigabyte(size) size / (1024 * 1024 * 1024) end |
#notify_after_record(program) ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/kaede/notifier.rb', line 15 def notify_after_record(program) tweet( sprintf( "%sを録画した。ファイルサイズ約%.2fGB。残り約%dGB\n", format_title(program), ts_filesize(program), available_disk, ) ) end |
#notify_before_record(program) ⇒ Object
11 12 13 |
# File 'lib/kaede/notifier.rb', line 11 def notify_before_record(program) tweet("#{format_title(program)}を録画する") end |
#notify_duration_error(program, got_duration) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/kaede/notifier.rb', line 34 def notify_duration_error(program, got_duration) msg = sprintf('%s (PID:%d) の長さが%g秒しか無いようだが……', format_title(program), program.pid, got_duration) if @twitter_target msg = "@#{@twitter_target} #{msg}" end tweet(msg) end |
#notify_exception(exception, program) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/kaede/notifier.rb', line 26 def notify_exception(exception, program) msg = "#{program.title}(PID #{program.pid}) の録画中に #{exception.class} で失敗した……" if @twitter_target msg = "@#{@twitter_target} #{msg}" end tweet(msg) end |
#notify_redo_error(program) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/kaede/notifier.rb', line 42 def notify_redo_error(program) msg = "再実行にも失敗した…… (PID:#{program.pid})" if @twitter_target msg = "@#{@twitter_target} #{msg}" end tweet(msg) end |
#record_path(program) ⇒ Object
FIXME: duplicate
64 65 66 |
# File 'lib/kaede/notifier.rb', line 64 def record_path(program) Kaede.config.record_dir.join("#{program.tid}_#{program.pid}.ts") end |
#ts_filesize(program) ⇒ Object
59 60 61 |
# File 'lib/kaede/notifier.rb', line 59 def ts_filesize(program) in_gigabyte(record_path(program).size.to_f) end |
#tweet(text) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/kaede/notifier.rb', line 77 def tweet(text) return unless @twitter Thread.start do begin @twitter.update(text) rescue Exception => e $stderr.puts "Failed to tweet: #{text}: #{e.class}: #{e.message}" end end end |