Skip to content

Commit

Permalink
tests and URL improved (vishakha-lall#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityabisoi authored Apr 2, 2020
1 parent fa44a4d commit ef50522
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
9 changes: 8 additions & 1 deletion constants.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
BASE_URL = {'direction': "https://www.google.com/maps/dir/?api=1", 'geocoding': "https://www.google.com/maps/search/?api=1&query"} # noqa: E501
BASE_URL = {'direction': "https://www.google.com/maps/dir/?api=1",
'geocoding': "https://www.google.com/maps/search/?api=1&query",
'latlng': "https://maps.googleapis.com/maps/api/geocode/json",
'timezone': "https://maps.googleapis.com/maps/api/timezone/json",
'mapsstatic': "https://maps.googleapis.com/maps/api/staticmap",
'elevation': "https://maps.googleapis.com/maps/api/elevation/json",
'places': "https://maps.googleapis.com/maps/api/place",
} # noqa: E501
16 changes: 9 additions & 7 deletions googleMapsApiModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def get_timestamp(date_time):

@logger_config.logger
def get_lat_lng(place):
response = requests.get(f'https://maps.googleapis.com/maps/api/geocode/json?address={place}&key={config.key}') # noqa: E501
response = requests.get(f'{BASE_URL["latlng"]}?address={place}&key={config.key}') # noqa: E501
resp_json_payload = response.json()
lat_lng = resp_json_payload['results'][0]['geometry']['location']
return (lat_lng)
Expand All @@ -45,10 +45,11 @@ def get_lat_lng(place):
def timezone(place, date_time): # format of datetime should be YYYY MM DD Hrs Mins and place should be a normal string # noqa: E501
lat_lng = get_lat_lng(place)
timestamp = get_timestamp(date_time)
response = requests.get(f'https://maps.googleapis.com/maps/api/timezone/json?location={lat_lng["lat"]},{lat_lng["lng"]}&timestamp={timestamp}&key={config.key}') # noqa: E501
response = requests.get(f'{BASE_URL["timezone"]}?location={lat_lng["lat"]},{lat_lng["lng"]}&timestamp={timestamp}&key={config.key}') # noqa: E501
resp_dict = response.json()
for key in resp_dict:
print(f"{key} : {resp_dict[key]}")
return resp_dict["timeZoneId"]


@logger_config.logger
Expand All @@ -65,7 +66,7 @@ def geocoding(search_location):
@logger_config.logger
def mapsstatic(search_location):
address = search_location
result_url = f'https://maps.googleapis.com/maps/api/staticmap?center={address.lower().replace(" ", "+")}&zoom=13&scale=1&size=600x350&maptype=roadmap&key={config.key}&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0xff0000%7Clabel:L%7C{address.lower().replace(" ", "+")}' # noqa: E501
result_url = f'{BASE_URL["mapsstatic"]}?center={address.lower().replace(" ", "+")}&zoom=13&scale=1&size=600x350&maptype=roadmap&key={config.key}&format=png&visual_refresh=true&markers=size:mid%7Ccolor:0xff0000%7Clabel:L%7C{address.lower().replace(" ", "+")}' # noqa: E501
logging.debug(result_url)
webbrowser.open_new(result_url)
return result_url
Expand All @@ -82,7 +83,7 @@ def mapsstatic(search_location):
@logger_config.logger
def elevation(search_location):
result = gmaps.geocode(search_location)
json = requests.get(f'https://maps.googleapis.com/maps/api/elevation/json?locations={result[0]["geometry"]["location"]["lat"]},{result[0]["geometry"]["location"]["lng"]}&key={config.key}').json() # noqa: E501
json = requests.get(f'{BASE_URL["elevation"]}?locations={result[0]["geometry"]["location"]["lat"]},{result[0]["geometry"]["location"]["lng"]}&key={config.key}').json() # noqa: E501
result_value = json['results'][0]['elevation']
position = "above" if result_value > 0 else "below"
print(f'{search_location} is {round(result_value,2)} metres {position} sea level')
Expand All @@ -91,10 +92,11 @@ def elevation(search_location):
@logger_config.logger
def places(search_location):
address = search_location
json = requests.get(f'{BASE_URL["places_textsearch"]}={address.lower().replace(" ", "+")}&inputtype=textquery&fields=photos,formatted_address,place_id&key={config.key}').json() # noqa: E501
json = requests.get(f'{BASE_URL["places"]}/findplacefromtext/json?input={address.lower().replace(" ", "+")}&inputtype=textquery&fields=photos,formatted_address,place_id&key={config.key}').json() # noqa: E501
logging.debug("Address:"+json["candidates"][0]["formatted_address"])
details = requests.get(f'{BASE_URL["places_details"]}={json["candidates"][0]["place_id"]}&fields=rating,formatted_phone_number&key={config.key}').json() # noqa: E501
details = requests.get(f'{BASE_URL["places"]}/details/json?place_id={json["candidates"][0]["place_id"]}&fields=rating,formatted_phone_number&key={config.key}').json() # noqa: E501
logging.debug("Rating:"+str(details["result"]["rating"]))
logging.debug("Phone:"+details["result"]["formatted_phone_number"])
photo = f'{BASE_URL["places_photos"]}={json["candidates"][0]["photos"][0]["photo_reference"]}&key={config.key}' # noqa: E501
photo = f'{BASE_URL["places"]}/photo?maxwidth=400&photoreference={json["candidates"][0]["photos"][0]["photo_reference"]}&key={config.key}' # noqa: E501
webbrowser.open_new(photo)
return json["candidates"][0]["place_id"]
18 changes: 18 additions & 0 deletions test_googleMapsApiModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,21 @@ def test_elevation_with_invalid_input(self):
with pytest.raises(IndexError):
result = googleMapsApiModule.elevation('hihih')
assert type(result) is float

def test_places_with_valid_input(self):
result = googleMapsApiModule.places('princeton university')
assert result == "ChIJ6baYzdjmw4kRTwKQ-tZ-ugI"

def test_places_with_invalid_input(self):
with pytest.raises(IndexError):
result = googleMapsApiModule.places('esffsf')
assert result == "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CmRaAAAA8o1VGVvds8zkqh745Pa6t2KcBbMA&key="+config.key

def test_timezone_with_valid_input(self):
result = googleMapsApiModule.timezone('ohio','2000 11 21 11 41')
assert result == "America/New_York"

def test_timezone_with_invalid_input(self):
with pytest.raises(ValueError):
result = googleMapsApiModule.timezone('wijd..','2000 18 21 11 41')
assert result == "America/New_York"

0 comments on commit ef50522

Please sign in to comment.