-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
発言の中のURLを取得し、そのページのタイトルを取得します。 ToDo: * エラーが出た場合にrescueで分岐して例外処理 * タイムアウトを設定する * User-Agent を設定ファイルで設定できるようにする
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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 |
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
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 |