From d227826aef47c1ce8917a5139573c1063c540915 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 12 Oct 2010 17:25:44 +0200 Subject: Added traffic module. --- modules/traffic.py | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 modules/traffic.py diff --git a/modules/traffic.py b/modules/traffic.py new file mode 100644 index 0000000..b315660 --- /dev/null +++ b/modules/traffic.py @@ -0,0 +1,110 @@ +# -*- coding: utf8 -*- + +info = { + 'author': 'Jon Bergli Heier', + 'title': 'Traffic data', + 'description': 'Traffic data from Statens Vegvesen', +} + +import urllib2, urllib +from lxml import etree + +counties = { + 'Akershus': 2, + 'Aust-Agder': 9, + 'Buskerud': 6, + 'Finnmark': 20, + 'Hedmark': 4, + 'Hordaland': 12, + u'Møre og Romsdal': 15, + u'Nord-Trøndelag': 17, + 'Nordland': 18, + 'Oppland': 5, + 'Oslo': 3, + 'Rogaland': 11, + 'Sogn og Fjordane': 14, + u'Sør-Trøndelag': 16, + 'Telemark': 8, + 'Troms': 19, + 'Vest-Agder': 10, + 'Vestfold': 7, + u'Østfold': 1, +} + +class InvalidCountyException(Exception): pass + +class Module: + def __init__(self, bot): + self.irc = bot + if self.irc: + self.irc.register_keyword('!traffic', self) + + def traffic(self, args): + if len(args) == 2: + rn, c = args + elif args[0][-1].isdigit(): + rn = args[0] + c = None + else: + rn = None + c = args[0] + if c: + c = c.decode('utf8').capitalize() + if not c in counties: + raise InvalidCountyException() + + if rn: + if rn.lower().startswith('e'): + rt = 'Ev' + elif rn.lower().startswith('r'): + rt = 'Rv' + else: + rt = 'Alle' + else: + rt = None + + while rn and not rn[0].isdigit(): + rn = rn[1:] + + if rt and rn: + url = 'http://www.vegvesen.no/trafikk/xml/search.xml?searchFocus.roadTypes=%s&searchFocus.roadNumber=%s' % (rt, rn) + elif c: + url = 'http://www.vegvesen.no/trafikk/xml/search.xml?searchFocus.counties=%d' % counties[c] + u = urllib2.urlopen(url) + + xml = etree.parse(u) + + results = [] + for message in xml.xpath('/searchresult/result-array/result/messages/message'): + if (rt and c and c.lower() in (x.text.lower() for x in message.xpath('actualCounties/string'))) or not c or not rt: + if len(results) == 5: + if rt and rn: + results.append('More results: http://www.vegvesen.no/Trafikkinformasjon/Reiseinformasjon/Trafikkmeldinger?%s' % urllib.urlencode({ + 'type': 'veg', + 'vegkategorier': rt, + 'vegnummer': '6', + })) + else: + results.append('More results: http://www.vegvesen.no/Trafikkinformasjon/Reiseinformasjon/Trafikkmeldinger?%s' % urllib.urlencode({ + 'type': 'fylke', + 'fylke': counties[c], + })) + break + results.append('\002%s\002 - %s' % (message.find('heading').text, message.find('ingress').text)) + return results + + def keyword(self, nick, channel, kw, msg): + if not len(msg): + self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], 'Usage: !traffic [ROADNO] [COUNTY]') + return + + try: + for line in self.traffic(msg.split()): + self.irc.msg(nick.split('!')[0], line.encode('utf8')) + except InvalidCountyException: + self.irc.msg(channel if not channel == self.irc.nickname else nick.split('!')[0], 'Invalid county.') + +if __name__ == '__main__': + import sys + m = Module(None) + print '\n'.join(m.traffic(sys.argv[1:])) -- cgit v1.2.3