From 32b3b04f8808efd2c642d244fad10094184416d2 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 17 Jul 2012 19:25:46 +0200 Subject: tracking: Added the url command. --- modules/tracking.py | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/modules/tracking.py b/modules/tracking.py index daad00f..aa3c309 100644 --- a/modules/tracking.py +++ b/modules/tracking.py @@ -91,8 +91,14 @@ class PostenModule(TrackingModule): u.close() return xml + def get_url(self, code = None): + url = 'http://sporing.posten.no/' + if code: + url += 'sporing.html?q=' + code + return url + def track(self, code): - xml = self.get_xml(self. url % code) + xml = self.get_xml(self.url % code) if not xml: return @@ -132,8 +138,6 @@ def get_tracking_module(arg): return tracking_modules[type]() class Module: - url = 'http://sporing.posten.no/sporing.xml?q=%s' - def __init__(self, bot): self.setup_db() self.irc = bot @@ -301,8 +305,29 @@ class Module: session.close() return msg + def track_url(self, code, nick, channel): + if not code: + m = get_tracking_module('') + return [m.get_url()] + msg = [] + try: + session = Session() + consignments = session.query(Consignment).filter(or_(Consignment.code.like(code + '%'), Consignment.label.like(code+ '%'))) + if nick: + consignments = consignments.filter(Consignment.nick == nick) + results = [] + for consignment in consignments: + for package in consignment.packages: + m = get_tracking_module(package.code) + msg.append(m.get_url(package.code)) + if not len(msg): + msg.append('No packages found.') + finally: + session.close() + return msg + def keyword(self, nick, channel, kw, msg): - usage = 'Usage: !track (start|label TRACKINGNO [LABEL])|(stop|status TRACKINGNO|LABEL)|list' + usage = 'Usage: !track (start|label TRACKINGNO [LABEL])|(stop|status TRACKINGNO|LABEL)|list|(url TRACKINGNO|LABEL)' args = msg.split() if len(args) < 1: self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], usage) @@ -324,6 +349,8 @@ class Module: return code = args[1] label = ' '.join(args[2:]) + elif mode.lower() == 'url': + code = ' '.join(args[1:]) if len(args) > 1 else None msg = None if mode.lower() in ('start', 'add'): @@ -336,6 +363,8 @@ class Module: msg = self.track_list(nick.split('!')[0]) elif mode.lower() == 'label': msg = self.track_label(code, label, nick.split('!')[0], channel if not channel == self.irc.nickname else None) + elif mode.lower() == 'url': + msg = self.track_url(code, nick.split('!')[0], channel if not channel == self.irc.nickname else None) else: msg = 'Invalid mode "%s".' % mode -- cgit v1.2.3