Пример #1
0
        def insert_eatery_into_results_collection(eatery_id):
                """
                First time when eatery will bei nserted into the eateries_results_collection
                By default, if you are running a eatery, this will flush the eatery and update the result
                again 
                """
               
                try:
                        google = eateries_results_collection.find_one({"eatery_id": eatery_id}).get("google") 
                except Exception as e:
                        print e
                        google = None
                
                print google
                eateries_results_collection.remove({"eatery_id": eatery_id}) 

                eatery = eateries.find_one({"eatery_id": eatery_id}, {"_id": False, "__eatery_id": True, "eatery_id": True, "eatery_name": True, \
                        "eatery_address": True, "location": True, "eatery_area_or_city": True, "eatery_cost": True, "eatery_url": True})

                latitude, longitude = eatery.pop("location")
                latitude, longitude = float(latitude), float(longitude)
                location = [latitude, longitude]
             
                if int(latitude) == 0:
                        g  = geocoder.google(eatery.get("eatery_address"))
                        longitude, latitude = g.geojson.get("geometry").get("coordinates")
                        location = [latitude, longitude]
                        print "eatery_id <<%s>> has not lcoation, founded by google is <<%s>>"%(eatery_id, location)
                        eateries.update({"eatery_id": eatery_id}, {"$set": {"location": location}}, upsert=False)
                eatery.update({"location": location})

                print eateries_results_collection.insert(eatery)
                return google 
def google_already_present(eatery_id, google):
    if google == "No entry exists on google":
        eateries_results_collection.update(
            {"eatery_id": eatery_id},
            {"$set": {
                "google": "No entry exists on google"
            }},
            upsert=False)
        return

    if google.get("address_components"):
        ##this implies that the google had lready been updated
        eateries_results_collection.update({"eatery_id": eatery_id},
                                           {"$set": {
                                               "google": google
                                           }},
                                           upsert=False)
        return

    eatery_result = eateries.find_one({"eatery_id": eatery_id})
    __eatery_id = eatery_result.get("__eatery_id")
    eatery_name = eatery_result.get("eatery_name")
    print google

    try:
        review_list = google.pop("reviews")
    except Exception as e:
        print e
        print "No reviews present for %s" % eatery_id
        review_list = []

    try:
        pic_list = google.pop("pictures")
    except Exception as e:
        print e
        print "No pictures present for %s" % eatery_id
        pic_list = []

    latitude, longitude = google.get("location")
    latitude, longitude = float(latitude), float(longitude)
    google.update({"location": [latitude, longitude]})
    try:
        address_components = google.pop("pincode")
    except Exception as e:
        try:
            address_components = google.pop("address_components")
        except Exception as e:
            g = GooglePlaces.nearby_search(google_places,
                                           name=eatery_name,
                                           lat_lng={
                                               "lat": latitude,
                                               "lng": longitude
                                           },
                                           types=[
                                               u'restaurant', u'food',
                                               u'point_of_interest',
                                               u'establishment'
                                           ])

            place = g.places[0]
            place.get_details()
            result = place.details
            address_components = result.get("address_components")

    google.update({"address_components": address_components})

    print review_list
    print len(pic_list)
    insert_images(__eatery_id, eatery_name, pic_list)
    insert_reviews(__eatery_id, eatery_name, review_list)
    eateries_results_collection.update({"eatery_id": eatery_id},
                                       {"$set": {
                                           "google": google
                                       }},
                                       upsert=False)
    return
Пример #3
0
 def __init__(self, eatery_id):
         self.eatery_id = eatery_id
         self.eatery_name = eateries.find_one({"eatery_id": self.eatery_id}).get("eatery_name")
         self.eatery_address = eateries.find_one({"eatery_id": self.eatery_id}).get("eatery_address")
def find_google_places(eatery_id):
    eatery_result = eateries.find_one({"eatery_id": eatery_id})
    __eatery_id = eatery_result.get("__eatery_id")
    latitude, longitude = eatery_result.get("location")
    eatery_name = eatery_result.get("eatery_name")

    google = dict()
    latitude = float(latitude)
    longitude = float(longitude)

    print eatery_name, latitude, longitude
    g = GooglePlaces.nearby_search(
        google_places,
        name=eatery_name,
        lat_lng={
            "lat": latitude,
            "lng": longitude
        },
        types=[u'restaurant', u'food', u'point_of_interest', u'establishment'])
    """
        TODO: If no extry prsent og google places
        if not place:
                return None

        """
    try:
        place = g.places[0]
    except Exception as e:
        print terminal.red(str(e))
        google = "No entry exists on google"
        eateries_results_collection.update({"eatery_id": eatery_id},
                                           {"$set": {
                                               "google": google
                                           }},
                                           upsert=False)

    print terminal.green("\nThe match we found for eatery_name %s is %s\n" %
                         (eatery_name, place))

    place.get_details()
    result = place.details
    address_components = result.get("address_components")
    eatery_address = result.get("formatted_address")
    eatery_phone_number = result.get("formatted_phone_number")
    location = [
        float(result.get("geometry").get("location").get("lat")),
        float(result.get("geometry").get("location").get("lng"))
    ]
    place_id = result.get("place_id")
    eatery_international_phone_number = result.get(
        "international_phone_number")
    opening_hours = result.get("opening_hours")
    pic_list = list()

    if place.photos:
        print terminal.green("\n Pics found for  eatery_name %s are %s\n" %
                             (eatery_name, len(place.photos)))
        for picture in place.photos:
            picture.get(picture.orig_width, picture.orig_height)
            print picture.filename
            pic_list.append({
                "data": picture.data,
                "width": picture.orig_width,
                "height": picture.orig_height,
                "url": picture.url
            })
    else:
        print terminal.red("No pic can be found for %s <<[%s, %s]>>" %
                           (eatery_name, latitude, longitude))

    review_list = list()

    if result.get("reviews"):
        for review in result.get("reviews"):
            review_list.append(review)

    else:
        print terminal.red("No reviews can be found for %s <<[%s, %s]>>" %
                           (eatery_name, latitude, longitude))


    google.update({"location": location, "eatery_address": eatery_address, "eatery_phone_number": eatery_phone_number, \
            "place_id": place_id, "address_components": address_components, "eatery_international_phone_number": eatery_international_phone_number,\
            "opening_hours": opening_hours})

    insert_images(__eatery_id, eatery_name, pic_list)
    insert_reviews(__eatery_id, eatery_name, review_list)
    eateries_results_collection.update({"eatery_id": eatery_id},
                                       {"$set": {
                                           "google": google
                                       }},
                                       upsert=False)
    return google