示例#1
0
def chicken_finder(request):
    t1 = time.time()
    lat, long = request.args["gps"][0].split(",")
    geopoint = GeoPoint(lat=float(lat), long=float(long))
    ghash = geohash.encode(geopoint.lat, geopoint.long, precision=10)
    postcode = yield geo.geopoint_to_postcode(geopoint, ghash)
    logging.info("Got postcode: %s" % postcode)
    location = Location(geopoint=geopoint, geohash=ghash, postcode=postcode)
    places = {}
    returner = []

    futures = []

    titles_cache = []

    for name, instance in SOURCES.items():
        if instance.NEEDS_POSTCODE and not location.postcode:
            continue
        futures.append(instance.GetAvailablePlaces(location))
    results = yield defer.DeferredList(futures)

    for result in results:
        if result[0]:
            for id, p in result[1].items():
                if p.Title not in titles_cache:
                    titles_cache.append(p.Title)
                    places[id] = p

    # Some sources may already have set the location. Filter those out and get the geopoints
    geopoints = yield geo.address_to_geopoint(
        {l.Id: l.Address
         for l in places.values() if l.Location is None})

    for id, point in geopoints.items():
        places[id] = places[id]._replace(Location=point)

    for id, place in places.items():

        if not place.Distance and not place.Location:
            # We have a problem! For now exclude them from the results.
            # ToDo: Do something smart here.
            continue

        returner.append(
            place._replace(
                Distance=geo.distance(location.geopoint, place.Location)))

    # Now each location hopefully has a resolved GeoPoint we can sort the Locations by the distance from us
    returner = [
        p._asdict()
        for p in sorted(returner, key=operator.attrgetter('Distance'))[:10]
    ]

    t2 = time.time()
    print "Request took %s" % (t2 - t1)
    request.setHeader("content-type", "application/json")
    defer.returnValue(json.dumps(returner))
示例#2
0
    def _fetchChickenPlace(self, id, info):
        '''
        I take an ID and a dictionary fetched from the JustEat page and I return a ChickenPlace with a geopoint.
        I have to fetch some stuff from the JustEat website though, which is silly :(
        I return (id, ChickenPlace)
        '''
        info = json.loads(info)
        just_eat_page = yield getPage(str(HOST+info["identifier"]))
        t1 = time.time()
        print "Inserting ID %s"%id
        parser = get_parser(just_eat_page)

        print "%s - Parsed in %s"%(id,str(time.time()-t1))
        has_chicken = False
        for tag in parser.findAll("h2", attrs={"class":"H2MC"}):
            if "chicken" in tag.text.lower():
                has_chicken = True
                break

        print "%s - Chicken got in %s"%(id,str(time.time()-t1))
        addresses = []
        address_components = parser.find("span", attrs={"itemprop":"address"})

        for comp in address_components.findAll("span"):
            addresses.append(comp.text)

        address =(", ".join(addresses)).strip()
        print "%s - Page to GeoPoint is %s"%(id, str(time.time()-t1))

        if has_chicken:
            geopoint_res = yield geo.address_to_geopoint({0:address})
            geopoint = geopoint_res[0]
        else:
            geopoint = GeoPoint(0,0)

        place = ChickenPlace(
            Id=info["identifier"],
            Source=JustEatSource.NAME,
            Title=info["title"],
            Address=address,
            Location=geopoint,
            Distance=None,
            MenuAvailable=True
        )

        # Return (id,None) if there is no chicken or (id,ChickenPlace) if there is chicken.
        print "%s Page to return is %s"%(id, str(time.time()-t1))
        if has_chicken:
            returner = json.dumps(place._asdict())
        else:
            returner = json.dumps({})
        defer.returnValue({"has_chicken":has_chicken,
                           "place":returner,
                           "id":id})
示例#3
0
    def _fetchChickenPlace(self, id, info):
        '''
        I take an ID and a dictionary fetched from the JustEat page and I return a ChickenPlace with a geopoint.
        I have to fetch some stuff from the JustEat website though, which is silly :(
        I return (id, ChickenPlace)
        '''
        info = json.loads(info)
        just_eat_page = yield getPage(str(HOST + info["identifier"]))
        t1 = time.time()
        print "Inserting ID %s" % id
        parser = get_parser(just_eat_page)

        print "%s - Parsed in %s" % (id, str(time.time() - t1))
        has_chicken = False
        for tag in parser.findAll("h2", attrs={"class": "H2MC"}):
            if "chicken" in tag.text.lower():
                has_chicken = True
                break

        print "%s - Chicken got in %s" % (id, str(time.time() - t1))
        addresses = []
        address_components = parser.find("span", attrs={"itemprop": "address"})

        for comp in address_components.findAll("span"):
            addresses.append(comp.text)

        address = (", ".join(addresses)).strip()
        print "%s - Page to GeoPoint is %s" % (id, str(time.time() - t1))

        if has_chicken:
            geopoint_res = yield geo.address_to_geopoint({0: address})
            geopoint = geopoint_res[0]
        else:
            geopoint = GeoPoint(0, 0)

        place = ChickenPlace(Id=info["identifier"],
                             Source=JustEatSource.NAME,
                             Title=info["title"],
                             Address=address,
                             Location=geopoint,
                             Distance=None,
                             MenuAvailable=True)

        # Return (id,None) if there is no chicken or (id,ChickenPlace) if there is chicken.
        print "%s Page to return is %s" % (id, str(time.time() - t1))
        if has_chicken:
            returner = json.dumps(place._asdict())
        else:
            returner = json.dumps({})
        defer.returnValue({
            "has_chicken": has_chicken,
            "place": returner,
            "id": id
        })
示例#4
0
def chicken_finder(request):
    t1 = time.time()
    lat, long = request.args["gps"][0].split(",")
    geopoint = GeoPoint(lat=float(lat), long=float(long))
    ghash = geohash.encode(geopoint.lat, geopoint.long, precision=10)
    postcode = yield geo.geopoint_to_postcode(geopoint, ghash)
    logging.info("Got postcode: %s"%postcode)
    location = Location(geopoint=geopoint, geohash=ghash, postcode=postcode)
    places = {}
    returner = []

    futures = []

    titles_cache = []

    for name,instance in SOURCES.items():
        if instance.NEEDS_POSTCODE and not location.postcode:
            continue
        futures.append(instance.GetAvailablePlaces(location))
    results = yield defer.DeferredList(futures)

    for result in results:
        if result[0]:
            for id,p in result[1].items():
                if p.Title not in titles_cache:
                    titles_cache.append(p.Title)
                    places[id] = p

    # Some sources may already have set the location. Filter those out and get the geopoints
    geopoints = yield geo.address_to_geopoint({l.Id:l.Address
                                               for l in places.values()
                                               if l.Location is None})

    for id,point in geopoints.items():
        places[id] = places[id]._replace(Location=point)

    for id,place in places.items():

        if not place.Distance and not place.Location:
            # We have a problem! For now exclude them from the results.
            # ToDo: Do something smart here.
            continue

        returner.append(place._replace(Distance=geo.distance(location.geopoint, place.Location)))

    # Now each location hopefully has a resolved GeoPoint we can sort the Locations by the distance from us
    returner = [p._asdict() for p in sorted(returner, key=operator.attrgetter('Distance'))[:10]]

    t2 = time.time()
    print "Request took %s"%(t2-t1)
    request.setHeader("content-type", "application/json")
    defer.returnValue(json.dumps(returner))