Skip to content

Instantly share code, notes, and snippets.

@snapcase snapcase/cinch-youtube.rb
Last active Aug 29, 2015

Embed
What would you like to do?
cinch-bot fetching youtube titles
require 'bundler/setup'
require 'open-uri'
require 'json'
require 'cinch'
API_KEY = 'meep'
class Youtube
include Cinch::Plugin
listen_to :channel
# @param [Array]
# @return [Array, nil]
def title(id)
url = "https://www.googleapis.com/youtube/v3/videos?id=#{id.join(',')}&" + \
"key=#{API_KEY}&part=snippet&fields=items(id,snippet(title))"
json = JSON.load(open(url))
json["items"].map { |item| "#{item["id"]} : #{item["snippet"]["title"]}" }
rescue OpenURI::HTTPError
nil
end
def listen(m)
id = m.message.scan(
%r{
(?:youtube(?:-nocookie)?\.com\/
(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|
\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})
}x
).uniq
unless id.empty?
titles = title(id)
m.reply titles.join(", ")
end
end
end
bot = Cinch::Bot.new do
configure do |c|
c.nick = "rubyrage"
c.server = "irc.freenode.net"
c.channels = ["#chan1", "#chan2"]
c.port = 7000
c.ssl.use = true
c.ssl.verify = true
c.plugins.plugins = [Youtube]
end
end
bot.loggers.level = :info
bot.start
@snapcase

This comment has been minimized.

Copy link
Owner Author

snapcase commented May 12, 2015

Usage

With bundler

http://bundler.io

Gemfile

source 'https://rubygems.org'
gem 'cinch'
  • Place the Gemfile in the same folder
% gem install bundler
% bundle install --path vendor/bundle
  • Make sure PATH is set (~/.gem/ruby/2.2.0/bin)

Without bundler

% gem install cinch
  • Remove the very first line from the code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.