eightmoons

loc_cal_update

Oct 10th, 2022
839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 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.         info = [
  42.             " Trip Duration ",
  43.             " Kilometers ",
  44.             " Fuel Used (Ltr) ",
  45.         ]
  46.         values = [
  47.             f' {json_data["route"]["formattedTime"]}  ',
  48.             f' {str("{:.2f}".format((json_data["route"]["distance"]) * 1.61))}  ',
  49.             f' {str("{:.2f}".format((json_data["route"]["fuelUsed"]) * 3.78))}  ',
  50.         ]
  51.         longest_info = len(max(info, key=len))
  52.         longest_values = len(max(values, key=len))
  53.         print(f'API Status: {str(json_status)} = A successful route call')
  54.  
  55.         table1_corner = f'+{"-" * longest_info}+{"-" * longest_values}+'
  56.         print(table1_corner)
  57.         print(f'|{row_builder(" Name", longest_info)}|{row_builder(" Value", longest_values)}|')
  58.         print(table1_corner)
  59.         for i in range(0, len(info)):
  60.             print(f'|{row_builder(info[i], longest_info)}|{row_builder(values[i], longest_values)}|')
  61.         print(table1_corner)
  62.  
  63.         table2_corner = f'+{"-" * longest_kms}+{"-" * longest_narrative_str}+'
  64.         print(table2_corner)
  65.         print(f'|{row_builder(" Distance", longest_kms)}|{row_builder(" Directions", longest_narrative_str)}|')
  66.         print(table2_corner)
  67.         for i in range(0, len(kms)):
  68.             print(f'|{row_builder(kms[i], longest_kms)}|{row_builder(narratives[i], longest_narrative_str)}|')
  69.         print(table2_corner)
  70.     elif json_status == 402:
  71.         show_error(json_status, "Invalid user inputs from one or both location")
  72.     elif json_status == 611:
  73.         show_error(json_status, "Missing an entry for one or both locations")
  74.     else:
  75.         print('************************************************************')
  76.         print(f'FOr Status Code: {str(json_status)}; Refer to:')
  77.         print('https://developer.mapquest.com/documentation/directions-api/status-codes')
  78.         print('*************************************************************\n')
  79.  
Advertisement
Add Comment
Please, Sign In to add comment