Skip to content

Commit

Permalink
Ruter: Rough and ugly proof of concept? Doesn't really work. Unless y…
Browse files Browse the repository at this point in the history
…ou try hard. Also output is a bit cryptic.
  • Loading branch information
Terje Hoås committed Dec 28, 2012
1 parent 9383d1f commit e963af4
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions Ruter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
# POSSIBILITY OF SUCH DAMAGE.

###
import re
import json
import urllib
import datetime

import supybot.utils as utils
from supybot.commands import *
Expand All @@ -44,9 +48,39 @@ class Ruter(callbacks.Plugin):
This should describe *how* to use this plugin."""
threaded = True

def ruter(self, irc, msg, args):
irc.reply('test')
ruter = wrap(ruter)
baseurl = 'http://api-test.trafikanten.no/'

def search(self, place):
url = self.baseurl + 'RealTime/FindMatches/' + urllib.quote(place)
data = utils.web.getUrl(url)
j = json.loads(data)

return j[0].get('ID')

def get_real_time_data(self, loc):
url = self.baseurl + 'RealTime/GetRealTimeData/' + urllib.quote(str(loc))
data = utils.web.getUrl(url)
j = json.loads(data)
date1 = j[0].get('ExpectedArrivalTime')
direction1 = j[0].get('DestinationName')
date2 = j[1].get('ExpectedArrivalTime')
direction2 = j[1].get('DestinationName')
pattern = r'\d+'
epoch1 = re.search(pattern, date1).group()
epoch2 = re.search(pattern, date2).group()
return int(str(epoch1)[:-3]), direction1, int(str(epoch2)[:-3]), direction2,

def ruter(self, irc, msg, args, place):
"""<boop>
:D"""
loc = self.search(place)
time1, dir1, time2, dir2 = self.get_real_time_data(loc)
t1 = datetime.datetime.fromtimestamp(time1)
t2 = datetime.datetime.fromtimestamp(time2)
irc.reply('Retning ' + str(dir1) + ': ' + str(t1))
irc.reply('Retning ' + str(dir2) + ': ' + str(t2))
ruter = wrap(ruter, ['text'])

Class = Ruter

Expand Down

0 comments on commit e963af4

Please sign in to comment.