2012-06-16 20:50:47 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-08-27 23:39:31 +00:00
|
|
|
"""Gets information about velib stations"""
|
|
|
|
|
2012-06-16 20:50:47 +00:00
|
|
|
import re
|
|
|
|
|
2014-09-07 21:52:54 +00:00
|
|
|
from hooks import hook
|
2012-11-04 15:26:20 +00:00
|
|
|
from tools import web
|
2012-06-16 20:50:47 +00:00
|
|
|
|
2014-08-13 13:53:55 +00:00
|
|
|
nemubotversion = 3.4
|
2012-06-16 20:50:47 +00:00
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
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")
|
2012-06-16 20:50:47 +00:00
|
|
|
|
2012-08-23 16:20:45 +00:00
|
|
|
# evt = ModuleEvent(station_available, "42706",
|
|
|
|
# (lambda a, b: a != b), None, 60,
|
|
|
|
# station_status)
|
|
|
|
# context.add_event(evt)
|
2012-06-16 20:50:47 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
|
|
|
def help_full():
|
|
|
|
return ("!velib /number/ ...: gives available bikes and slots at "
|
|
|
|
"the station /number/.")
|
2012-06-16 20:50:47 +00:00
|
|
|
|
|
|
|
|
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)
|
2012-06-16 20:50:47 +00:00
|
|
|
else:
|
2012-11-04 03:39:54 +00:00
|
|
|
return (None, None)
|
2012-06-16 20:50:47 +00:00
|
|
|
|
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:
|
2014-09-18 05:57:06 +00:00
|
|
|
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
|
|
|
|
2014-09-07 21:52:54 +00:00
|
|
|
@hook("cmd_hook", "velib")
|
2012-08-22 19:05:33 +00:00
|
|
|
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):
|
2012-08-30 23:54:57 +00:00
|
|
|
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.")
|
|
|
|
|
2012-06-16 20:50:47 +00:00
|
|
|
else:
|
2014-07-25 16:02:30 +00:00
|
|
|
raise IRCException("pour quelle station ?")
|