Skip to content

Commit

Permalink
新規プラグイン UrlFetchTitle を追加
Browse files Browse the repository at this point in the history
発言の中のURLを取得し、そのページのタイトルを取得します。

ToDo:
* エラーが出た場合にrescueで分岐して例外処理
* タイムアウトを設定する
* User-Agent を設定ファイルで設定できるようにする
  • Loading branch information
koi-chan committed Feb 23, 2015
1 parent ad70e75 commit 43cf499
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ gem 'activesupport', '~> 4.2'
gem 'cinch', '~> 2.1'
gem 'twitter', '~> 5.11'
gem 'sysexits', '~> 1.2'
gem 'mechanize', '~> 2.7.3'

group :development, :test do
gem 'pry', '~> 0.10'
Expand Down
24 changes: 24 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,42 @@ GEM
safe_yaml (~> 1.0.0)
diff-lcs (1.2.5)
docile (1.1.5)
domain_name (0.5.23)
unf (>= 0.0.5, < 1.0.0)
equalizer (0.0.9)
faraday (0.9.1)
multipart-post (>= 1.2, < 3)
http (0.6.3)
http_parser.rb (~> 0.6.0)
http-cookie (1.0.2)
domain_name (~> 0.5)
http_parser.rb (0.6.0)
i18n (0.7.0)
json (1.8.2)
mechanize (2.7.3)
domain_name (~> 0.5, >= 0.5.1)
http-cookie (~> 1.0)
mime-types (~> 2.0)
net-http-digest_auth (~> 1.1, >= 1.1.1)
net-http-persistent (~> 2.5, >= 2.5.2)
nokogiri (~> 1.4)
ntlm-http (~> 0.1, >= 0.1.1)
webrobots (>= 0.0.9, < 0.2)
memoizable (0.4.2)
thread_safe (~> 0.3, >= 0.3.1)
method_source (0.8.2)
mime-types (2.4.3)
mini_portile (0.6.2)
minitest (5.5.1)
multi_json (1.10.1)
multipart-post (2.0.0)
naught (1.0.0)
net-http-digest_auth (1.4)
net-http-persistent (2.9.4)
netrc (0.10.2)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
ntlm-http (0.1.1)
parser (2.2.0.3)
ast (>= 1.1, < 3.0)
powerpack (0.1.0)
Expand Down Expand Up @@ -100,9 +119,13 @@ GEM
simple_oauth (~> 0.3.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
unf (0.1.4)
unf_ext
unf_ext (0.0.6)
webmock (1.20.4)
addressable (>= 2.3.6)
crack (>= 0.3.2)
webrobots (0.1.1)
yard (0.8.7.6)

PLATFORMS
Expand All @@ -112,6 +135,7 @@ DEPENDENCIES
activesupport (~> 4.2)
cinch (~> 2.1)
coveralls
mechanize (~> 2.7.3)
pry (~> 0.10)
rake (~> 10.4)
rspec (~> 3.1)
Expand Down
29 changes: 29 additions & 0 deletions lib/rgrb/plugin/url_fetch_title/generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# vim: fileencoding=utf-8

require 'uri'
require 'mechanize'

module RGRB
module Plugin
# ウェブページタイトル自動取得プラグイン
module UrlFetchTitle
# UrlFetchTitle の出力テキスト生成器
class Generator

def initialize(*args)
super

@agent = Mechanize.new
@agent.user_agent = "RGRB/#{VERSION} (Creator's Network IRC bot)"
end

# 誰かが発言した URL にアクセスし、ページのTitleタグを取得する
def fetch_title(url)
page = @agent.get(url)
page.title
end

end
end
end
end
37 changes: 37 additions & 0 deletions lib/rgrb/plugin/url_fetch_title/irc_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# vim: fileencoding=utf-8

require 'cinch'
require 'uri'
require 'pp'

require 'rgrb/plugin/url_fetch_title/generator'

module RGRB
module Plugin
module UrlFetchTitle
# UrlFetchTitle の IRC アダプター
class IrcAdapter
include Cinch::Plugin

set(plugin_name: 'UrlFetchTitle')
self.prefix = ''
match(/(.*)/, method: :fetch_title)

def initialize(*args)
super

@generator = Generator.new
end

# NOTICE でページのタイトルを返す
# @return [void]
def fetch_title(m, message)
urls = URI.extract(message, ['http'])
urls.each { |url|
m.target.send(@generator.fetch_title(url), true)
}
end
end
end
end
end

0 comments on commit 43cf499

Please sign in to comment.