Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created March 15, 2010 05:16
Show Gist options
  • Save mashiro/332549 to your computer and use it in GitHub Desktop.
Save mashiro/332549 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import re
from System.Text import Encoding
from System.IO import StringReader
from System.Net import WebClient, WebException
from Misuzilla.Applications.TwitterIrcGateway import NilClasses, Status, Statuses, User, Users
from Misuzilla.Applications.TwitterIrcGateway.AddIns import TypableMapSupport
from Misuzilla.Applications.TwitterIrcGateway.AddIns.DLRIntegration import DLRIntegrationAddIn
class TypableMap(object):
@classmethod
def instance(cls):
if not hasattr(cls, '__instance__'):
cls.__instance__ = TypableMap()
return cls.__instance__
def __init__(self):
CurrentSession.AddInManager.GetAddIn[DLRIntegrationAddIn]().BeforeUnload += self.on_before_unload
self.typablemap_commands = CurrentSession.AddInManager.GetAddIn[TypableMapSupport]().TypableMapCommands
self.registered_commands = []
self.register_methods()
def on_before_unload(self, sender, e):
for command in self.registered_commands:
self.typablemap_commands.RemoveCommand(command)
self.registered_commands = []
def register(self, command, desc, proc):
self.registered_commands.append(command)
self.typablemap_commands.AddCommand(command, desc, proc)
@classmethod
def send_notice(cls, receiver, nick, content):
CurrentSession.SendChannelMessage(receiver, nick, content, True, False, False, True)
@classmethod
def post(cls, url, data=''):
return CurrentSession.TwitterService.POST(url, Encoding.UTF8.GetBytes(data))
@classmethod
def get(cls, url):
return CurrentSession.TwitterService.GET(url)
@classmethod
def deserialize(cls, type, xml):
if NilClasses.CanDeserialize(xml):
return None
else:
return type.Serializer.Deserialize(StringReader(xml))
def register_methods(self):
def retweet(p, msg, status, args):
def f():
retweeted = self.deserialize(Status, self.post('/statuses/retweet/%s.xml' % status.Id))
self.send_notice(msg.Receiver, CurrentSession.CurrentNick, 'ユーザ %s のステータス "%s" を Retweet しました。' % (status.User.ScreenName, retweeted.Text))
p.Session.RunCheck(f)
return True
def conversation(p, msg, status, args):
def f():
empty = False
try:
body = WebClient().DownloadString('http://search.twitter.com/search/thread/%s' % status.Id)
divs = re.findall(r'<div class="msg">(.*?)</div>', body, re.S)
if len(divs) > 0:
for div in divs:
match = re.search(r'<a[^>]*>(.*?)</a>.*<span[^>]*>(.*?)</span>', div)
name = match.group(1)
text = re.sub(r'<[^>]*>', '', match.group(2))
self.send_notice(msg.Receiver, name, text)
else:
empty = True
except:
# 404とか
empty = True
finally:
if empty:
self.send_notice(msg.Receiver, CurrentSession.CurrentNick, '会話が存在しません。')
p.Session.RunCheck(f)
return True
# register typablemap commands
self.register('rt', 'Retweet Command', retweet)
self.register('cv', 'Conversation Command', conversation)
# instanciate
typablemap = TypableMap.instance()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment