Exemplo n.º 1
0
 def check_details(self, url, placeid, resultCount):
     # Performs another API call to get more details
     details = places.place(self.client, placeid)
     result = details["result"]
     try:
         address_component = result["address_components"]
         for x in range(0, len(address_component)):
             if "country" in address_component[x]["types"]:
                 test = address_component[x]["short_name"]
                 if test != "NL":
                     return None
         # Gets the website from the results
         result_website = None
         if "website" in result:
             result_website = result["website"]
         if result_website is not None:
             currentUrl = url
             if "www" in currentUrl:
                 # Extracts hostname from URL
                 currentUrl = self.extract_from_url(currentUrl)
             # Checks if the website in the results is equal to the website we are looking for
             if currentUrl == self.extract_from_url(result_website):
                 # Gets the adress of the result and returns it
                 result_address = result["formatted_address"]
                 self.lat = result["geometry"]["location"]["lat"]
                 self.lng = result["geometry"]["location"]["lng"]
                 return result_address
         elif resultCount == 1:
             self.lat = result["geometry"]["location"]["lat"]
             self.lng = result["geometry"]["location"]["lng"]
             return result["formatted_address"]
         return None
     except KeyError:
         return None
Exemplo n.º 2
0
def find_station(browser, place_id: str) -> Website:
    """Finds a single station given a place_id and returns a website"""

    info = places.place(gmaps, place_id)['result']

    name = info['name']
    address = info['formatted_address']
    brand = info['name']  # TODO Create a map of station names to brands
    location = info['geometry']['location']
    rating = info['rating'] if 'rating' in info else 0
    rating_count = info[
        'user_ratings_total'] if 'user_ratings_total' in info else 0
    url = info['url']

    browser.get(url)
    time.sleep(2)
    for gas_type in browser.find_elements_by_css_selector(
            'div.section-gas-prices-price'):
        if gas_type.find_element_by_class_name(
                'section-gas-prices-label').text == 'Regular':
            price = gas_type.find_element_by_tag_name('span').text
            if len(price) > 1:
                price = float(price[1:])
            else:
                price = NaN
            return Website(name, brand, address, location['lat'],
                           location['lng'], price, rating, rating_count)
Exemplo n.º 3
0
def get_place_details(place_id):
        """ Returns a dict of - website, opening hours and maps url of the given place_id  """

        google_client = Client(app.config['GOOGLE_API_KEY'])
        
        result = places.place(google_client, place_id, fields=['url', 'website', 'opening_hours'])
        return result['result']
Exemplo n.º 4
0
def get_place_details(
    query: str,
    api_key: Optional[str] = None,
    fields: Optional[List[str]] = None,
):
    """Get the place details for a given a name.
    Args:
        query (str): The text to use to search for a place.
        api_key (str): Optional Google Maps API key, None by default.
        fields (list): Optional fields to retrieve.
    Returns:
        (dict): A dictionary of place details.
    """
    if api_key is None:
        api_key = get_google_maps_api_key()
    if not fields:
        fields = [
            'formatted_address',
            'photo',
            'opening_hours',
            'website',
        ]
    gmaps = Client(key=api_key)
    search = places.find_place(gmaps, query, 'textquery')
    place_id = search['candidates'][0]['place_id']
    place = places.place(gmaps, place_id, fields=fields)
    return place['result']
Exemplo n.º 5
0
def get_google_place_details(place_id):
    logger.debug('Searching Google for place_id: ' + place_id)
    google_place = places.place(client=google_client, place_id=place_id)['result']

    if google_place is None:
        logger.warning('No Google Place found for place_id ' + place_id)
        return None

    return format_google_details(google_place)
Exemplo n.º 6
0
def get_place_details(query, api_key=None, fields=[]):
    """Get the place details for a given a name."""
    if api_key is None:
        api_key = get_google_maps_api_key()
    if not fields:
        fields = [
            "formatted_address",
            "photo",
            "opening_hours",
            "website",
        ]
    gmaps = Client(key=api_key)
    search = places.find_place(gmaps, query, "textquery")
    place_id = search["candidates"][0]["place_id"]
    place = places.place(gmaps, place_id, fields=fields)
    return place["result"]
Exemplo n.º 7
0
def map_district_to_deaths(deaths):
    gmaps = googlemaps.Client(key=os.getenv("GOOGLE_PLACES_API_KEY"))
    districts_deaths_per_day = {}
    for hospital in deaths.keys():
        places = find_place(gmaps, [hospital],
                            "textquery",
                            fields=["place_id"])
        if len(places["candidates"]) == 0:
            print(
                f"Warning could not find address for {hospital}. Skipping...")
            continue
        place_details = place(gmaps, places["candidates"][0]['place_id'])
        district = next(
            iter([
                p["long_name"]
                for p in place_details["result"]["address_components"]
                if "administrative_area_level_2" in p["types"]
            ]))
        if len(places) > 1:
            print(
                f"Using {district} more than one address for {hospital}, using data {', '.join([p['short_name'] for p in place_details['result']['address_components']])}"
            )
        if district in districts_deaths_per_day:
            districts_deaths_per_day[district] = {
                date: {
                    "deathsDaily":
                    districts_deaths_per_day[district][date]["deathsDaily"]
                    or 0 + deaths[hospital][date]
                } or 0
                for date in deaths[hospital].keys()
            }
        else:
            districts_deaths_per_day[district] = {
                date: {
                    "deathsDaily": deaths[hospital][date]
                }
                for date in deaths[hospital].keys()
            }
    calculate_death_totals(districts_deaths_per_day)

    if "City of Bristol" in districts_deaths_per_day:
        districts_deaths_per_day[
            "Bristol, City of"] = districts_deaths_per_day["City of Bristol"]
        del districts_deaths_per_day["City of Bristol"]
    return districts_deaths_per_day
Exemplo n.º 8
0
def find_station_ids() -> list:
    """Finds a list of all the place ids of the stations around town"""

    print("Collecting gas station data.")
    stations = []

    print('Collecting result data')
    time.sleep(2)
    search_result = places.places_nearby(gmaps,
                                         '41.977576,-91.666851',
                                         160935,
                                         keyword='gas station')
    iter = 1
    while True:
        stations += search_result['results']
        if 'next_page_token' not in search_result:
            break
        else:
            iter += 1
            print("Collecting page {}".format(iter), end='\r')
            token = search_result['next_page_token']
            time.sleep(1)
            while True:
                try:
                    search_result = places.places_nearby(
                        gmaps,
                        '41.977576,-91.666851',
                        160935,
                        keyword='gas station',
                        page_token=token)
                    break
                except googlemaps.exceptions.ApiError as e:
                    continue

    result = []
    print("Filtering bad data")
    for s in tqdm(stations):
        info = places.place(gmaps, s['place_id'])['result']

        if info is not None and info['name'] != '':
            result.append(s['place_id'])
        else:
            print("Found a dud station entry, skipping")

    return result
Exemplo n.º 9
0
def get_photos(results, tempdir='tmp'):
    for item in results:
        #if 'political' in item['types']:
        #    continue
        try:
            detailed = Places.place(CLIENT,
                                    item['place_id'])['result']['photos']

            for i in range(min(len(detailed), MAXIMG)):
                imageret = Places.places_photo(CLIENT,
                                               detailed[i]['photo_reference'],
                                               max_width=500)
                write_picture(imageret,
                              item['name'],
                              iteration=i,
                              tempdir=tempdir)
        except:
            try:
                ret = Places.places_photo(CLIENT,
                                          item['photos'][0]['photo_reference'],
                                          max_width=500)
            except:
                continue
            write_picture(ret, item['name'], tempdir=tempdir)
                                     radius=500,
                                     type="restaurant",
                                     max_price=3,
                                     page_token=page_token)
            for obj in response['results']:
                photo = obj["photos"][0]["photo_reference"] if (
                    "photos" in obj) else np.float("nan")
                rating = obj["rating"] if ("rating"
                                           in obj) else np.float("nan")
                user_ratings_total = obj["user_ratings_total"] if (
                    "user_ratings_total" in obj) else np.float("nan")
                writer.writerow([
                    obj['geometry']['location']['lat'],
                    obj['geometry']['location']['lng'], obj["name"], photo,
                    rating, user_ratings_total, obj["vicinity"],
                    obj["place_id"], obj["types"]
                ])
            print("page number {}".format(i))

photo = places_photo(
    gmaps,
    "CmRaAAAAj1V6ntNUDJ72Jtw_AoPrUKQTjzo9G51MpsbsZa8cVdSwQFE4EXXXLO7tOgV0cipgsmS4XEUUJIVmLInnIhhOLrvLXHShqde8jV68NYIFmgPO_S-pwWM39jQGkWnIRIbAEhCttHIKc125_NwpOkMS8K-wGhQwUJb43Td9tWpMAeMCI8R-TV_3OQ",
    max_width=300)
f = open("img.png", 'wb')
for chunk in photo:
    if chunk:
        f.write(chunk)
f.close()

place_results = place(gmaps, 'ChIJqzBe2EVu5kcRvU67ufRGCTU')
# Keep the best restaurants
restaurants = restaurants[(restaurants.rating > 3.5)
                          & (restaurants.user_rating_total > 10)]
key = ""
gmaps = googlemaps.Client(key=key)

additional_info = pd.DataFrame(columns=[
    'place_id', 'international_phone_number', 'weekday_opening_hours',
    'reviews', 'website'
])

for place_id in restaurants.place_id:
    response = place(gmaps,
                     place_id,
                     language="fr",
                     fields=[
                         "review", "international_phone_number",
                         "opening_hours", "website"
                     ])
    results = response["result"]
    website = results["website"] if ("website" in results) else np.float("nan")
    international_phone_number = results["international_phone_number"] if (
        "international_phone_number" in results) else np.float("nan")
    weekday_opening_hours = results["opening_hours"]["weekday_text"] if (
        "opening_hours" in results) else np.float("nan")
    if "reviews" in results:
        reviews = [(element["rating"], element["text"])
                   for element in results["reviews"]]
    else:
        reviews = np.float("nan")
    additional_info = additional_info.append(
Exemplo n.º 12
0
def build_place(id):
    place = places.place(gmaps, id, language='ja')
    result = place['result']
    return Place(id=id, clipped_count=0, rating=result.get('rating', None), name=result['name'],
                 geom="SRID=3857;POINT({0} {1})".format(result['geometry']['location']['lng'],
                                                        result['geometry']['location']['lat']))