Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
フェイトちゃんちゅっちゅ
# -*- 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
You can’t perform that action at this time.