Created
January 23, 2012 09:47
-
-
Save hirataya/1662101 to your computer and use it in GitHub Desktop.
フェイトちゃんちゅっちゅ
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
# -*- coding: utf-8 -*- | |
require "uri" | |
require "open-uri" | |
require "nokogiri" | |
require "kconv" | |
class Fatechan::Plugin::GetURITitle | |
include Cinch::Plugin | |
listen_to :channel | |
def listen(m) | |
return if not m.command == "PRIVMSG" | |
URI.extract(m.message, %w{http}) do |uri| | |
open(uri) do |f| | |
title = nil | |
if f.content_type == "text/html" then | |
content = f.read | |
doc = Nokogiri::HTML(content) | |
# XXX: Should use lower-case function if XPath 2.0 is available. | |
html_content_type = doc.xpath(%{ | |
//meta[ | |
translate( | |
@http-equiv, | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', | |
'abcdefghijklmnopqrstuvwxyz' | |
) = 'content-type' | |
][1] | |
}) | |
if html_content_type =~ /charset=([\w_-]+)/ then | |
encoding = $1 | |
else | |
encoding = Kconv.guess(content) | |
end | |
content.force_encoding(encoding).encode | |
doc = Nokogiri::HTML(content) | |
title = doc.xpath("//title[1]/text()") | |
title = nil if title =~ /^\s*$/ | |
elsif f.content_type == "text/plain" then | |
title = f.gets.chomp | |
title.force_encoding(Kconv.guess(title)).encode | |
end | |
title ||= f.content_type || "Untitled" | |
m.channel.notice "#{m.user.nick}: #{title}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment