Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AloyASen/b9bb0ed97c0c687744c42cec3bc381d9 to your computer and use it in GitHub Desktop.
Save AloyASen/b9bb0ed97c0c687744c42cec3bc381d9 to your computer and use it in GitHub Desktop.
Upwork client required this code to pull details from places api
# for parsing http requests to the web
import requests
# for parsing web service responses from gcloud
import json
import time
# enable pretty printing of lists
import pprint
#enable system generated interupts
import sys
class GooglePlaces(object):
#initialize the Google Maps Places api
def __init__(self, apiKey):
super(GooglePlaces, self).__init__()
# add your personal key set from the google cloud consoles
self.apiKey = apiKey
def search_places_by_coordinate(self, location, radius, types):
endpoint_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"
places = []
params = {
'location': location,
'radius': radius,
'types': types,
'key': self.apiKey
}
res = requests.get(endpoint_url, params = params)
results = json.loads(res.content)
places.extend(results['results'])
time.sleep(2)
while "next_page_token" in results:
params['pagetoken'] = results['next_page_token'],
res = requests.get(endpoint_url, params = params)
results = json.loads(res.content)
places.extend(results['results'])
time.sleep(2)
return places
def get_place_from_text(self, placetext,fields):
endpoint_addr = "https://maps.googleapis.com/maps/api/place/findplacefromtext/json"
params = {
'input': placetext,
'inputtype': 'textquery',
'language': 'en',
'fields': ",".join(fields),
'key': self.apiKey
}
response = requests.get(endpoint_addr, params = params)
placeDetails = json.loads(response.content)
return placeDetails
def get_place_details(self, place_id, fields):
endpoint_url = "https://maps.googleapis.com/maps/api/place/details/json"
params = {
'placeid': place_id,
'fields': ",".join(fields),
'key': self.apiKey
}
res = requests.get(endpoint_url, params = params)
place_details = json.loads(res.content)
return place_details
def get_textquery_from_search(self, text):
endpoint_addr = "https://maps.googleapis.com/maps/api/place/textsearch/json"
params = {
'query': text,
'language': 'en',
'key': self.apiKey
}
response = requests.get(endpoint_addr, params = params)
placeDetails = json.loads(response.content)
return placeDetails
if __name__ == '__main__':
api = GooglePlaces("<Put your own clowd enabled api key here>")
#change the type = 'store' -> to the required type
# see https://developers.google.com/places/web-service/supported_types for all reported typues
#places = api.search_places_by_coordinate("40.819057,-73.914048", "100", "restaurant")
# modulate the requirement of fields according to https://developers.google.com/places/web-service/search
fieldDict = ['name', 'formatted_address', 'permanently_closed', 'place_id', 'opening_hours', 'plus_code', 'geometry']
#custom scan i/o for python input
company=input("Enter the Name of the company that you are try to parse from G map : [ use string input only]")
# for the place details of a company from maps
place= api.get_place_from_text(company,fields= fieldDict)
if place['status']=='OK':
#search for the descriptive page details using the previously collected pageid
# get the place details
fieldsForPlaceID = ['name', 'formatted_address', 'international_phone_number', 'website', 'rating', 'review']
details = api.get_place_details(place['candidates'][0]['place_id'], fieldsForPlaceID)
if details['status']=='OK':
print('Company details are properly collected ')
print
else:
print ('Cannot fetch detailed description about organization : output of raw packet')
pp=pprint.PrettyPrinter(indent=4)
pp.pprint(place)
print("==============xxx---- preferably closing script ----xxx=======================")
#kill the script with preferable output
sys.exit()
else:
print(' >>> > the company name cannot be mnapped to a map page id')
sys.exit()
# places= api.get_textquery_from_search('radii corporation')
# print (places)
try:
website = details['result']['website']
except KeyError:
website = ""
try:
name = details['result']['name']
except KeyError:
name = ""
try:
address = details['result']['formatted_address']
except KeyError:
address = ""
try:
phone_number = details['result']['international_phone_number']
except KeyError:
phone_number = ""
try:
reviews = details['result']['reviews']
except KeyError:
reviews = []
print("===================PLACE===================")
print("Name:", name)
print("Website:", website)
print("Address:", address)
print("Phone Number", phone_number)
print("==================REWIEVS==================")
for review in reviews:
author_name = review['author_name']
rating = review['rating']
text = review['text']
time = review['relative_time_description']
profile_photo = review['profile_photo_url']
print("Author Name:", author_name)
print("Rating:", rating)
print("Text:", text)
print("Time:", time)
print("Profile photo:", profile_photo)
print("-----------------------------------------")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment