eightmoons

loc_calc.py

Oct 10th, 2022
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.10 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. while True:
  8.     orig = input("Starting Location: ")
  9.     if orig in ["q", "orig"]:
  10.         break
  11.     dest = input("Destination: ")
  12.     if dest in ["q", "quit"]:
  13.         break
  14.     url = main_api + urllib.parse.urlencode({
  15.         "key": key,
  16.         "from": orig,
  17.         "to": dest,
  18.     })
  19.     print("URL: " + url)
  20.     json_data = requests.get(url).json()
  21.     json_status = json_data["info"]["statuscode"]
  22.     if json_status == 0:
  23.         print(f'API Status: {str(json_status)} = A successful route call.\n\n'
  24.               f'=========================================\n'
  25.               f'Directions from {orig} to {dest}\n'
  26.               f'Trip Duration:\t\t{json_data["route"]["formattedTime"]}\n'
  27.               f'Kilometers:\t\t\t{str("{:.2f}".format((json_data["route"]["distance"])*1.61))}\n'
  28.               f'Fuel Used (Ltr):\t{str("{:.2f}".format((json_data["route"]["fuelUsed"])*3.78))}\n'
  29.               f'=========================================\n')
  30.         for route in json_data["route"]["legs"][0]["maneuvers"]:
  31.             print(f'{route["narrative"]} ({str("{:.2f}".format((route["distance"])*1.61))} km)')
  32.         print(f'=========================================\n')
  33.     elif json_status == 402:
  34.         print('******************************************')
  35.         print(f'Status Code: {str(json_status)}; Invalid user inputs from one or both location')
  36.         print('******************************************\n')
  37.     elif json_status == 611:
  38.         print('******************************************')
  39.         print(f'Status Code: {str(json_status)}; Missing an entry for one or both locations')
  40.         print('******************************************\n')
  41.     else:
  42.         print('************************************************************')
  43.         print(f'FOr Status Code: {str(json_status)}; Refer to:')
  44.         print('https://developer.mapquest.com/documentation/directions-api/status-codes')
  45.         print('*************************************************************\n')
Advertisement
Add Comment
Please, Sign In to add comment