Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit 7ec74724e209d01c06ddaaabc05226dec298cf4e @davidmarek committed Sep 17, 2009

Large diffs are not rendered by default.

Oops, something went wrong.
0 README
No changes.
No changes.
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import xmpp, pluginLoader, contact
+
+class JabberBotException(Exception):
+ pass
+
+class JabberBot:
+
+ def __init__(self, jid, password, plugin_path):
+ self.__establish_connection(jid, password)
+
+ self.client.RegisterHandler('message', self.__message_handler)
+ self.client.RegisterHandler('presence', self.__presence_handler)
+ self.client.RegisterHandler('iq', self.__iq_handler)
+
+ self.plugins = pluginLoader.PluginLoader(plugin_path)
+ self.client.sendInitPresence()
+
+ def __establish_connection(self, jid, password):
+ sp = jid.split('@')
+ if len(sp) != 2:
+ raise JabberBotException('JID should be in format name@server.')
+
+ self.client = xmpp.Client(sp[1], debug=[])
+ conn = self.client.connect()
+ if not conn:
+ raise JabberBotException("Can't connect.")
+
+ auth = self.client.auth(sp[0],password,'jabber_bot')
+ if not auth:
+ raise JabberBotException("Can't authenticate.")
+
+
+ def __message_handler(self, conn, msg):
+ txt = msg.getBody()
+ if txt:
+ split_txt = txt.strip().split(' ',1)
+ try:
+ plugin = self.plugins.get_plugin(split_txt[0])
+ plugin.run(contact.Contact(conn, msg.getFrom()), split_txt[1])
+ except KeyError:
+ plugin = self.plugins.get_plugin('help')
+ plugin.run(contact.Contact(conn, msg.getFrom()), '')
+ except IndexError:
+ plugin.run(contact.Contact(conn, msg.getFrom()), '')
+
+ def __presence_handler(self, conn, node):
+ if node.getType() == 'subscribe':
+ self.client.getRoster().Authorize(node.getFrom())
+
+ def __iq_handler(self, conn, node):
+ pass
+
+ def loop(self,timeout=1):
+ try:
+ while True:
+ self.client.Process(timeout)
+ except KeyboardInterrupt:
+ self.plugins.exit()
+
+if __name__ == '__main__':
+ a = JabberBot('pokusny_kralicek@jabbim.cz', 'heslo', 'plugins')
+ a.loop()
+
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import xmpp
+
+class Contact:
+
+ def __init__(self, conn, jid):
+ self.conn = conn
+ self.jid = jid
+
+ def get_jid(self):
+ return self.jid
+
+ def send(self, msg):
+ self.conn.send(xmpp.Message(self.jid, msg, 'chat'))
+
@@ -0,0 +1,27 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+class Helper:
+ """Shows help"""
+
+ def __init__(self, plugins):
+ self.plugins = plugins
+
+ def run(self, contact, message):
+ msg = 'List of keywords: help '
+ if message:
+ try:
+ msg = '%s - %s' % (message, self.plugins[message].__doc__)
+ except KeyError:
+ if message == 'help':
+ msg = 'help - Shows help'
+ else:
+ msg = '%s is not a keyword' % message
+ else:
+ for (key, object) in self.plugins.iteritems():
+ msg += key + ' '
+ contact.send(msg)
+
+ def exit(self):
+ pass
+
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import os, os.path, glob, helper, sys, imp
+
+class PluginLoaderException(Exception):
+ pass
+
+class PluginLoader:
+
+ def __init__(self, path):
+ self.change_path(path)
+
+ def __load_plugins(self):
+ for plugin_filename in glob.glob(os.path.sep.join([self.path,'*.py'])):
+ try:
+ plugin = imp.load_source(os.path.split(plugin_filename)[1][:-3], plugin_filename)
+ #plugin = __import__(plugin_filename[:-3]) # Get rid of .py
+ for keyword in plugin.provides.keys():
+ if keyword in self.commands.keys():
+ raise PluginLoaderException('Keyword already used.')
+ self.commands[keyword] = plugin.provides[keyword]()
+ except ImportError, err:
+ print err
+ raise PluginLoaderException('Cannot import %s.' % plugin_filename)
+ except AttributeError:
+ pass
+ #raise PluginLoaderException('File %s is not correct plugin.' % plugin_filename)
+ except NameError:
+ pass
+ #raise PluginLoaderException('File %s is not correct plugin.' % plugin_filename)
+
+ def load(self):
+ self.commands = {}
+ self.__load_plugins()
+
+ def change_path(self, path):
+ self.path = path
+ self.load()
+
+ def get_plugin(self, keyword):
+ if keyword == 'help':
+ return helper.Helper(self.commands)
+ return self.commands[keyword]
+
+ def exit(self):
+ for (key, obj) in self.commands.iteritems():
+ obj.exit()
39 jabbot
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import jabber_bot.client, getopt, sys, os.path
+
+def usage(name):
+ print "usage: %s -j jid -p password [-d plugins_directory]" % name
+
+if __name__ == '__main__':
+ try:
+ optlist, args = getopt.gnu_getopt(sys.argv[1:], "j:p:d:", ["jid=","password=","plugins="])
+ except getopt.GetoptError, err:
+ print str(err)
+ usage(sys.argv[0])
+ sys.exit(2)
+
+ jid=None
+ password=None
+ plugins='plugins'
+ for opt, arg in optlist:
+ if opt in ('-j', '--jid'):
+ jid = arg
+ elif opt in ('-p', '--password'):
+ password = arg
+ elif opt in ('-d', '--plugins'):
+ plugins = arg
+ else:
+ assert False, "unhandled option"
+
+ if not (jid and password):
+ usage(sys.argv[0])
+ sys.exit(2)
+
+ plugins = os.path.abspath(plugins)
+
+ bot = jabber_bot.client.JabberBot(jid, password, plugins)
+ bot.loop()
+
+
@@ -0,0 +1,17 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+class EchoPlugin:
+ """Echoes message"""
+
+ def __init__(self):
+ print "echo init"
+
+ def run(self, contact, msg):
+ contact.send(msg)
+
+ def exit(self):
+ print "echo exit"
+
+provides = {'echo': EchoPlugin}
+
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import xml.etree.ElementTree as et
+import urllib
+
+class FMyLife:
+ """Show random message from http://fmylife.com"""
+
+ def __init__(self):
+ print 'fmylife init'
+
+ def run(self, contact, msg):
+ rand = urllib.urlopen('http://api.betacie.com/view/random?language=en&key=readonly')
+ tree = et.fromstring(rand.read())
+ message = tree.find('items/item/text').text
+ contact.send(message)
+
+ def exit(self):
+ pass
+
+provides = {'fml' : FMyLife}
+
@@ -0,0 +1,137 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import urllib, urllib2, re, BeautifulSoup
+
+class IDOS:
+ u"""Zjišťuje informace o dopravních spojeních.
+ Formát: idos from odkud to kam [at čas [datum]]"""
+
+ def __init__(self):
+ print "idos init"
+ self.regex = re.compile("from (.+) to (.+?)(?: at (\d\d:\d\d)( \d{1,2}.\d{1,2}.\d{4})?)?$")
+
+ def run(self, contact, msg):
+ msg = msg.encode('utf-8')
+ found = self.regex.findall(msg)
+ if found:
+ connection = self.find_connection(found[0])
+ contact.send(connection)
+ else:
+ contact.send("I don't understand, read help.")
+
+ def find_connection(self, args):
+ opener = urllib2.build_opener()
+ opener.addheaders = [('User-agent', \
+ 'Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)'), \
+ ('Referer', 'http://ttwap.chaps.cz')]
+ url = "http://ttwap.chaps.cz/conn.aspx?%s&jr=C&OP=&Lang=C&Format=3&cmd=search"
+
+ data = urllib.urlencode([('odkud',args[0]),('kam',args[1]),
+ ('cas',args[2]),('datum',args[3])])
+ page = opener.open(url % data)
+
+ soup = BeautifulSoup.BeautifulSoup(page.read())
+
+ return self.format_output(soup)
+
+ def convert(self, str):
+ return unicode(BeautifulSoup.BeautifulStoneSoup(str,
+ convertEntities=BeautifulSoup.BeautifulStoneSoup.HTML_ENTITIES))
+
+ def format_output(self, soup):
+ response = soup.findAll('p')
+ caption = response[0].firstText().string
+ caption = self.convert(caption)
+
+ return_string = caption
+ if u'Upřesnit zadání' in caption:
+ for s in soup.findAll('select'):
+ return_string += u'\nSpecifikujte blíže, které místo jste měli na mysli:\n'
+ for o in s.findAll('option'):
+ return_string += self.convert(o.string) + '\n'
+
+ else:
+ informations = response[1].contents
+ while len(informations) > 7:
+ odkud = self.convert(informations[0].string)
+ cas_odjezdu = self.convert(informations[1].string)
+ kam = self.convert(informations[3].string)
+ cas_prijezdu = self.convert(informations[4].string)
+ spoj = self.convert(informations[6].string)
+ return_string += "\nFrom: %s %s\nTo: %s %s\n%s\n" % \
+ (odkud, cas_odjezdu, kam, cas_prijezdu, spoj)
+ informations = informations[8:]
+
+ return_string += "\n" + self.convert(informations[0].string)
+
+ return return_string
+
+ def exit(self):
+ pass
+
+class MHD(IDOS):
+ u"""Hledá spojení v MHD 76 českých měst.
+ Formát: mhd město from odkud to kam [at čas [datum]] | mhd list"""
+
+ cities = {u'Turnov': u'Turnov', u'Trutnov': u'TUCz',
+ u'\u010cesk\xe9 Bud\u011bjovice': u'CBCz', u'Jihlava': u'JICz',
+ u'T\u0159inec': u'TRICz', u'Znojmo': u'ZNCz', u'Zl\xedn': u'ZLCz',
+ u'Chrudim': u'CRCz', u'Fr\xfddek-M\xedstek': u'FMCz',
+ u'P\u0159\xedbram': u'PBCz', u'\u017d\u010f\xe1r nad S\xe1zavou':
+ u'ZRCz', u'Velk\xe9 Mezi\u0159\xed\u010d\xed': u'VELMCz',
+ u'Hradec Kr\xe1lov\xe9': u'HKCz', u'Klatovy': u'KTCz',
+ u'Beroun': u'BECz', u'Orlov\xe1': u'ORCz', u'Kutn\xe1 Hora': u'KHCz',
+ u'P\u0159e\u0161tice': u'PRECz', u'Vset\xedn': u'VSCz', u'Karlovy Vary': u'KVCz', u'T\xe1bor': u'TACz', u'Doma\u017elice': u'DOCz',
+ u'Havl\xed\u010dk\u016fv Brod': u'HBCz', u'Mlad\xe1 Boleslav':
+ u'MBCz', u'M\u011bln\xedk': u'MECz', u'D\u011b\u010d\xedn':
+ u'DCCz', u'P\u0159erov': u'PRCz', u'Poli\u010dka': u'POLCz',
+ u'Pelh\u0159imov': u'PECz', u'Hranice': u'HRACz', u'\xdast\xed nad Labem': u'ULCz', u'Brno': u'IDSJMK', u'Mikulov': u'MIKCz',
+ u'Olomouc': u'OLCz', u'Plze\u0148': u'PMCz', u'Chomutov': u'CVCz',
+ u'Jind\u0159ich\u016fv Hradec': u'JHCz', u'Bene\u0161ov': u'BNCz',
+ u'Pardubice': u'PACz', u'Dv\u016fr Kr\xe1lov\xe9 nad Labem':
+ u'DKCz', u'\u010cesk\xfd T\u011b\u0161\xedn': u'CTECz',
+ u'\u010c\xe1slav': u'Caslav', u'Kladno': u'KDCz', u'Hodon\xedn':
+ u'HOCz', u'Jablonec nad Nisou': u'JNCz', u'J\xe1chymov': u'JachCz',
+ u'Teplice': u'TPCz', u'Kol\xedn': u'KOCz', u'Blansko': u'BKCz',
+ u'\u010cesk\xe1 L\xedpa': u'CLCz', u'Krom\u011b\u0159\xed\u017e':
+ u'KROMCz', u'Prost\u011bjov': u'PVCz', u'Vla\u0161im': u'VlasimCz',
+ u'A\u0161': u'ASCz', u'Ostrava': u'OVCz', u'Vala\u0161sk\xe9 Mezi\u0159\xed\u010d\xed': u'VACz', u'B\u0159eclav': u'BVCz',
+ u'Nymburk': u'NBCz', u'Z\xe1b\u0159eh': u'ZACz', u'Vy\u0161kov':
+ u'VYCz', u'Liberec': u'LICz', u'Kyjov': u'KYJCz', u'Tachov':
+ u'TCCz', u'Most a Litv\xednov': u'MOCz', u'Brunt\xe1l': u'BRCz',
+ u'Sokolov': u'SOCz', u'Strakonice': u'STCz', u'Hav\xed\u0159ov':
+ u'HAVCz', u'Krnov': u'KRNCz', u'Praha': u'PID', u'Cheb': u'CHCz',
+ u'P\xedsek': u'PICz', u'\u0160umperk': u'SUCz', u'Karvin\xe1':
+ u'KACz', u'St\u0159\xedbro': u'SBCz', u'Stud\xe9nka': u'SDCz'}
+
+ def __init__(self):
+ print "mhd init"
+ self.regex = re.compile("(?:(.+) from (.+) to (.+?)(?: at (\d\d:\d\d)( \d{1,2}.\d{1,2}.\d{4})?)?$)|(list)")
+
+ def find_connection(self, args):
+ opener = urllib2.build_opener()
+ opener.addheaders = [('User-agent', \
+ 'Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)'), \
+ ('Referer', 'http://ttwap.chaps.cz')]
+ url = 'http://ttwap.chaps.cz/ConnMHD.aspx?%s&linka=&OP=&Lang=C&Format=3&cmd=search'
+
+ if not self.convert(args[0]) in self.cities.keys():
+ if args[5] == 'list':
+ c = self.cities.keys()
+ c.sort()
+ return ", ".join(c)
+ return "Město nenalezeno"
+
+ city = self.cities[self.convert(args[0])]
+ data = urllib.urlencode([('JR',city),('odkud',args[1]),
+ ('kam',args[2]),('cas',args[3]),('datum',args[4])])
+ page = opener.open(url % data)
+
+ soup = BeautifulSoup.BeautifulSoup(page.read())
+ print soup.prettify()
+
+ return self.format_output(soup)
+
+provides = {'idos' : IDOS, 'mhd' : MHD}
+
Oops, something went wrong.

0 comments on commit 7ec7472

Please sign in to comment.