Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chatBot/map.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
42 lines (42 sloc)
1.82 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-import urllib.request | |
-import json | |
-import re | |
- #https://www.youtube.com/watch?v=UrrWxyq1Z48 | |
-#Strips the html using regular expression and condense those words into sentences | |
-def striphtml(data): | |
- p = re.compile(r'<.*?>') | |
- return p.sub(' ', data) | |
- | |
- | |
-def destin(): | |
- | |
- # Google MapsDirections API endpoint | |
- endpoint = 'https://www.google.com/maps/api/directions/json?' | |
- api_key = "AIzaSyCjYXLdIxy1eOMbQ-rzNVwSZ6Kfhg3zCtc" | |
- # Asks the user to input what is your current location and what is your destination. | |
- origins = input('What is your current location?: ').replace(' ', '+') | |
- destination = input('What is your destination?: ').replace(' ', '+') | |
- # Building the URL for the request | |
- print(origins) | |
- try: | |
- | |
- nav_request = 'origin={}&destination={}&key={}'.format(origins, destination, api_key) | |
- request = endpoint + nav_request | |
- # Sends the request and reads the response. | |
- response = urllib.request.urlopen(request).read().decode('utf8') | |
- #print(response) | |
- # Loads response as JSON file | |
- directions = json.loads(str(response)) | |
- #print(directions) | |
- instructions = [] | |
- steps = directions['routes'][0]['legs'][0]['steps'] | |
- for step in steps: | |
- instructions.append(striphtml(step['html_instructions'])) | |
- for instruction in instructions: | |
- print(instruction) | |
- # Prints a response after the directions are displayed to show that the user has reached destination reached destination. | |
- print('You have reached your destination.') | |
- except: | |
- # If the destination is to far a message is displayed explaining that. | |
- print("The destination is too far the direction cannot be printed.") | |
- destin() |