Untitled
user_3839718
python
a year ago
1.6 kB
6
Indexable
import ssl import time from googleplaces import GooglePlaces import simplejson from config import gmap_api_key import json class PlaceSearch: def __init__(self, param, saved_path): self.param = param self.saved_path = saved_path ssl._create_default_https_context = ssl._create_unverified_context self.google_places = GooglePlaces(gmap_api_key) self.query_result = self.google_places.nearby_search(**self.param) def process_results(self, results): data = [] for raw in results.raw_response['results']: item = dict(name=raw['name'], location={'lat': raw['geometry']['location']['lat'], 'lon': raw['geometry']['location']['lng']}, place_id=raw['place_id'], rating=raw.get('rating', None), user_ratings_total=raw.get('user_ratings_total', None), vicinity=raw.get('vicinity', None)) json_data = simplejson.dumps(item, ensure_ascii=False) data.append(json.loads(json_data)) return data def run(self): data = self.process_results(self.query_result) if self.query_result.has_next_page_token: time.sleep(2) next_page_results = self.google_places.nearby_search(pagetoken=self.query_result.next_page_token, **self.param) data.extend(self.process_results(results=next_page_results)) with open(self.saved_path, 'w') as f: json.dump(data, f, ensure_ascii=False)
Editor is loading...
Leave a Comment