Last active
August 29, 2015 14:21
-
-
Save snapcase/a1efd9df5d4f1b02514d to your computer and use it in GitHub Desktop.
cinch-bot fetching youtube titles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
With bundler
http://bundler.io
Gemfile
Without bundler