lib/agig/session.rb in agig-0.0.1 vs lib/agig/session.rb in agig-0.0.2

- old
+ new

@@ -1,9 +1,10 @@ require 'rubygems' require 'net/irc' -require 'net/https' -require 'libxml' +require 'nokogiri' + +require 'open-uri' require 'ostruct' require 'time' class Agig::Session < Net::IRC::Server::Session EVENTS = { @@ -14,10 +15,17 @@ 'CreateEvent' => '13', 'ForkEvent' => '3', 'PushEvent' => '14', } + ACTIVITIES = %w( + GistEvent + ForkEvent + FollowEvent + WatchEvent + ) + def server_name "github" end def server_version @@ -29,61 +37,51 @@ end def initialize(*args) super @last_retrieved = Time.now - @cert_store = OpenSSL::X509::Store.new - @cert_store.set_default_paths end def on_disconnected @retrieve_thread.kill rescue nil end def on_user(m) super + @real, *@opts = @real.split(/\s+/) @opts = OpenStruct.new @opts.inject({}) {|r, i| key, value = i.split("=", 2) r.update key => case value when nil then true when /\A\d+\z/ then value.to_i when /\A(?:\d+\.\d*|\.\d+)\z/ then value.to_f else value end } - post @nick, JOIN, main_channel + [main_channel, '#activity'].each {|c| post @nick, JOIN, c } @retrieve_thread = Thread.start do loop do begin @log.info 'retrieveing feed...' - uri = URI.parse("https://github.com/#{@real}.private.atom?token=#{@pass}") - http = Net::HTTP.new(uri.host, uri.port) - http.use_ssl = true - http.verify_mode = OpenSSL::SSL::VERIFY_PEER - http.cert_store = @cert_store - req = Net::HTTP::Get.new(uri.request_uri) - res = http.request(req) - - doc = LibXML::XML::Document.string(res.body, :base_uri => uri.to_s) - ns = %w|a:http://www.w3.org/2005/Atom| - entries = [] - doc.find('/a:feed/a:entry', ns).each do |n| - entries << { - :datetime => Time.parse(n.find('string(a:published)', ns)), - :id => n.find('string(a:id)', ns), - :title => n.find('string(a:title)', ns), - :author => n.find('string(a:author/a:name)', ns), - :link => n.find('string(a:link/@href)', ns), + atom = open("https://github.com/#{@real}.private.atom?token=#{@pass}").read + ns = {'a' => 'http://www.w3.org/2005/Atom'} + entries = Nokogiri::XML(atom).xpath('/a:feed/a:entry', ns).map do |entry| + { + :datetime => Time.parse(entry.xpath('string(a:published)', ns)), + :id => entry.xpath('string(a:id)', ns), + :title => entry.xpath('string(a:title)', ns), + :author => entry.xpath('string(a:author/a:name)', ns), + :link => entry.xpath('string(a:link/@href)', ns), } end entries.reverse_each do |entry| next if entry[:datetime] <= @last_retrieved type = entry[:id][%r|tag:github.com,2008:(.+?)/\d+|, 1] - post entry[:author], PRIVMSG, main_channel, - "\003#{EVENTS[type] || '5'}#{entry[:title]}\017 \00314#{entry[:link]}\017" + channel = ACTIVITIES.include? type ? '#activity' : main_channel + post entry[:author], PRIVMSG, channel, "\003#{EVENTS[type] || '5'}#{entry[:title]}\017 \00314#{entry[:link]}\017" end @last_retrieved = entries.first[:datetime] @log.info 'sleep' sleep 30