Created
August 8, 2014 07:18
-
-
Save mashiro/2696f2d4dac30fc7b956 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 'nokogiri' | |
require 'kconv' | |
class URL | |
include Donkun::Plugin | |
set :help, <<-EOS | |
URL の情報を表示します | |
EOS | |
match /((https?):\/\/(([^[:space:].\/]+\.)+[a-z]+)(:\d+)?(\/[^[:space:]]*)?)/i, :use_prefix => false, :use_suffix => false | |
def execute(m, url) | |
text = fetch url | |
m.target.notice text if text | |
rescue RestClient::Exception => e | |
m.target.notice "#{e.message} #{OWATA}" | |
end | |
private | |
class Page | |
attr_reader :plugin, :url | |
def initialize(plugin, url, request_options = {}, options = {}) | |
@plugin = plugin | |
@url = url | |
@request_options = request_options | |
@timeout = options[:timeput] || 5 | |
end | |
def headers | |
unless @headers | |
timeout @timeout do | |
begin | |
request_head | |
rescue RestClient::MethodNotAllowed | |
request_get | |
end | |
end | |
end | |
@headers | |
end | |
def body | |
unless @body | |
timeout @timeout do | |
request_get | |
end | |
end | |
@body | |
end | |
def doc | |
@doc ||= Nokogiri::HTML(body) | |
end | |
private | |
def request_head | |
res = @plugin.head @url, :options => @request_options | |
@headers = res.headers | |
end | |
def request_get | |
res = @plugin.get @url, :options => @request_options | |
@headers = res.headers | |
@body = res.body | |
@body = @body.toutf8 unless @headers[:content_type] =~ /utf-?8/i | |
end | |
end | |
def open(url) | |
Page.new self, url, request_options(url) | |
end | |
def fetch(url) | |
page = open url | |
case page.headers[:content_type] | |
when /html/ | |
on_html page | |
else | |
on_binary page | |
end | |
end | |
def on_binary(page) | |
content_length = page.headers[:content_length].to_i | |
human_size = ['B', 'KB', 'MB', 'GB', 'TB'].inject(content_length) do |n, r| | |
n < 1024 ? (break "#{n.round(2)} #{r}") : n / 1024.0 | |
end | |
"#{page.headers[:content_type]} (#{human_size})" | |
end | |
def on_html(page) | |
text = case page.url | |
when %r(https?://(?:\w+\.)?twitter\.com/(\w+)/status(?:es)?/(\d+)) | |
on_twitter $1, $2 | |
else | |
page.doc.title | |
end | |
sanitize text | |
end | |
def on_twitter(screen_name, id) | |
page = open "https://twitter.com/#{screen_name}/status/#{id}" | |
stream_protected = page.doc.at_css('.stream-protected h2') | |
return stream_protected.text if stream_protected | |
tweet = page.doc.at_css('.permalink-tweet') | |
name = tweet['data-screen-name'] | |
text = tweet.at_css('.tweet-text').text | |
"#{name}: #{text}" | |
end | |
def request_options(url) | |
options = config[:request_options] || {} | |
options.each do |k, v| | |
return v if url =~ k | |
end | |
{} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment