Created
June 8, 2011 17:57
-
-
Save ku/1014933 to your computer and use it in GitHub Desktop.
rubycocoa skypebot sample
This file contains hidden or 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
#!/usr/bin/ruby | |
require 'rubygems' | |
require 'osx/cocoa' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'nkf' | |
require 'pp' | |
OSX.require_framework 'Skype' | |
class SkypeAPIClient < OSX::NSObject | |
attr_accessor :misuser | |
attr_accessor :passwd | |
def clientApplicationName | |
return 'Skype API Client' | |
end | |
addRubyMethod_withType 'skypeAttachResponse:', 'v@:i' | |
def skypeAttachResponse(status) | |
return if status != 1 | |
puts 'Successfully connected to Skype!' | |
self.sendCommand("GET CHAT #username/$username;9aec91ce397d37d1 TOPIC") | |
end | |
def isRunning | |
return OSX::SkypeAPI.isSkypeRunning | |
end | |
def setDelegate appname | |
OSX::SkypeAPI.setSkypeDelegate self | |
end | |
def setMood text | |
OSX::SkypeAPI.sendSkypeCommand("SET PROFILE MOOD_TEXT #{text}" ) | |
end | |
def sendCommand text | |
puts "COMND #{text}" | |
OSX::SkypeAPI.sendSkypeCommand text | |
end | |
def skypeNotificationReceived(notification) | |
puts "NOTIF #{notification}" | |
tokens = String.new(notification).split(" ") | |
cmd = tokens.shift | |
if cmd == 'CHATS' then | |
tokens.each{|n| | |
p self.sendCommand("GET CHAT #{n} TOPIC") | |
} | |
elsif cmd == 'CHAT' then | |
if tokens[1] == 'TOPIC' then | |
chat_id = tokens.shift | |
prop = tokens.shift | |
topic_text = tokens.join(" ") | |
self.replaceTopicText(chat_id, topic_text) | |
end | |
end | |
end | |
def replaceTopicText(chat_id, current_topic) | |
p m = current_topic.match(/\[(.+?)\]/) | |
if m then | |
newmsg = self.findNextPerson(m[1]) | |
cmd = "ALTER CHAT #{chat_id} SETTOPIC #{newmsg}" | |
puts cmd | |
self.sendCommand(cmd) | |
exit | |
end | |
end | |
def findNextPerson(name) | |
username = @misuser | |
passwd = @passwd | |
host = 'wiki.gree.jp' | |
path = '/index.php' | |
html = NKF.nkf('-w', open("http://#{host}#{path}", :http_basic_authentication => [username, passwd]).read) | |
doc = Nokogiri::HTML(html) | |
order = doc.css('div#sns-master-table td').map do |e| | |
e.text | |
end | |
index = order.index(name) | |
index = (index + 1) % order.length | |
today = Date.today.strftime("%m/%d") | |
wdaynames = ['日','月','火','水','木','金','土']; | |
wday = wdaynames[Date.today.wday]; | |
msg = "SNS関連サポート部屋 『#{today}(#{wday})のSNSマスターは[#{ order[index] }]さんです』"; | |
end | |
end | |
class AppController < OSX::NSObject | |
end | |
if ARGV.length < 2 then | |
puts "Usage: #{$0} MISAccount password" | |
exit 1 | |
end | |
sm = SkypeAPIClient.alloc.init | |
OSX::SkypeAPI.setSkypeDelegate(sm) | |
OSX::SkypeAPI.connect | |
sm.misuser = ARGV[0].to_s | |
sm.passwd = ARGV[1].to_s | |
c = AppController.new | |
OSX::NSRunLoop.currentRunLoop.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment