eightmoons

loc_cal_Table

Oct 10th, 2022
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. import urllib.parse
  2. import requests
  3.  
  4. main_api = "https://www.mapquestapi.com/directions/v2/route?"
  5. key =
  6.  
  7.  
  8. def show_error(status, message):
  9.     print('******************************************')
  10.     print(f'Status Code: {str(status)}; {message}')
  11.     print('******************************************\n')
  12.  
  13.  
  14. def row_builder(value, longest_str, last=""):
  15.     return f'{value}{" " * ((longest_str) - len(value))}{last}'
  16.  
  17.  
  18. while True:
  19.     orig = input("Starting Location: ")
  20.     if orig in ["q", "orig"]:
  21.         break
  22.     dest = input("Destination: ")
  23.     if dest in ["q", "quit"]:
  24.         break
  25.     url = main_api + urllib.parse.urlencode({
  26.         "key": key,
  27.         "from": orig,
  28.         "to": dest,
  29.     })
  30.     print("URL: " + url)
  31.     json_data = requests.get(url).json()
  32.     json_status = json_data["info"]["statuscode"]
  33.     if json_status == 0:
  34.         kms = []
  35.         narratives = []
  36.         for route in json_data["route"]["legs"][0]["maneuvers"]:
  37.             kms.append(f' {str("{:.2f}".format((route["distance"]) * 1.61))} km  ')
  38.             narratives.append(f' {route["narrative"]}  ')
  39.         longest_narrative_str = len(max(narratives, key=len))
  40.         longest_kms = len(max(kms, key=len))
  41.         headers = f'|{row_builder(" Distance", longest_kms)}|{row_builder(" Directions", longest_narrative_str)}|'
  42.         print(f'API Status: {str(json_status)} = A successful route call.\n\n'
  43.               f'=========================================\n'
  44.               f'Directions from {orig} to {dest}\n'
  45.               f'Trip Duration:\t\t{json_data["route"]["formattedTime"]}\n'
  46.               f'Kilometers:\t\t\t{str("{:.2f}".format((json_data["route"]["distance"]) * 1.61))}\n'
  47.               f'Fuel Used (Ltr):\t{str("{:.2f}".format((json_data["route"]["fuelUsed"]) * 3.78))}\n'
  48.               f'=========================================\n')
  49.  
  50.  
  51.         print(f'+{"-" * longest_kms}+{"-" * longest_narrative_str}+')
  52.         print(headers)
  53.         print(f'+{"-" * longest_kms}+{"-" * longest_narrative_str}+')
  54.         for i in range(0, len(kms)):
  55.             print(f'|{row_builder(kms[i], longest_kms)}|{row_builder(narratives[i], longest_narrative_str)}|')
  56.         print(f'+{"-" * longest_kms}+{"-" * longest_narrative_str}+')
  57.     elif json_status == 402:
  58.         show_error(json_status, "Invalid user inputs from one or both location")
  59.     elif json_status == 611:
  60.         show_error(json_status, "Missing an entry for one or both locations")
  61.     else:
  62.         print('************************************************************')
  63.         print(f'FOr Status Code: {str(json_status)}; Refer to:')
  64.         print('https://developer.mapquest.com/documentation/directions-api/status-codes')
  65.         print('*************************************************************\n')
  66.  
Advertisement
Add Comment
Please, Sign In to add comment