def api_id(): # Check if an ID was provided as part of the URL. # If ID is provided, assign it to a variable. # If no ID is provided, display an error in the browser. if 'id' in request.args: _id = int(request.args['id']) else: return "Error: No id field provided. Please specify an id." # Create an empty list for our results results = [] # Loop through the data and match results that fit the requested ID. for stop in luas_list(): if stop['stop_number'] == _id: lc = luas.api.LuasClient() stop['inbound'] = {} stop['outbound'] = {} inbound_dest = str( lc.next_tram(stop['abbr'], LuasDirection.Inbound).destination) try: inbound_due = int( lc.next_tram(stop['abbr'], LuasDirection.Inbound).due) except ValueError: if inbound_dest == 'No trams forecast': inbound_due = '' else: inbound_due = 'DUE' stop['inbound']['destination'] = inbound_dest stop['inbound']['due'] = inbound_due outbound_dest = str( lc.next_tram(stop['abbr'], LuasDirection.Outbound).destination) try: outbound_due = int( lc.next_tram(stop['abbr'], LuasDirection.Outbound).due) except ValueError: if outbound_dest == 'No trams forecast': outbound_due = '' else: outbound_due = 'DUE' stop['outbound']['destination'] = outbound_dest stop['outbound']['due'] = outbound_due results.append(stop) print(results) # Use the jsonify function from Flask to convert our list of # Python dictionaries to the JSON format. return jsonify(results)
def api_id(): # Check if an ID was provided as part of the URL. # If ID is provided, assign it to a variable. # If no ID is provided, display an error in the browser. if 'id' in request.args: _id = int(request.args['id']) else: return "Error: No id field provided. Please specify an id." # Create an empty list for our results results = [] # Loop through the data and match results that fit the requested ID. # IDs are unique, but other fields might return many results for stop in luas_list(): if stop['number'] == _id: lc = luas.api.LuasClient() inbound = stop outbound = stop inbound_dest = str(lc.next_tram(stop['abbr'], LuasDirection.Inbound).destination) inbound_due = lc.next_tram(stop['abbr'], LuasDirection.Inbound).due inbound['destination'] = inbound_dest inbound['due'] = inbound_due outbound_dest = str(lc.next_tram(stop['abbr'], LuasDirection.Outbound).destination) outbound_due = lc.next_tram(stop['abbr'], LuasDirection.Outbound).due outbound['destination'] = outbound_dest outbound['due'] = outbound_due results.append(inbound) results.append(outbound) # Use the jsonify function from Flask to convert our list of # Python dictionaries to the JSON format. return jsonify(results)
from urllib.request import Request, urlopen from json import loads from helpers.luas_list import luas_list import helpers.coordinates as co from flask import Flask, render_template my_address = 'Dorset Street Dublin' my_location = co.my_location(my_address) closest_stop = co.closest(luas_list(), my_location) print(closest_stop) url = 'http://127.0.0.1:5000/stop?id=' + str(closest_stop['number']) req = Request( url, None, { 'User-agent': 'Mozilla/5.0 (Windows; U; Windows\ NT 5.1; de; rv: 1.9.1.5) Gecko/20091102 Firefox/3.5.5' }) data = loads(urlopen(req).read().decode("utf-8")) print(data) # Quick Flask Web App app = Flask(__name__) class Station: def __init__(self, name, lat, lng): self.name = name self.lat = lat self.lng = lng
def api_all(): return jsonify(luas_list())