def getNextDeparturesForStop(args): ''' Returns Lines (code, destination) to departureTimes (in seconds) ''' json = None if args.lines: json = Communicator.Instance().sendRequest_getJSON("GetNextDepartures", stopCode=args.stop.code, linesCode=",".join([l.code for l in args.lines])) else: json = Communicator.Instance().sendRequest_getJSON("GetNextDepartures", stopCode=args.stop.code) stop = Stop.fromJson(json["stop"]) departures = [Departure.fromJson(dep) for dep in json["departures"]] departuresFiltered = [dep for dep in departures if dep] return stop, departuresFiltered
def getLineColors(): ''' Returns a dictionary of {line: colour} ''' json = Communicator.Instance().sendRequest_getJSON("GetLinesColors") linesToColors = dict() for color in json["colors"]: linesToColors[color["lineCode"]] = { "background": color["background"], "foreground": color["text"] } return linesToColors
def getTodaysStopsAndLines(): todaysStops = "todaysStops" todaysLines = "todaysLines" if Tpg.cache.has(todaysLines): return Tpg.cache.read(todaysStops), Tpg.cache.read(todaysLines) json = Communicator.Instance().sendRequest_getJSON("GetStops") stops = [Stop(s["stopCode"],s["stopName"]) for s in json["stops"]] lines = [] for stop in json["stops"]: for connection in stop["connections"]: line = Line(connection["lineCode"], connection["destinationName"]) if line not in lines: lines.append(line) Tpg.cache.write(todaysStops, stops, delay=Tpg.cacheTime) # cache for an hour Tpg.cache.write(todaysLines, lines, delay=Tpg.cacheTime) return stops,lines
import os import logging from flask import Flask, request, redirect from config import Config from tpgnow.server import Server from tpgnow.communicator import Communicator logging.basicConfig(level=logging.DEBUG) app = Flask(__name__) app.debug = True # setup Communicator Communicator.Instance().apiKey = os.getenv("API_KEY", Config.apiKey) server = Server() def _get_agent_and_baseurl(request): agent = request.headers.get('User-Agent') baseurl = request.headers.get('Host') url = request.url print(url) return {"agent" : agent, "baseurl": baseurl, "url": url} @app.route('/help') def help(): requestArgs = _get_agent_and_baseurl(request) response = server.getHelp(**requestArgs) return server.reply(response, **requestArgs)