Skip to content

Commit

Permalink
Renamed files and added two more files for refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
vladislavtrukhin committed Feb 15, 2020
1 parent 53ac116 commit 9d72247
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("hello")
Empty file added path.py
Empty file.
77 changes: 77 additions & 0 deletions places.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import requests
import json



SEARCH_ENDPOINT = "https://maps.googleapis.com/maps/api/place/textsearch/json"
DETAILS_ENDPOINT = "https://maps.googleapis.com/maps/api/place/details/json"
ROUTE_ENDPOINT = "https://maps.googleapis.com/maps/api/distancematrix/json"
API_KEY = "AIzaSyBYhkmFKfz745tUYWf5CskwslxKan6M_-E" # MUST CHANGE API KEY

#This is just some safe default headers to make sure our connection doesnt somehow get dropped! Ignore these for now
headers = {

"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0",
"Accept-Encoding": "*",
"Connection": "keep-alive"
}

#The API to get details about a place given a place id!
def api_details(id):
parameters = {"place_id":id, "key":API_KEY}

response = requests.get(url=DETAILS_ENDPOINT,params=parameters, headers=headers)

return response.json()

#The API to search for a place given some query text
def api_search(text):
parameters = {"query": text, "key": API_KEY}

response = requests.get(url=SEARCH_ENDPOINT,params=parameters, headers=headers)

return response.json()


def pull_data(input_text):
data = {}
for result in api_search(input_text)['results']:
name = result['name']
data[name] = result
data[name]["descr"] = ""

details = api_details(result['place_id'])
# print("BHours: ")

if 'result' not in details: continue

#These if statements check if the key is actually in the JSON object, AKA they check if the place actually
# has opening hours, or has reviews before trying to access them! This is a safe practice
if 'opening_hours' in details['result'] and 'weekday_text' in details['result']['opening_hours']:
for open_day in details['result']['opening_hours']['weekday_text']:
# print(" "+open_day)
data[name]["descr"] += open_day + "|| "
# print(data)
return data


def find_route(place_ids):
parameters = {"key":API_KEY, "origins": "place_id:" + "|place_id:".join(place_ids)}
# parameters = {"origins": place_ids}
response = requests.get(url=ROUTE_ENDPOINT, params=parameters, headers=headers);
return response.json()

if __name__ == "__main__":

#Main program loop
while True:
try:
input_text = input("Enter your query text: ")
if (input_text == "quit"): exit(0)
places = pull_data(input_text)
print(find_route([place['place_id'] for place in places.values()]))

#Exit on ctrl+c
except KeyboardInterrupt:
print("Exiting program")
exit(0)

0 comments on commit 9d72247

Please sign in to comment.