Skip to content

Instantly share code, notes, and snippets.

@snapcase
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snapcase/a1efd9df5d4f1b02514d to your computer and use it in GitHub Desktop.
Save snapcase/a1efd9df5d4f1b02514d to your computer and use it in GitHub Desktop.
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
Copy link
Author

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