From 43cf49990da12ea3383626ce5ed9a4b57387f172 Mon Sep 17 00:00:00 2001 From: koi-chan Date: Mon, 23 Feb 2015 20:32:43 +0900 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E8=A6=8F=E3=83=97=E3=83=A9=E3=82=B0?= =?UTF-8?q?=E3=82=A4=E3=83=B3=20UrlFetchTitle=20=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 発言の中のURLを取得し、そのページのタイトルを取得します。 ToDo: * エラーが出た場合にrescueで分岐して例外処理 * タイムアウトを設定する * User-Agent を設定ファイルで設定できるようにする --- Gemfile | 1 + Gemfile.lock | 24 ++++++++++++ lib/rgrb/plugin/url_fetch_title/generator.rb | 29 +++++++++++++++ .../plugin/url_fetch_title/irc_adapter.rb | 37 +++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 lib/rgrb/plugin/url_fetch_title/generator.rb create mode 100644 lib/rgrb/plugin/url_fetch_title/irc_adapter.rb diff --git a/Gemfile b/Gemfile index e98dc40f..1d0758fe 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 58f4355d..431a5b5f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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 @@ -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) diff --git a/lib/rgrb/plugin/url_fetch_title/generator.rb b/lib/rgrb/plugin/url_fetch_title/generator.rb new file mode 100644 index 00000000..995dcf20 --- /dev/null +++ b/lib/rgrb/plugin/url_fetch_title/generator.rb @@ -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 diff --git a/lib/rgrb/plugin/url_fetch_title/irc_adapter.rb b/lib/rgrb/plugin/url_fetch_title/irc_adapter.rb new file mode 100644 index 00000000..de5ddf4b --- /dev/null +++ b/lib/rgrb/plugin/url_fetch_title/irc_adapter.rb @@ -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