Skip to content

Instantly share code, notes, and snippets.

@kimoto
Created March 2, 2012 04:04
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 kimoto/1955545 to your computer and use it in GitHub Desktop.
Save kimoto/1955545 to your computer and use it in GitHub Desktop.
はてなお気に入りのフィードのIRC Gateway
#!/bin/env ruby
# encoding: utf-8
# Author: kimoto
require 'net/irc'
require 'net/irc/mala'
require 'rss'
require 'open-uri'
class Net::IRC::HateFav < Net::IRC::Server::Session
def server_name
"hatefav"
end
def main_channel
"#hatefav"
end
def initialize(*args)
super
end
def on_user(m)
super
post server_name, MODE, @nick, "+o"
post @prefix, JOIN, main_channel
post server_name, MODE, main_channel, "+mto", @nick
post server_name, MODE, main_channel, "+q", @nick
@streaming_thread = Thread.start do
on_new_thread
end
end
def on_new_thread
last_fetched_at = nil
wait_time = 60
while true
data = open("http://b.hatena.ne.jp/#{@real}/favorite.rss").read
rss = RSS::Parser.parse(data)
rss.items.sort_by{ |e|
e.date
}.each do |item|
username = item.dc_creator
title = [item.title, item.link].join(" - ")
if last_fetched_at.nil? or last_fetched_at < item.date
last_fetched_at = item.date
puts [username, title].join(": ")
post username, PRIVMSG, main_channel, title
end
end
STDERR.puts "wait time: #{wait_time}"
sleep wait_time
end
end
def on_disconnected
@streaming_thread.kill rescue nil
end
end
Net::IRC::CLI.run(Net::IRC::HateFav, ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment