nemubot/modules/velib.py

91 lines
2.6 KiB
Python
Raw Normal View History

# coding=utf-8
2014-08-27 23:39:31 +00:00
"""Gets information about velib stations"""
import re
from nemubot.exception import IRCException
from nemubot.hooks import hook
from nemubot.tools import web
2014-08-13 13:53:55 +00:00
nemubotversion = 3.4
from more import Response
2014-11-13 01:51:49 +00:00
2012-08-14 04:57:19 +00:00
def load(context):
2012-11-04 03:39:54 +00:00
global DATAS
DATAS.setIndex("name", "station")
# evt = ModuleEvent(station_available, "42706",
# (lambda a, b: a != b), None, 60,
# station_status)
# context.add_event(evt)
2014-11-13 01:51:49 +00:00
def help_full():
return ("!velib /number/ ...: gives available bikes and slots at "
"the station /number/.")
2012-08-14 04:57:19 +00:00
def station_status(station):
2012-11-04 03:39:54 +00:00
"""Gets available and free status of a given station"""
2012-11-04 15:26:20 +00:00
response = web.getXML(CONF.getNode("server")["url"] + station)
if response is not None:
available = response.getNode("available").getContent()
if available is not None and len(available) > 0:
available = int(available)
2012-11-04 03:39:54 +00:00
else:
available = 0
2012-11-04 15:26:20 +00:00
free = response.getNode("free").getContent()
if free is not None and len(free) > 0:
free = int(free)
2012-11-04 03:39:54 +00:00
else:
free = 0
return (available, free)
else:
2012-11-04 03:39:54 +00:00
return (None, None)
2014-11-13 01:51:49 +00:00
2012-08-14 04:57:19 +00:00
def station_available(station):
"""Gets available velib at a given velib station"""
(a, f) = station_status(station)
return a
2014-11-13 01:51:49 +00:00
2012-08-14 04:57:19 +00:00
def station_free(station):
"""Gets free slots at a given velib station"""
(a, f) = station_status(station)
return f
def print_station_status(msg, station):
"""Send message with information about the given station"""
(available, free) = station_status(station)
if available is not None and free is not None:
return Response("à la station %s : %d vélib et %d points d'attache"
2014-07-25 16:02:30 +00:00
" disponibles." % (station, available, free),
channel=msg.channel, nick=msg.nick)
raise IRCException("station %s inconnue." % station)
2012-08-14 04:57:19 +00:00
2014-11-13 01:51:49 +00:00
@hook("cmd_hook", "velib")
def ask_stations(msg):
2012-08-14 04:57:19 +00:00
"""Hook entry from !velib"""
global DATAS
2012-11-04 03:39:54 +00:00
if len(msg.cmds) > 5:
2014-07-25 16:02:30 +00:00
raise IRCException("demande-moi moins de stations à la fois.")
2012-11-04 03:39:54 +00:00
elif len(msg.cmds) > 1:
for station in msg.cmds[1:]:
2012-08-14 04:57:19 +00:00
if re.match("^[0-9]{4,5}$", station):
return print_station_status(msg, station)
2012-08-14 04:57:19 +00:00
elif station in DATAS.index:
2014-11-13 01:51:49 +00:00
return print_station_status(msg,
DATAS.index[station]["number"])
2012-08-14 04:57:19 +00:00
else:
2014-07-25 16:02:30 +00:00
raise IRCException("numéro de station invalide.")
else:
2014-07-25 16:02:30 +00:00
raise IRCException("pour quelle station ?")