2014-08-27 23:39:31 +00:00
|
|
|
"""Gets information about velib stations"""
|
|
|
|
|
2015-10-25 17:50:18 +00:00
|
|
|
# PYTHON STUFFS #######################################################
|
|
|
|
|
2012-06-16 20:50:47 +00:00
|
|
|
import re
|
|
|
|
|
2015-02-11 17:12:39 +00:00
|
|
|
from nemubot import context
|
2015-10-30 20:57:45 +00:00
|
|
|
from nemubot.exception import IMException
|
2015-01-03 19:34:44 +00:00
|
|
|
from nemubot.hooks import hook
|
|
|
|
from nemubot.tools import web
|
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
|
|
|
|
2015-10-25 17:50:18 +00:00
|
|
|
# LOADING #############################################################
|
|
|
|
|
2015-06-03 20:07:06 +00:00
|
|
|
URL_API = None # http://www.velib.paris.fr/service/stationdetails/paris/%s
|
|
|
|
|
2012-08-14 04:57:19 +00:00
|
|
|
def load(context):
|
2015-06-03 20:07:06 +00:00
|
|
|
global URL_API
|
2015-10-27 17:03:28 +00:00
|
|
|
if not context.config or "url" not in context.config:
|
2015-06-03 20:07:06 +00:00
|
|
|
raise ImportError("Please provide url attribute in the module configuration")
|
|
|
|
URL_API = context.config["url"]
|
2015-02-11 17:12:39 +00:00
|
|
|
context.data.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
|
|
|
|
2015-10-25 17:50:18 +00:00
|
|
|
# MODULE CORE #########################################################
|
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"""
|
2015-06-03 20:07:06 +00:00
|
|
|
response = web.getXML(URL_API % station)
|
2012-11-04 15:26:20 +00:00
|
|
|
if response is not None:
|
2015-10-25 17:50:18 +00:00
|
|
|
available = int(response.getElementsByTagName("available")[0].firstChild.nodeValue)
|
|
|
|
free = int(response.getElementsByTagName("free")[0].firstChild.nodeValue)
|
2012-11-04 03:39:54 +00:00
|
|
|
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:
|
2015-10-25 17:50:18 +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),
|
2015-10-25 17:50:18 +00:00
|
|
|
channel=msg.channel)
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("station %s inconnue." % station)
|
2012-08-14 04:57:19 +00:00
|
|
|
|
2014-11-13 01:51:49 +00:00
|
|
|
|
2015-10-25 17:50:18 +00:00
|
|
|
# MODULE INTERFACE ####################################################
|
|
|
|
|
2015-11-02 19:19:12 +00:00
|
|
|
@hook.command("velib",
|
2015-10-25 17:50:18 +00:00
|
|
|
help="gives available bikes and slots at the given station",
|
|
|
|
help_usage={
|
|
|
|
"STATION_ID": "gives available bikes and slots at the station STATION_ID"
|
|
|
|
})
|
2012-08-22 19:05:33 +00:00
|
|
|
def ask_stations(msg):
|
2015-07-10 21:09:54 +00:00
|
|
|
if len(msg.args) > 4:
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("demande-moi moins de stations à la fois.")
|
2015-10-25 17:50:18 +00:00
|
|
|
elif not len(msg.args):
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("pour quelle station ?")
|
2015-10-25 17:50:18 +00:00
|
|
|
|
|
|
|
for station in msg.args:
|
|
|
|
if re.match("^[0-9]{4,5}$", station):
|
|
|
|
return print_station_status(msg, station)
|
|
|
|
elif station in context.data.index:
|
|
|
|
return print_station_status(msg,
|
|
|
|
context.data.index[station]["number"])
|
|
|
|
else:
|
2015-10-30 20:57:45 +00:00
|
|
|
raise IMException("numéro de station invalide.")
|