しょんぼり技術メモ

まいにちがしょんぼり

TypableMap Timelineコマンドを追加する

TIG(TwitterIRCGateway)のTypableMapについては、TwitterIRCGatewayをより(俺にとって)便利にする - しょんぼり技術メモをご参照ください。

あるユーザの発言を表示したいときには、#Consoleで、

timeline [screen_name]

としてやれば最近の発言が取得できます。が、めんどくさいですよね。

そこで、TypableMapで指定したユーザの最近の発言を取得するDLR拡張を書きました。
あまり説明することもないと思うので、全文を掲載します。

使い方ですが、#Consoleのdlrコンテキストでreloadするなり、TIGを再起動するなりしてスクリプトを認識させてから

12:34:56 syonbori: こうすることで協調型仮想計算機ができるんだ。(ka)
-------------------------------------------------------------------
t ka

とやると、syonboriの最近の発言3件(デフォルト。後述)が表示されます。

12:34:56 syonbori: こうすることで協調型仮想計算機ができるんだ。(ka)
-------------------------------------------------------------------
t ka 10

とやると、syonboriの最近の発言10件が表示されます。

以下コード全文。timeline_by_tm.rb

module Misuzilla::IronRuby
  module TypableMap
	include Misuzilla::Applications::TwitterIrcGateway::AddIns::TypableMap

	@@commands = []

	def self.setup
	  @@typablemap_proc = Session.AddInManager.GetAddIn(Misuzilla::Applications::TwitterIrcGateway::AddIns::TypableMapSupport.to_clr_type).TypableMapCommands

	  # スクリプトアンロード時にコマンドを削除する
	  Session.AddInManager.GetAddIn(Misuzilla::Applications::TwitterIrcGateway::AddIns::DLRIntegration::DLRIntegrationAddIn.to_clr_type).BeforeUnload do |sender, e|
		@@commands.each do |command|
		  @@typablemap_proc.RemoveCommand(command)
		end
	  end
	end

	def self.register(command, desc, &proc_cmd)
	  @@commands << command
	  @@typablemap_proc.AddCommand(command, desc, ProcessCommand.new{|p, msg, status, args|
		proc_cmd.call(p, msg, status, args)
	  })
	end

	setup
  end
end


# TypableMap: t(timeline) コマンドを追加する
Misuzilla::IronRuby::TypableMap.register("t", "timeline Command") do |p, msg, status, args|
  Session.RunCheck(Misuzilla::Applications::TwitterIrcGateway::Procedure.new{
	# 表示件数省略時のデフォルト件数
	Def_count = 3
	
	u=status.user
	sname = u.screen_name
	
	# 表示件数をセット
	count = Def_count
	if args.to_s != ""
		count = args.to_i rescue ""
	end
	count = Def_count if count<=0

	# 現在時刻以前、を指定
	since = System::DateTime.new
	# 指定条件で取得
	friend_tl = Session.twitter_service.get_timeline_by_screen_name(sname, since, count)

	if friend_tl != nil and friend_tl.status.length > 0
		# ヘッダを表示
		target = friend_tl.status[0]
		outtext = "User #{target.user.name}(#{target.user.screen_name})'s Timeline (count=#{count})"
		Session.send_server(Misuzilla::Net::Irc::NoticeMessage.new(	msg.receiver,
					outtext))

		# 各発言についてのループ(古い発言から順に表示するためにreverse)
		friend_tl.status.reverse.each do |stat|
			time = stat.created_at.strftime("%H:%M:%S").to_s
			outtext =  "  #{time}: #{stat.text}"
			Session.send_server(Misuzilla::Net::Irc::NoticeMessage.new(	msg.receiver,
						outtext))
		end
	end
  }, System::Action[System::Exception].new{|ex|
	Session.send_channel_message(msg.receiver, Server.server_nick, "メッセージ送信に失敗しました", false, false, true)
  })

  true # true を返すとハンドルしたことになりステータス更新処理は行われない
end

デフォルトの発言表示件数が少ないと思ったら、Def_countを適当に変更してください。

Copyright © 2009 syonbori_tech

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.