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/direction.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
81 lines (56 sloc)
2.34 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 | |
from pprint import pprint | |
urlRequest = "https://maps.googleapis.com/maps/api/place/textsearch/json?" | |
apiKey = "AIzaSyD1pMiJyDTK5V-ayhWnpNm5FHSXly4TeLk" | |
finalPoint = "https://maps.googleapis.com/maps/api/directions/json?" | |
place= input("what place are you looking for? ").replace(",", " in " ) | |
# this function finds the place in specific area | |
def placesFound(place): | |
place = place.replace(" ","+") | |
parameters = "key={}&query={}".format ( apiKey, place ) | |
dest=[] | |
request = urlRequest + parameters | |
response = urllib.request.urlopen(request).read() | |
places = json.loads(response) | |
for i in range(0,len(places["results"])): | |
results = places["results"][i]['formatted_address'] | |
dest.append(results) | |
return dest | |
# ToDO: chech for empty list | |
for item in placesFound(place) : | |
print(item +"\n") | |
num = input("which one of the following addresses is the best for you, select the address with number from 1 to 5? ?>>> " ) | |
def indexNumber(num): | |
num = int(num) | |
num -=1 | |
return num | |
destination= placesFound(place)[indexNumber(num)] | |
origin = input("where are you ? >>> ") | |
def directions(origin, destination): | |
origin=origin.replace(' ','') | |
destination=destination.replace(' ','') | |
stepSummary=[] | |
navRequest = "origin={}&destination={}&key={}" . format ( origin, destination , apiKey) | |
request = finalPoint + navRequest | |
response = urllib.request.urlopen(request).read() | |
directions = json.loads(response) | |
# ToDO: chech for empty list | |
legs = directions["routes"][0]["legs"] | |
for leg in legs : | |
steps = leg["steps"] | |
for step in steps: | |
stepSummaryary=(step['html_instructions'].replace("0.9em","").replace("style","").replace(":","").replace("div","").replace("font-size","").replace('"',"").replace("</b>","").replace("<b>"," ").replace("</div >"," ").replace("<div >"," ").replace("="," ").replace('<div style"font-size:0.9em">'," ")) | |
stepSummary.append(stepSummaryary) | |
return stepSummary | |
route=(directions(origin,destination)) | |
def cleanText(route): | |
steps=[] | |
for step in route: | |
for letter in step: | |
letter=letter.replace('<','') | |
letter=letter.replace('>','') | |
steps.append(step) | |
return steps | |
for item in cleanText(route): | |
print(item +"\n") | |