-
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.
Showing
2 changed files
with
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# vim: fileencoding=utf-8 | ||
|
||
require 'uri' | ||
require 'open-uri' | ||
require 'json' | ||
|
||
module RGRB | ||
module Plugin | ||
# オンラインセッション情報検索プラグイン | ||
module OnlineSessionSearch | ||
# OnlineSessionSearch の出力テキスト生成器 | ||
class Generator | ||
# セッションマッチングシステム JSON 形式データの URL | ||
SESSION_JSON_URL = 'http://session.trpg.net/json.php2' | ||
# 情報取得に失敗したときのエラーメッセージ | ||
GET_ERROR_MESSAGE = 'オンラインセッション情報の取得に失敗しました' | ||
|
||
def latest_schedules(n = 5) | ||
url = "#{SESSION_JSON_URL}?n=#{n}" | ||
|
||
begin | ||
session_data = get_session_data(url) | ||
rescue | ||
[GET_ERROR_MESSAGE] | ||
end | ||
end | ||
|
||
def search(str, n = 5) | ||
params = { | ||
s: str, | ||
n: n | ||
} | ||
url = "#{SESSION_JSON_URL}?#{URI.encode_www_form(params)}" | ||
|
||
begin | ||
session_data = get_session_data(url) | ||
rescue | ||
[GET_ERROR_MESSAGE] | ||
end | ||
end | ||
|
||
def get_session_data(url) | ||
json = open(url, 'r:UTF-8') { |f| f.read } | ||
parse(json) | ||
end | ||
private :get_session_data | ||
|
||
def body(session_data) | ||
if session_data.empty? | ||
['開催予定のセッションは見つかりませんでしたの☆'] | ||
else | ||
end | ||
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,54 @@ | ||
# vim: fileencoding=utf-8 | ||
|
||
require 'cinch' | ||
require 'rgrb/plugin/online_session_search/generator' | ||
|
||
module RGRB | ||
module Plugin | ||
module OnlineSessionSearch | ||
# OnlineSessionSearch の IRC アダプター | ||
class IrcAdapter | ||
include Cinch::Plugin | ||
|
||
# セッションマッチングシステムの URL | ||
SESSION_URL = 'http://session.trpg.net/' | ||
# 一覧の URL を提示するメッセージ | ||
LIST_MESSAGE = "一覧は #{SESSION_URL} からどうぞ♪" | ||
|
||
set(plugin_name: 'OnlineSessionSearch') | ||
match(/ons\s*$/, method: :latest_schedules) | ||
match(/ons\s+(.+)/, method: :search) | ||
|
||
def initialize(*args) | ||
super | ||
|
||
@generator = Generator.new | ||
end | ||
|
||
def latest_schedules(m) | ||
m.target.send( | ||
'最近追加されたオンラインセッション情報ですわ☆', true | ||
) | ||
m.target.send(LIST_MESSAGE, true) | ||
|
||
messages = @generator.latest_schedules(5) | ||
messages.each do |s| | ||
m.target.send(s, true) | ||
end | ||
end | ||
|
||
def search(m, str) | ||
m.target.safe_send( | ||
"オンラインセッション情報検索: #{str}", true | ||
) | ||
m.target.send(LIST_MESSAGE, true) | ||
|
||
messages = @generator.search(str, 5) | ||
messages.each do |s| | ||
m.target.send(s, true) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |