Example #1
0
def query_for_visits(operation, location, time, mongo, city):
    """Return a dict resulting of the call of `operation` with `location` and
    `time` arguments."""
    if 'find' in str(operation.im_func):
        return answer_to_dict(operation(location, {time: 1}))
    # $near is not supported in aggregate $match
    if 'loc' in location:
        ids = mongo.foursquare.venue.find({'city': city,
                                           'loc': location['loc']}, {'_id': 1})
        ids = [v['_id'] for v in ids]
        location['lid'] = {'$in': ids}
        del location['loc']
    match = {'$match': location}
    project = {'$project': {'time': '$'+time, 'lid': 1, '_id': 0}}
    group = {'$group': {'_id': '$lid', 'visits': {'$push': '$time'}}}
    query = [match, project, group]
    convert = (lambda x: map(int, x)) if time == 'tuid' else None
    return answer_to_dict(itertools.chain(operation(query)['result']), convert)
Example #2
0
def collect_similars(venues_db, client, city):
    """Find similars venues for 100 location in city, save the result in DB and
    return matching venues that were already in DB."""
    venues = answer_to_dict(venues_db.find({'city': city}, {'loc': 1}))
    chosen = sample(venues.items(), 500)
    distances = []
    all_match = []
    for vid, loc in chosen:
        similars = af.similar_venues(vid, client=client)
        if similars is None:
            continue
        else:
            print(vid, similars)
        venues_db.update({'_id': vid}, {'$set': {'similars': similars}})
        matching = answer_to_dict(venues_db.find({'_id': {'$in': similars}},
                                                 {'loc': 1}))
        all_match.append(matching)
        distances.append([geodesic_distance(loc, sloc)
                          for sloc in matching.itervalues()])
    return chosen, distances, all_match