Skip to content

Instantly share code, notes, and snippets.

@yosida95
Created June 10, 2012 07:55
Show Gist options
  • Save yosida95/2904429 to your computer and use it in GitHub Desktop.
Save yosida95/2904429 to your computer and use it in GitHub Desktop.
#-*- coding: utf-8 -*-
import Skype4Py
import functools
import time
class SkypeBot(object):
def __init__(self):
self.skype = Skype4Py.Skype(Transport='x11')
self.skype.Attach()
self.skype.OnMessageStatus = self.status_handler
self.processors = {}
def status_handler(self, msg, event):
if event == Skype4Py.enums.cmsReceived:
for line in msg.Body.split('\n'):
body = line.split() + ['']
cmd, args = body[0].lower(), body[1:-1]
processor = self.processors.get(cmd, lambda args: None)
self.send_message(processor(args), msg.Chat)
def send_message(self, body, chat):
if body:
chat.SendMessage(body)
def add_processor(self, cmd):
def receive_processor(processor):
@functools.wraps(processor)
def wrapper(*args, **kwargs):
return processor(*args, **kwargs)
self.processors[cmd] = wrapper
return wrapper
return receive_processor
def run(self):
while True:
time.sleep(1)
#-*- coding: utf-8 -*-
from skypebot import SkypeBot
import urlparse
import urllib
bot = SkypeBot()
@bot.add_processor(u'ping')
def process_ping(args):
return u'pong'
@bot.add_processor(u'expand')
def process_expand(args):
if not len(args) == 1:
return
url = args[0]
parsed_url = urlparse.urlparse(args[0])
if parsed_url.netloc == 'ux.nu':
query = urllib.urlencode(
[('id', parsed_url.path[1:]), ('format', 'plain')])
request = urlparse.urlunparse(
['http', 'ux.nu', '/api/expand', '', query, ''])
else:
query = urllib.urlencode([('url', url), ('format', 'plain')])
request = urlparse.urlunparse(
['http', 'ux.nu', '/hugeurl', '', query, ''])
result = urllib.urlopen(request)
expanded = result.read()
if result.code == 200 and expanded:
return expanded
@bot.add_processor('shorten')
def process_shorten(args):
if not len(args) == 1:
return
url = args[0]
query = urllib.urlencode([('url', url), ('format', 'plain')])
request = urlparse.urlunparse(
['http', 'ux.nu', '/api/short', '', query, ''])
result = urllib.urlopen(request)
shortened = result.read()
if result.code == 200 and shortened:
return shortened
if __name__ == '__main__':
bot.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment