Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib.parse
- import requests
- main_api = "https://www.mapquestapi.com/directions/v2/route?"
- key = ""
- def show_error(status, message):
- print('******************************************')
- print(f'Status Code: {str(status)}; {message}')
- print('******************************************\n')
- # [value] = value ng cell
- # [longest_str] = longest string sa isang row
- # [last] = string na pwede idagdag sa huli ng cell
- # mag ge-generate ito ng row na may complete whitespaces para pantay
- def row_builder(value, longest_str, last=""):
- return f'{value}{" " * ((longest_str) - len(value))}{last}'
- while True:
- orig = input("Starting Location: ")
- if orig in ["q", "orig"]:
- break
- dest = input("Destination: ")
- if dest in ["q", "quit"]:
- break
- url = main_api + urllib.parse.urlencode({
- "key": key,
- "from": orig,
- "to": dest,
- })
- print("URL: " + url)
- json_data = requests.get(url).json()
- json_status = json_data["info"]["statuscode"]
- if json_status == 0:
- kms = []
- narratives = []
- # [1] ilagay lahat ng direction (narratives) at distance sa isang list
- for route in json_data["route"]["legs"][0]["maneuvers"]:
- kms.append(f' {str("{:.2f}".format((route["distance"]) * 1.61))} km ')
- narratives.append(f' {route["narrative"]} ')
- # [2] kunin yung may pinakamahabang length at istore sa variable
- longest_narrative_str = len(max(narratives, key=len))
- longest_kms = len(max(kms, key=len))
- # [3] info = 1st column
- info = [
- " Trip Duration ",
- " Kilometers ",
- " Fuel Used (Ltr) ",
- ]
- # [4] values = 2nd column
- values = [
- f' {json_data["route"]["formattedTime"]} ',
- f' {str("{:.2f}".format((json_data["route"]["distance"]) * 1.61))} ',
- f' {str("{:.2f}".format((json_data["route"]["fuelUsed"]) * 3.78))} ',
- ]
- # same process ng sa [2]
- longest_info = len(max(info, key=len))
- longest_values = len(max(values, key=len))
- print(f'API Status: {str(json_status)} = A successful route call')
- # [5] table1 1st table na magegenerate, nilagay ko sa variable [table1_corner] kase tatlong beses ko sya tatawagin. ang p-print nito ay yung table header
- # + ---------+------+
- # | name | value|
- # + ---------+------+
- table1_corner = f'+{"-" * longest_info}+{"-" * longest_values}+'
- print(table1_corner)
- print(f'|{row_builder(" Name", longest_info)}|{row_builder(" Value", longest_values)}|')
- print(table1_corner)
- # [6] etong for loop, dito yung pag print ng rows
- for i in range(0, len(info)):
- print(f'|{row_builder(info[i], longest_info)}|{row_builder(values[i], longest_values)}|')
- print(table1_corner)
- # [7] table2 eto yung second table na may directions at distance na kinuha sa [1] at [2], same logic ng [5]
- table2_corner = f'+{"-" * longest_kms}+{"-" * longest_narrative_str}+'
- print(table2_corner)
- print(f'|{row_builder(" Distance", longest_kms)}|{row_builder(" Directions", longest_narrative_str)}|')
- print(table2_corner)
- # same ng [6]
- for i in range(0, len(kms)):
- print(f'|{row_builder(kms[i], longest_kms)}|{row_builder(narratives[i], longest_narrative_str)}|')
- print(table2_corner)
- elif json_status == 402:
- show_error(json_status, "Invalid user inputs from one or both location")
- elif json_status == 611:
- show_error(json_status, "Missing an entry for one or both locations")
- else:
- print('************************************************************')
- print(f'FOr Status Code: {str(json_status)}; Refer to:')
- print('https://developer.mapquest.com/documentation/directions-api/status-codes')
- print('*************************************************************\n')
Advertisement
Add Comment
Please, Sign In to add comment