Refactor modules that used nemubot XML parser due to previous commit

This commit is contained in:
nemunaire 2015-10-25 18:50:18 +01:00
parent 2b96c32063
commit 59ea2e971b
4 changed files with 108 additions and 94 deletions

View file

@ -1,7 +1,7 @@
# coding=utf-8
"""Gets information about velib stations"""
# PYTHON STUFFS #######################################################
import re
from nemubot import context
@ -9,11 +9,11 @@ from nemubot.exception import IRCException
from nemubot.hooks import hook
from nemubot.tools import web
nemubotversion = 4.0
from more import Response
# LOADING #############################################################
URL_API = None # http://www.velib.paris.fr/service/stationdetails/paris/%s
def load(context):
@ -29,25 +29,14 @@ def load(context):
# context.add_event(evt)
def help_full():
return ("!velib /number/ ...: gives available bikes and slots at "
"the station /number/.")
# MODULE CORE #########################################################
def station_status(station):
"""Gets available and free status of a given station"""
response = web.getXML(URL_API % station)
if response is not None:
available = response.getNode("available").getContent()
if available is not None and len(available) > 0:
available = int(available)
else:
available = 0
free = response.getNode("free").getContent()
if free is not None and len(free) > 0:
free = int(free)
else:
free = 0
available = int(response.getElementsByTagName("available")[0].firstChild.nodeValue)
free = int(response.getElementsByTagName("free")[0].firstChild.nodeValue)
return (available, free)
else:
return (None, None)
@ -69,27 +58,30 @@ 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"
return Response("À la station %s : %d vélib et %d points d'attache"
" disponibles." % (station, available, free),
channel=msg.channel, nick=msg.nick)
channel=msg.channel)
raise IRCException("station %s inconnue." % station)
@hook("cmd_hook", "velib")
# MODULE INTERFACE ####################################################
@hook("cmd_hook", "velib",
help="gives available bikes and slots at the given station",
help_usage={
"STATION_ID": "gives available bikes and slots at the station STATION_ID"
})
def ask_stations(msg):
"""Hook entry from !velib"""
if len(msg.args) > 4:
raise IRCException("demande-moi moins de stations à la fois.")
elif len(msg.args):
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:
raise IRCException("numéro de station invalide.")
else:
elif not len(msg.args):
raise IRCException("pour quelle station ?")
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:
raise IRCException("numéro de station invalide.")