First module ready for v3.2: velib

This commit is contained in:
Némunaire 2012-08-14 06:57:19 +02:00
parent 69f375e9f1
commit 3492cf302f
2 changed files with 49 additions and 24 deletions

View File

@ -4,14 +4,20 @@ import http.client
import re import re
from xml.dom.minidom import parseString from xml.dom.minidom import parseString
from module_state import ModuleState from event import ModuleEvent
from xmlparser.node import ModuleState
nemubotversion = 3.0 nemubotversion = 3.2
def load(): def load(context):
global DATAS global DATAS
DATAS.setIndex("name", "station") DATAS.setIndex("name", "station")
evt = ModuleEvent(station_available, "42706",
(lambda a, b: a != b), None, 60,
station_status)
context.events.append(evt)
evt.run()
def help_tiny (): def help_tiny ():
"""Line inserted in the response to the command !help""" """Line inserted in the response to the command !help"""
@ -35,8 +41,8 @@ def getPage (s, p):
conn.close() conn.close()
return (res.status, data) return (res.status, data)
def station_status(station):
def station_status(msg, station): """Gets available and free status of a given station"""
(st, page) = getPage(CONF.getNode("server")["ip"], CONF.getNode("server")["url"] + station) (st, page) = getPage(CONF.getNode("server")["ip"], CONF.getNode("server")["url"] + station)
if st == http.client.OK: if st == http.client.OK:
response = parseString(page) response = parseString(page)
@ -50,25 +56,43 @@ def station_status(msg, station):
free = int(free[0].childNodes[0].nodeValue) free = int(free[0].childNodes[0].nodeValue)
else: else:
free = 0 free = 0
msg.send_chn("%s: à la station %s : %d vélib et %d points d'attache disponibles." % (msg.nick, station, available, free)) return (available, free)
else: else:
msg.send_chn("%s: station %s inconnue." % (msg.nick, station)) return (None, None)
def parseanswer(msg): def station_available(station):
global DATAS """Gets available velib at a given velib station"""
if msg.cmd[0] == "velib": (a, f) = station_status(station)
if len(msg.cmd) > 5: return a
msg.send_chn("%s: Demande-moi moins de stations à la fois." % msg.nick)
elif len(msg.cmd) > 1: def station_free(station):
for station in msg.cmd[1:]: """Gets free slots at a given velib station"""
if re.match("^[0-9]{4,5}$", station): (a, f) = station_status(station)
station_status(msg, station) return f
elif station in DATAS.index:
station_status(msg, DATAS.index[station]["number"])
else: def print_station_status(msg, station):
msg.send_chn("%s: numéro de station invalide." % (msg.nick)) """Send message with information about the given station"""
(available, free) = station_status(station)
if available is not None and free is not None:
msg.send_chn("%s: à la station %s : %d vélib et %d points d'attache disponibles." % (msg.nick, station, available, free))
else: else:
msg.send_chn("%s: Pour quelle station ?" % msg.nick) msg.send_chn("%s: station %s inconnue." % (msg.nick, station))
return True
else: def ask_stations(data, msg):
"""Hook entry from !velib"""
global DATAS
if len(msg.cmd) > 5:
msg.send_chn("%s: Demande-moi moins de stations à la fois." % msg.nick)
elif len(msg.cmd) > 1:
for station in msg.cmd[1:]:
if re.match("^[0-9]{4,5}$", station):
print_station_status(msg, station)
elif station in DATAS.index:
print_station_status(msg, DATAS.index[station]["number"])
else:
msg.send_chn("%s: numéro de station invalide." % (msg.nick))
return True
else:
msg.send_chn("%s: Pour quelle station ?" % msg.nick)
return False return False

View File

@ -1,4 +1,5 @@
<?xml version="1.0" ?> <?xml version="1.0" ?>
<nemubotmodule name="velib"> <nemubotmodule name="velib">
<server ip="www.velib.paris.fr" url="/service/stationdetails/paris/" /> <server ip="www.velib.paris.fr" url="/service/stationdetails/paris/" />
</nemubotmodule> <message type="cmd" name="velib" call="ask_stations" />
</nemubotmodule>