Class: Cinch::Plugins::NicoVideo

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/cinch/plugins/nicovideo.rb

Constant Summary collapse

NICONICO_VIDEO_REGEXP =

From Google's Closure Library, with slight modifications: closure-library.googlecode.com/svn/docs/closure_goog_ui_media_youtube.js.source.html

/https?:\/\/(?:[a-zA-Z]{2,3}\.)?(?:nicovideo\.jp\/watch\/)(sm\d+)/i
NICONICO_API_VIDEO_URL =
"http://ext.nicovideo.jp/api/getthumbinfo/%s"

Instance Method Summary collapse

Instance Method Details

#listen_to_channel(m) ⇒ Object


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cinch/plugins/nicovideo.rb', line 17

def listen_to_channel(m)
  return if is_channel_disabled?(m.channel)
  return unless m.message.match(NICONICO_VIDEO_REGEXP)
  videos = m.message.scan(NICONICO_VIDEO_REGEXP).flatten.reject(&:'nil?').uniq

  videos.each {|v|
    response = HTTParty.get(NICONICO_API_VIDEO_URL % v, headers: {'User-Agent' => "HTTParty/#{HTTParty::VERSION} #{RUBY_ENGINE}/#{RUBY_VERSION}"})["nicovideo_thumb_response"]
    raise StandardError, 'Video %s - %s' % [response['error']['description'], v] if response['status'].eql?('fail')
    video = response['thumb']
    m.reply "#{Format(:bold,'NicoNico »')} #{Format(:purple,'%<title>s')} (%<length>s) · uploaded %<uploaded>s · #{Format(:green,'%<likes>s mylists')} · %<views>s views" % {
      title: video['title'],
      uploaded: Time.parse(video['first_retrieve']).strftime('%F'),
      length: video['length'],
      likes: commify_numbers(video['mylist_counter'].to_i),
      views: commify_numbers(video['view_counter'].to_i)
    }
  }
rescue => e
  m.reply "#{Format(:bold,'NicoNico »')} #{e.message}"
end