Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. from googlemaps import places
  2. import requests
  3. import json
  4. import time
  5. class GooglePlaces(object):
  6.     def __init__(self, apiKey):
  7.         API_KEY = "AIzaSyDT67KyLaX7PLiTji5iSLK2zCiw9CszLag"
  8.         super(GooglePlaces, self).__init__()
  9.         self.apiKey = apiKey
  10.  
  11.     def get_stores_by_string(self, location, radius, types):
  12.         stores = []
  13.         endpoint_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?&key={}".format(API_KEY)
  14.         params = {
  15.             'location': location,
  16.             'radius': radius,
  17.             'types': types,
  18.             'key': self.apiKey
  19.         }
  20.         res = requests.get(endpoint_url, params = params)
  21.         results =  json.loads(res.content)
  22.         places.extend(results['results'])
  23.         time.sleep(2)
  24.         while "next_page_token" in results:
  25.             params['pagetoken'] = results['next_page_token'],
  26.             res = requests.get(endpoint_url, params = params)
  27.             results =  json.loads(res.content)
  28.             # results = res.json()
  29.             # with open('results.json', 'w') as f:
  30.             #     json.dump(results, f)
  31.             places.extend(results['results'])
  32.             time.sleep(2)
  33.        
  34.         print(len(places))
  35.  
  36.         return places
  37.  
  38.     def get_store_details(self, place_id, fields):
  39.         endpoint_url = "https://maps.googleapis.com/maps/api/place/details/json?"
  40.         params = {
  41.             'placeid': place_id,
  42.             'fields': ",".join(fields),
  43.             'key': self.apiKey
  44.         }
  45.         res = requests.get(endpoint_url, params = params)
  46.         place_details =  json.loads(res.content)
  47.         return place_details
  48.  
  49. def main():
  50.     api = GooglePlaces("AIzaSyDT67KyLaX7PLiTji5iSLK2zCiw9CszLag")
  51.  
  52.     places = api.get_stores_by_string("32.795297,-96.763218", "1000", "store")
  53.     fields = ['name', 'formatted_address', 'international_phone_number', 'website', 'rating', 'type']
  54.  
  55.     for place in places:
  56.         details = api.get_store_details(place['place_id'], fields)
  57.         try:
  58.             website = details['result']['website']
  59.         except KeyError:
  60.             website = ""
  61.  
  62.         try:
  63.             name = details['result']['name']
  64.         except KeyError:
  65.             name = ""
  66.  
  67.         try:
  68.             address = details['result']['formatted_address']
  69.         except KeyError:
  70.             address = ""
  71.  
  72.         try:
  73.             phone_number = details['result']['international_phone_number']
  74.         except KeyError:
  75.             phone_number = ""
  76.        
  77.         try:
  78.             type_place = details['result']['types']
  79.         except KeyError:
  80.             type_place = ""
  81.  
  82.  
  83.         print("--------------------STORE--------------------")
  84.         print("Name:", name)
  85.         print("Website:", website)
  86.         print("Address:", address)
  87.         print("Phone Number:", phone_number)
  88.         print("Type:", type_place)
  89.  
  90. if __name__ == '__main__':
  91.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement