Esempio n. 1
0
def search(name):
    f = Factual(FACTUAL_KEY, FACTUAL_SECRET)
    table = f.table('restaurants-us')
    
    result = table.search(name)
    #print result.data()[0]
    return result.data()[0]
Esempio n. 2
0
    def handle(self, *args, **options):
        factual = Factual(KEY, SECRET)
        restaurants_table = factual.table('restaurants-us')

        # Get all restaurants from New York
        # {"$and":[{"country":{"$eq":"US"}},{"region":{"$eq":"NY"}}]}
        query = restaurants_table.filters({'$and':[{'region':{'$eq':"NY"}}]}).include_count(True)
        total = query.total_row_count()
        cnt_requests = (total-1)/BATCH + 1
        self.stdout.write('Ready to import %s restaurants, in %s requests' % (total, cnt_requests))
        for i in range(cnt_requests):
            try:
                data = query.offset(BATCH * i).limit(BATCH).data()
            except APIException as e:
                # If you are using a free version of factual, only 500 restaurant are imported
                # you need to premium account to access the complete data
                self.stdout.write('API Error: %s' % e)
                break

            for restoData in data:
                # Get or created restaruant using factual id
                resto, created = Restaurant.objects.get_or_create(identifier=restoData.get('factual_id'))
                # Update restaurant with new values
                self.map_data_restaurant(resto, restoData)

            self.stdout.write('Successfully imported batch %s of restaurants' % i)
Esempio n. 3
0
def search(name):
    f = Factual(FACTUAL_KEY, FACTUAL_SECRET)
    table = f.table('restaurants-us')

    result = table.search(name)
    #print result.data()[0]
    return result.data()[0]
def main():
    factual = Factual(KEY, SECRET)

    table = factual.table('places-us')
    lim = 400
    
    q = table.filters({"$and":[{'locality':{'$in':['los angeles', 'boston', 'rawlins']}}, {'name': {'$blank': False}}, {'locality': {'$blank': False}}, {'latitude': {'$blank': False}}, {'longitude': {'$blank': False}},{'factual_id': {'$blank': False}}]}).limit(lim).sort('$random_133').select("name,locality,latitude,longitude,factual_id")

    print '{ "type": "FeatureCollection","features": ['

    for count in range(0,lim):

        lat = q.data()[count][u'latitude']
        name = q.data()[count][u'name']
        lng = q.data()[count][u'longitude']
        fid = q.data()[count][u'factual_id']
        locality = q.data()[count][u'locality']

        coor = '"coordinates":' + '[' + str(lng) + ',' + str(lat) +']},'

        pop_den = dstk.coordinates2statistics((lat,lng))[0]['statistics']['population_density']['value']
        #pop_den sometimes returns value even when in mountain or ocean. pop_us remedies this issue but is limited to US. 
        pop_us = dstk.coordinates2statistics((lat,lng))[0]['statistics']['us_population']['value']
        landCover = dstk.coordinates2statistics((lat,lng))[0]['statistics']['land_cover']['value']
        
        typFeat = '{ "type": "Feature",'
        geo = '"geometry": {"type": "Point",' + coor
        prop = '"properties": {"name": "' + name + '", "locality":"' + locality + '", "factual_id": "' + fid + '", "pop_density": "' + str(pop_den) + '", "us_pop": "' + str(pop_us) + '", "enviro": "' + landCover + '"}}'
        
        if count == lim-1:
            print typFeat + geo + prop
        else:
            print typFeat + geo + prop + ","

    print ']}'
Esempio n. 5
0
def look_up_upc(upc):
    factual = Factual("1psULPx7BQfmamX3bnkOnR7NWkcPRKcjnSvazvXF", "Eu8sIGOyXIPrq3jHAudGjkPea4v5v813jJcxOOTW")

    q = factual.table("products-cpg").filters({"upc": "611269991000"})

    result = q.data()[0]
    return "{brand} - {product_name}".format(**result)
Esempio n. 6
0
def push_product(org_id, ean_13, quantity):
    fb = firebase.FirebaseApplication(
        'https://feedthechildren.firebaseio.com/', None)
    exists = fb.get("/inventory/" + org_id,
                    None,
                    params={'print': 'pretty'},
                    headers={'X_FANCY_HEADER': 'VERY FANCY'})
    if exists is not None and ean_13 in exists:
        result = fb.put('/inventory/' + org_id + '/' + ean_13,
                        'quantity',
                        str(int(exists[ean_13]['quantity']) + quantity),
                        params={'print': 'pretty'},
                        headers={'X_FANCY_HEADER': 'VERY FANCY'})
        return str(result)
    factual = Factual('pFMeueZJQpyZnSPILemUPxzNJmathJyrxoqOnow0',
                      'ROHZOgzy9GwJGb9egKpzAVTYZq35iuj6f3Uu4rNu')
    nutrition = factual.table('products-cpg-nutrition').search(ean_13)
    result = fb.put('/inventory/' + org_id,
                    ean_13, {
                        'quantity': str(quantity),
                        'metadata': nutrition.data()[0],
                        'reserved': str(0)
                    },
                    params={'print': 'pretty'},
                    headers={'X_FANCY_HEADER': 'VERY FANCY'})
    return str(result)
Esempio n. 7
0
def searchZip(name, zipcode):
    f = Factual(FACTUAL_KEY, FACTUAL_SECRET)
    table = f.table('restaurants-us')

    filters = table.filters({'postcode': zipcode}).limit(1)
    result = filters.search(name)
    #print result.data()[0]
    return result.data()[0]
Esempio n. 8
0
def searchZip(name, zipcode):
    f = Factual(FACTUAL_KEY, FACTUAL_SECRET)
    table = f.table('restaurants-us')

    filters = table.filters({'postcode': zipcode}).limit(1)
    result = filters.search(name)
    #print result.data()[0]
    return result.data()[0]
Esempio n. 9
0
def search(name):
    f = Factual(FACTUAL_KEY, FACTUAL_SECRET)
    table = f.table('restaurants-us')
    
    result = table.search(name)
    try:
        return result.data()[0]
    except IndexError:
        return {}
Esempio n. 10
0
def push_product(org_id, ean_13, quantity):
    fb = firebase.FirebaseApplication('https://feedthechildren.firebaseio.com/', None)
    exists = fb.get("/inventory/" + org_id, None, params={'print': 'pretty'}, headers={'X_FANCY_HEADER': 'VERY FANCY'})
    if exists is not None and ean_13 in exists:
        result = fb.put('/inventory/' + org_id + '/' + ean_13, 'quantity', str(int(exists[ean_13]['quantity']) + quantity), params={'print': 'pretty'}, headers={'X_FANCY_HEADER': 'VERY FANCY'})
        return str(result)
    factual = Factual('pFMeueZJQpyZnSPILemUPxzNJmathJyrxoqOnow0', 'ROHZOgzy9GwJGb9egKpzAVTYZq35iuj6f3Uu4rNu')
    nutrition = factual.table('products-cpg-nutrition').search(ean_13)
    result = fb.put('/inventory/' + org_id, ean_13, {'quantity': str(quantity), 'metadata' : nutrition.data()[0], 'reserved' : str(0)}, params={'print': 'pretty'}, headers={'X_FANCY_HEADER': 'VERY FANCY'})
    return str(result)
Esempio n. 11
0
def searchCity(name,city,state):
    f = Factual(FACTUAL_KEY, FACTUAL_SECRET)
    table = f.table('restaurants-us')

    filters = table.filters({'locality': city, 'region': state}).limit(1)
    result = filters.search(name)
    try:
        return result.data()[0]
    except IndexError:
        return {}
def GetBizNearAddress(category_id, address, radius_meters, offset, n):
    factual = Factual('YK4fO9P1qZFZy4Cu7HDNkeZd9YEN1ut4tyO426OR',
                      '9G1L9zUop7kwMmNkM7eIYsFNbvhINDl8hyxyZnKv')
    places = factual.table('places')
    # All with category_id
    rest = places.filters({"category_ids": {"$includes": category_id}})
    # Latitutde and Longitude of address
    lat, lng = GetLatLng(address)
    near = rest.geo(circle(lat, lng,
                           radius_meters)).offset(offset).limit(n).data()
    return near
Esempio n. 13
0
def main():
    factual = Factual(KEY, SECRET)
    
    table = factual.table('places')
    
    q1 = table.search("sushi Santa Monica")
    print q1.data()[1]
    print q1.get_url()
    
    q2 = table.filters({'category_ids':{'$includes':338}, 'region': "CA"}).limit(1)
    print q2.data()
Esempio n. 14
0
def main():
    factual = Factual(KEY, SECRET)
    
    table = factual.table('places')
    
    # q1 = table.search("sushi Santa Monica")
    # print q1.data()[1]
    # print q1.get_url()
    
    q2 = table.select("longitude,latitude,website").filters({'category_ids':{'$includes':218}, 'region': "WA", 'locality': "SEATTLE"}).limit(50)
    print q2.data()
Esempio n. 15
0
def main():
    #print "---------------TEST OF FACTUAL API-----------------"
    f = Factual(FACTUAL_KEY, FACTUAL_SECRET)
    
    table = f.table('restaurants-us')
    
    q1 = table.search("Ichiro")
    print q1.data()[1]['name']
    #print q1.get_url()
    
    q2 = table.filters({'region': "NY"}).limit(1)
def main():
    factual = Factual(KEY, SECRET)
    
    table = factual.table('places')
    
    q1 = table.search("sushi Santa Monica")
    print q1.data()[1]
    print q1.get_url()
    
    q2 = table.filters({'category': "Food & Beverage", 'region': "CA"}).limit(1)
    print q2.data()
Esempio n. 17
0
def new_restaurants():
    """
    This function gets restaurants for a certain zipcode and is not visible to the user.
    """
    factual = Factual(KEY, SECRET)
    table = factual.table('restaurants')
    user_geo =  session['user_geo']   
    new_restaurants = table.filters({"postcode": 20006}).limit(50)
    check_db_for_restos(new_restaurants, user_geo)

    return "Success"
Esempio n. 18
0
def main():
    #print "---------------TEST OF FACTUAL API-----------------"
    f = Factual(FACTUAL_KEY, FACTUAL_SECRET)

    table = f.table('restaurants-us')

    q1 = table.search("Ichiro")
    print q1.data()[1]['name']
    #print q1.get_url()

    q2 = table.filters({'region': "NY"}).limit(1)
Esempio n. 19
0
 def __init__(self, query, lat, lng, radius):
     factual = Factual(app.config['FACTUAL_KEY'],
                       app.config['FACTUAL_SECRET'])
     places = factual.table('places')
     # Searches for all restaurants
     if query.lower() == 'restaurants':
         self.data = places.filters({'category_ids':{'$includes_any':[312,338]}}).\
             geo(circle(lat, lng, radius)).limit(50).data()
     # Searches according to user's query
     else:
         self.data = places.search(query).geo(circle(
             lat, lng, radius)).limit(50).data()
Esempio n. 20
0
def main():

	factual = Factual(KEY, SECRET)

	table = factual.table('restaurants')

	q1= table.filters({"locality":"washington"})

	q2= q1.select("name, address, tel, website")


	print q2.data()
Esempio n. 21
0
def check_db_for_restos(restaurant_data, user_geo):
    """ 
    Checks database for restaurants to see if they exist as entries yet. 
    If not, adds restaurants into database.
    """

    restaurant_ids = []

    factual = Factual(KEY, SECRET)
    table = factual.table('restaurants')
 

    for restaurant in restaurant_data: 
        parsed_data = parse_restaurant_input(restaurant)
        db_entry = model.session.query(model.Restaurant).filter_by(name= parsed_data['name']).first() 
        

        if db_entry:
             restaurant_deets = db_entry.restaurant_features.get_all_data()
             if restaurant_deets:
                restaurant_ids.append(db_entry.id)
 
        else:
            new_resto = table.filters({'name':{'$bw':parsed_data['name']}})
            new_resto_data = new_resto.data() 
       
            if new_resto_data:
                new_restaurant = model.Restaurant()

                for item in new_resto_data: 
                    new_restaurant.set_from_factual(item)
                    model.session.add(new_restaurant)
            
                    new_restaurant_features = model.Restaurant_Features()
                    new_restaurant_features.restaurant = new_restaurant
                    new_restaurant_features.set_from_factual(item)

                    model.session.add(new_restaurant_features)

                    new_restaurant_categories = model.Restaurant_Category()  
                    new_restaurant_categories.restaurant_id = new_restaurant.id                  
                    new_restaurant_categories.set_from_factual(item)
                    model.session.add(new_restaurant_categories)
                    model.session.commit()

                restaurant_ids.append(new_restaurant.id) 
    
    if restaurant_ids == []:
        flash("We need a bit more information, please try some other restaurants.")
        return redirect("/restaurants")
    else: 
        return restaurant_ids
Esempio n. 22
0
def get_reservations_for_user(user_id):
    fb = firebase.FirebaseApplication('https://feedthechildren.firebaseio.com/', None)
    factual = Factual('pFMeueZJQpyZnSPILemUPxzNJmathJyrxoqOnow0', 'ROHZOgzy9GwJGb9egKpzAVTYZq35iuj6f3Uu4rNu')
    exists = fb.get("/reservations/" + user_id, None, params={'print': 'pretty'}, headers={'X_FANCY_HEADER': 'VERY FANCY'})
    if exists is not None:
        for org in exists:
            for ean in exists[org]:
                quantity = exists[org][ean]
                nutrition = factual.table('products-cpg-nutrition').search(ean)
                resp = nutrition.data()[0]
                exists[org][ean] = {'quantity' : quantity, 'metadata' : resp}
        return jsonify(exists)
    return ""
def main():
    factual = Factual(KEY, SECRET)

#    table = factual.table('places-us')
#    lim = 10
#    
#    q = table.limit(lim).select("en, category_id")
    
#    s = factual.table('places').schema()
#    print str(s)
    

    s = factual.table('places').schema()['fields']
    print(s)
def main():
    factual = Factual(KEY, SECRET)

    table = factual.table('places')

    q1 = table.search("sushi Santa Monica")
    print q1.data()[1]
    print q1.get_url()

    q2 = table.filters({
        'category': "Food & Beverage",
        'region': "CA"
    }).limit(1)
    print q2.data()
Esempio n. 25
0
def get_data():
    factual = Factual(KEY,SECRET)
    cn = Connection(MONGOURL)
    db = cn['factual']
    fields = "name,address,locality,region,postcode,country,category_labels,latitude,longitude"
    query = factual.table('places-v3').filters({"locality":"San Francisco"}).limit(50)
    try:
        for offset in xrange(0,10000,50):
            data = query.select(fields).offset(offset).data()
            inserted = db.places.insert(data)
            print "offset", offset, "inserted", len(inserted), "documents, sleeping for 60s"
            sleep(60)
    except APIException as e:
        print e
Esempio n. 26
0
def get_reservations_for_user(user_id):
    fb = firebase.FirebaseApplication(
        'https://feedthechildren.firebaseio.com/', None)
    factual = Factual('pFMeueZJQpyZnSPILemUPxzNJmathJyrxoqOnow0',
                      'ROHZOgzy9GwJGb9egKpzAVTYZq35iuj6f3Uu4rNu')
    exists = fb.get("/reservations/" + user_id,
                    None,
                    params={'print': 'pretty'},
                    headers={'X_FANCY_HEADER': 'VERY FANCY'})
    if exists is not None:
        for org in exists:
            for ean in exists[org]:
                quantity = exists[org][ean]
                nutrition = factual.table('products-cpg-nutrition').search(ean)
                resp = nutrition.data()[0]
                exists[org][ean] = {'quantity': quantity, 'metadata': resp}
        return jsonify(exists)
    return ""
Esempio n. 27
0
def get_product(upc_code):
    factual = Factual("gCKclwfy6eBki5UyHDxS56x7zmcvCMaGJ7l7v9cM", "Dt8V4ngb484Blmyaw5G9SxbycgpOsJL0ENckwxX0")
    products = factual.table('products')
    data = products.filters({'upc': {'$includes': upc_code}}).data()
    if data:
        upc_data = data[0]
        wanted = ['size', 'product_name', 'brand', 'image_urls']
        new = {}
        for pair in upc_data.items():
            if pair[0] in wanted:
                if pair[0] == 'product_name':
                    new['name'] = pair[1]
                elif pair[0] == 'size':
                    new['size'] = float(re.search(r'[\d\.]+', pair[1][0]).group())
                elif pair[0] == 'image_urls':
                    new['pic'] = pair[1][0]
                else:
                    new[pair[0]] = pair[1]
        new_json = json.dumps(new)
        return new_json, new['pic']
    else:
        return None, None
Esempio n. 28
0
def main():
	factual = Factual(KEY,SECRET)
	table = factual.table('places')
	s = factual.table('places').schema()

	#q1 = table.search("grocery new york city")

	#q2 = table.filters({'category_ids':{'$includes_any':[171,154,155,156,443]}}).data()
	#q2 = table.filters({'category_ids':{'$includes_any':[312,347]}}&geo={"$circle":{"$center":[35.953984, -79.011725],"$meters":5000}}.data()
	
	#data = table.search('Bop-n-grill').filters({'locality':'chicago'}).data()
##this is for china
#data = table.search(biz).filters({"$and":[{'locality':locality},{'country':country},{'category_ids':{'$includes_any':[312,347]}}]}).data()
	bizdata = []  #api returns list of dictionaries 
	#for i in range(0,3):
	for i in range(len(biz)):
			#search restaurants/bars in specified region and country 
		data = table.search(biz[i]).filters({"$and":[{'locality':locality[i]},{'country':country[i]},{'category_ids':{'$includes_any':[312,347]}}]}).data()
		bizdata.append(data)
		print "added " + biz[i]
		sleep(2)
		with open('RestaurantsADD.json', 'w') as outfile:
			json.dump(bizdata, outfile)
Esempio n. 29
0
def get_product(upc_code):
    factual = Factual("gCKclwfy6eBki5UyHDxS56x7zmcvCMaGJ7l7v9cM",
                      "Dt8V4ngb484Blmyaw5G9SxbycgpOsJL0ENckwxX0")
    products = factual.table('products')
    data = products.filters({'upc': {'$includes': upc_code}}).data()
    if data:
        upc_data = data[0]
        wanted = ['size', 'product_name', 'brand', 'image_urls']
        new = {}
        for pair in upc_data.items():
            if pair[0] in wanted:
                if pair[0] == 'product_name':
                    new['name'] = pair[1]
                elif pair[0] == 'size':
                    new['size'] = float(
                        re.search(r'[\d\.]+', pair[1][0]).group())
                elif pair[0] == 'image_urls':
                    new['pic'] = pair[1][0]
                else:
                    new[pair[0]] = pair[1]
        new_json = json.dumps(new)
        return new_json, new['pic']
    else:
        return None, None
    def __init__(self, location):
        self.dev = True
        self.location = location
        LOGGER.info(self.location)
        self.searchTerm = None
        self.getSearchTerm()
        self.results = {'place': self.formatTitle(self.searchTerm)}

        # Load API credentials
        with open('creds/cred.json') as f:
            self.creds = json.load(f)
            f.close()

        # Initialize factual
        self._factual = Factual(self.creds['FACTUAL_API_KEY'], self.creds['FACTUAL_API_SECRET'])
        self._places = self._factual.table('places')
Esempio n. 31
0
def task2(location, meal_of_the_day):
    """
    :param location: location around which the restaurants are looked up
    :param meal_of_the_day: the meal of the day, i.e., lunch, dinner, breakfast etc.
    :return: json body with 5 restaurants that meet the criteria
    # we can take many approaches to match the restaurants:
    # 1. We can take a attributes based approach, i.e., likes, dislikes, requirements of all individuals
    # 2. We can take an individual based approach, i.e., each person's likes, dislikes, and requirements at a time
    # 3. Possibly combine the weighted score of the first two approaches
    I am demonstrating a hybrid approach where the personal likes are checked for three people
    """
    factual = Factual(key=key, secret=secret)
    individuals = request.get_json()

    # get the nearest location
    nearest_location = find_smart_location(factual, location)

    if 'Error' in nearest_location:
        return Response(json.dumps({
            'Error':
            'No location of that name or postcode found. '
            'Please check the location or postcode name and try again.'
        }),
                        mimetype='application/json'), 400

    # initiate query with geo location search
    query = form_geo_query(factual,
                           lat=nearest_location['latitude'],
                           lon=nearest_location['longitude'])

    try:
        data = get_restaurants(individuals, meal_of_the_day, query)
    except:
        return Response(json.dumps({
            'Error':
            'An error occurred. Please try using one of breakfast, lunch, or dinner'
        }),
                        mimetype='application/json')
    if len(data) > 4:  # if at least 5 results are found, return
        return Response(json.dumps(data), mimetype='application/json')

    return Response(json.dumps(
        {'Error': 'No matching restaurant found. Please try again.'}),
                    mimetype='application/json'), 400
Esempio n. 32
0
def task1(location, search_string):
    """
    :param location: location around which the restaurants are required
    :param search_string: keyword searched, e.g., coffee, thai, indian
    :return: 5 restaurants or cafes ordered by distance from the place

    If the location returns multiple results, the location closest to the ip of the search device is used.
    If a unique location can't be found in such a situation, a regions based results can be returned.
    """
    factual = Factual(key=key, secret=secret)

    # get the nearest location
    nearest_location = find_smart_location(factual, location)

    if 'Error' in nearest_location:
        return Response(json.dumps({
            'Error':
            'No location of that name or postcode found. '
            'Please check the location or postcode name and try again.'
        }),
                        mimetype='application/json'), 400

    # form the initial query based on geo location
    query = form_geo_query(factual,
                           lat=nearest_location['latitude'],
                           lon=nearest_location['longitude'])
    # add the filters
    query = query.search('{}'.format(search_string)).filters(
        {"category_ids": {
            "$includes": 338
        }})
    data = query.data()

    if len(data):
        return Response(json.dumps(data), mimetype='application/json')
    else:
        return Response(json.dumps({
            'Error':
            'The search returned no match. Please change the location or search string and try again.'
        }),
                        mimetype='application/json'), 400
Esempio n. 33
0
class Database:
    def __init__(self, key, secret):
        self.key = key
        self.secret = secret
        self.db = Factual(key, secret)

    def schema(self, tableName):
        return self.db.table(tableName).schema()

    def table(self, tableName):
        return self.db.table(tableName)

    def search(self, tableName, searchContent):
        table = self.db.table(tableName)
        tableQuery = table.search(searchContent).include_count(True)
        return tableQuery.included_rows(), \
               tableQuery.total_row_count(), \
               tableQuery.data()

    def filter(self, tableName, condition):
        table = self.db.table(tableName)
        data = table.filters(condition).data()
        return data

    def search_filters(self, tableName, searchContent, condition):
        table = self.db.table(tableName)
        data = table.search(searchContent).filters(condition).data()
        return data

    def search_filters_paging(self, tableName, searchContent, condition,
                              pages):
        table = self.db.table(tableName)
        data = table.search(searchContent).filters(condition).offset(
            pages).limit(pages).data()
        return data

    def geofilters(self, tableName, searchContent, latitude, longitude,
                   radius):
        table = self.db.table(tableName)
        data = table.search(searchContent).geo(
            circle(latitude, longitude, radius)).data()
        return data
Esempio n. 34
0
class Database:

    def __init__(self, key, secret):
        self.key = key
        self.secret = secret
        self.db = Factual(key, secret)


    def schema(self, tableName):
        return self.db.table(tableName).schema()

    def table(self, tableName):
        return self.db.table(tableName)

    def search(self, tableName, searchContent):
        table = self.db.table(tableName)
        tableQuery = table.search(searchContent).include_count(True)
        return tableQuery.included_rows(), \
               tableQuery.total_row_count(), \
               tableQuery.data()

    def filter(self, tableName, condition):
        table = self.db.table(tableName)
        data = table.filters(condition).data()
        return data

    def search_filters(self, tableName, searchContent, condition):
        table = self.db.table(tableName)
        data = table.search(searchContent).filters(condition).data()
        return data

    def search_filters_paging(self, tableName, searchContent, condition, pages):
        table = self.db.table(tableName)
        data = table.search(searchContent).filters(condition).offset(pages).limit(pages).data()
        return data

    def geofilters(self, tableName, searchContent, latitude, longitude, radius):
        table = self.db.table(tableName)
        data = table.search(searchContent).geo(circle(latitude, longitude, radius)).data()
        return data
Esempio n. 35
0
 def setUp(self):
     self.factual = Factual(KEY, SECRET)
     self.places = self.factual.table("places")
     self.facets = self.factual.facets("global")
Esempio n. 36
0
class FactualAPITestSuite(unittest.TestCase):
    def setUp(self):
        self.factual = Factual(KEY, SECRET)
        self.places = self.factual.table("places")
        self.facets = self.factual.facets("global")

    def test_search(self):
        q = self.places.search("factual")
        row = q.data()[0]
        self.assertRegexpMatches(row["name"], "Factual")

    # full text search for "Mcdonald's, Santa Monica" (test spaces, commas, and apostrophes)
    def test_search2(self):
        q = self.places.search("McDonald's,Santa Monica").limit(20)
        included_rows = q.included_rows()
        self.assertEqual(20, included_rows)

    def test_limit(self):
        q = self.places.search("sushi").limit(3)
        self.assertEqual(3, len(q.data()))

    def test_select(self):
        q = self.places.select("name,address")
        row = q.data()[0]
        self.assertEqual(2, len(row.keys()))

    def test_sort(self):
        q1 = self.places.sort_asc("name")
        self.assertTrue(q1.data()[0]["name"] < q1.data()[1]["name"])

        q2 = self.places.sort_desc("name")
        self.assertTrue(q2.data()[0]["name"] > q2.data()[1]["name"])

    def test_paging(self):
        q1 = self.places.offset(30)
        r1 = q1.data()[0]

        q2 = self.places.page(3, 15)
        r2 = q2.data()[0]
        self.assertEqual(r1["name"], r2["name"])

    def test_filters(self):
        q = self.places.filters({"region": "NV"})
        for r in q.data():
            self.assertEqual("NV", r["region"])

    def test_geo(self):
        q = self.places.search("factual").geo(circle(34.06021, -118.41828, 1000))
        row = q.data()[0]
        self.assertEqual("Factual", row["name"])
        self.assertEqual("1801 Avenue Of The Stars", row["address"])

    def test_resolve(self):
        q = self.factual.resolve({"name": "factual inc", "locality": "los angeles"})
        row = q.data()[0]
        self.assertTrue(row["resolved"])
        self.assertEqual("1801 Avenue Of The Stars", row["address"])

    def test_schema(self):
        schema = self.places.schema()
        self.assertEqual(21, len(schema["fields"]))
        self.assertIn("title", schema)
        self.assertIn("locality", set(f["name"] for f in schema["fields"]))

    # full text search for things where locality equals 大阪市 (Osaka: test unicode)
    def test_unicode(self):
        q = self.places.filters({"locality": "大阪市"})
        for r in q.data():
            self.assertEqual("大阪市", r["locality"])

    def test_bw_encoding(self):
        q = self.places.filters({"category": {"$bw": "Arts, Entertainment & Nightlife > Bars"}})
        row = q.data()[0]
        self.assertRegexpMatches(row["category"], "Arts, Entertainment & Nightlife > Bars")

    def test_in(self):
        q = self.places.filters({"locality": {"$in": ["Santa Monica", "Los Angeles", "Culver City"]}})
        row = q.data()[0]
        self.assertIn(row["locality"], ["Santa Monica", "Los Angeles", "Culver City"])

    def test_and(self):
        q = self.places.filters({"$and": [{"country": "US"}, {"website": {"$blank": False}}]})
        row = q.data()[0]
        self.assertEqual("US", row["country"])
        self.assertRegexpMatches(row["website"], "http")

    def test_raw_read(self):
        # filters here is url encoded {"name":"Starbucks"}
        response = self.factual.raw_read("t/places/read", "limit=15&filters=%7B%22name%22%3A%22Starbucks%22%7D")
        payload = json.loads(response)
        data = payload["response"]["data"]
        self.assertEqual(15, payload["response"]["included_rows"])
        self.assertTrue(all(row["name"] == "Starbucks" for row in data))

    def test_facets1(self):
        q = self.facets.search("starbucks").select("country")
        results = q.data()["country"]
        self.assertTrue(results["us"] > 5000)
        self.assertTrue(results["ca"] > 200)

    def test_facets2(self):
        q = self.facets.search("starbucks").select("locality,region").filters({"country": "US"})
        locality = q.data()["locality"]
        region = q.data()["region"]
        self.assertTrue(locality["chicago"] > 50)
        self.assertTrue(region["tx"] > 200)

    def test_facets_geo(self):
        q = self.facets.select("category").geo(circle(34.06018, -118.41835, 5000))
        category = q.data()["category"]
        self.assertTrue(category["shopping"] > 1000)
        self.assertTrue(category["health & medicine > physicians"] > 1000)
Esempio n. 37
0
from factual import Factual

factual = Factual("pFMeueZJQpyZnSPILemUPxzNJmathJyrxoqOnow0", "ROHZOgzy9GwJGb9egKpzAVTYZq35iuj6f3Uu4rNu")
nutrition = factual.table("products-cpg-nutrition").search("0024100101481")
print nutrition.data()

nutrition = factual.table("products-cpg-nutrition")
Esempio n. 38
0
import csv

pp = pprint.PrettyPrinter(indent=2)

factual_ids = []
with open('factual_to_naics2012.tsv', 'r') as csvfile:
    factual_id_reader = csv.DictReader(csvfile, delimiter='\t')
    for row in factual_id_reader:
        factual_ids.append({
            'factual_id': int(row['factual_id']),
            'factual_name': row['factual_description']
        })

print factual_ids

factual_service = Factual('key', 'secret')

q = factual_service.table('places')

output_file = open('factual_counts.csv', 'w')

writer = csv.writer(output_file)

for fact_key in factual_ids:
    filter_query = q.filters({
        "$and": [{
            "category_ids": {
                '$includes': fact_key['factual_id']
            }
        }, {
            "region": "ma"
Esempio n. 39
0
import mykey
from factual import Factual  # compiled for python 3 from the forked rep: https://github.com/manonthemat/factual-python-driver

factual = Factual(mykey.OAuthKey, mykey.OAuthSecret)
acai = factual.table('products').search('acai').data()
for i, listitem in enumerate(acai):
    print('Product #' + str(i + 1))
    for k, v in listitem.items():
        if k == 'avg_price':
            print('Average price:', v)
        if k == 'product_name':
            print('Name:', v)
    print('*' * 20)
Esempio n. 40
0
class FactualAPITestSuite(unittest.TestCase):
    def setUp(self):
        self.factual = Factual(KEY, SECRET)
        self.places = self.factual.table('places')
        self.facets = self.factual.facets('global')

    def test_search(self):
        q = self.places.search('factual')
        row = q.data()[0]
        self.assertRegexpMatches(row['name'], 'Factual')

    # full text search for "Mcdonald's, Santa Monica" (test spaces, commas, and apostrophes)
    def test_search2(self):
        q = self.places.search("McDonald's,Santa Monica").limit(20)
        included_rows = q.included_rows()
        self.assertEqual(20, included_rows)

    def test_limit(self):
        q = self.places.search('sushi').limit(3)
        self.assertEqual(3, len(q.data()))

    def test_select(self):
        q = self.places.select('name,address')
        row = q.data()[0]
        self.assertEqual(2, len(row.keys()))

    def test_sort(self):
        q1 = self.places.sort_asc('name')
        self.assertTrue(q1.data()[0]['name'] < q1.data()[1]['name'])

        q2 = self.places.sort_desc('name')
        self.assertTrue(q2.data()[0]['name'] > q2.data()[1]['name'])

    def test_paging(self):
        q1 = self.places.offset(30)
        r1 = q1.data()[0]

        q2 = self.places.page(3, 15)
        r2 = q2.data()[0]
        self.assertEqual(r1['name'], r2['name'])

    def test_filters(self):
        q = self.places.filters({'region': 'NV'})
        for r in q.data():
            self.assertEqual('NV', r['region'])

    def test_geo(self):
        q = self.places.search('factual').geo(circle(34.06021, -118.41828, 1000))
        row = q.data()[0]
        self.assertEqual('Factual', row['name'])
        self.assertEqual('1801 Avenue Of The Stars', row['address'])

    def test_read_user(self):
        q = self.places.search('sushi').user('python_driver_tester')
        included_rows = q.included_rows()
        self.assertEqual(20, included_rows)

    def test_resolve(self):
        q = self.factual.resolve('places-v3', {'name': 'factual inc', 'locality': 'los angeles'})
        row = q.data()[0]
        self.assertTrue(row['resolved'])
        self.assertEqual('1801 Avenue Of The Stars', row['address'])

    def test_crosswalk(self):
        q = self.factual.crosswalk()
        result = q.filters({'factual_id':'03c26917-5d66-4de9-96bc-b13066173c65','namespace':'simplegeo'}).data()
        self.assertEqual(1, len(result))
        self.assertEqual('SG_3ueEyOH4YN3ob9ryHjV1ey', result[0]['namespace_id'])

    def test_schema(self):
        schema = self.places.schema()
        self.assertGreater(len(schema['fields']),20)
        self.assertIn('title', schema)
        self.assertIn('locality', set(f['name'] for f in schema['fields']))

    # full text search for things where locality equals 大阪市 (Osaka: test unicode)
    def test_unicode(self):
        q = self.places.filters({'locality': u'大阪市'})
        for r in q.data():
            self.assertEqual(u'大阪市', r['locality'])

    def test_bw_encoding(self):
        q = self.places.filters({'name': {"$bw":"Star"}})
        row = q.data()[0]
        self.assertRegexpMatches(row['name'], "Star")

    def test_in(self):
        q = self.places.filters({"locality":{"$in":["Santa Monica","Los Angeles","Culver City"]}})
        row = q.data()[0]
        self.assertIn(row['locality'], ["Santa Monica","Los Angeles","Culver City"])

    def test_and(self):
        q = self.places.filters({"$and":[{"country":"US"},{"website":{"$blank":False}}]})
        row = q.data()[0]
        self.assertEqual('US', row['country'].upper())
        self.assertRegexpMatches(row['website'], 'http')

    def test_includes(self):
        q = self.places.filters({'category_ids':{'$includes':10}})
        rows = q.data()
        for row in rows:
            self.assertIn(10, row['category_ids'])

    def test_raw_read(self):
        # filters here is url encoded {"name":"Starbucks"}
        response = self.factual.raw_read('t/places/read', 'limit=15&filters=%7B%22name%22%3A%22Starbucks%22%7D')
        payload = json.loads(response)
        data = payload['response']['data']
        self.assertEqual(15, payload['response']['included_rows'])
        self.assertTrue(all(row['name'] == 'Starbucks' for row in data))

    def test_raw_read_with_map(self):
        response = self.factual.raw_read('t/places/read', {'limit':15,'filters':{"name":"Starbucks"}})
        payload = json.loads(response)
        data = payload['response']['data']
        self.assertEqual(15, payload['response']['included_rows'])
        self.assertTrue(all(row['name'] == 'Starbucks' for row in data))

    def test_raw_write(self):
        uuid = '1007462b-dd79-44f5-a69f-e0b6041fa8bd'
        params = {'problem':'other','user':'******','debug':True}
        response = self.factual.raw_write('t/us-sandbox/' + uuid + '/flag', params)
        payload = json.loads(response)
        self.assertEqual('ok', payload['status'])

    def test_facets1(self):
        q = self.facets.search("starbucks").select("country")
        results = q.data()['country']
        self.assertTrue(results['us'] > 5000)
        self.assertTrue(results['ca'] > 200)

    def test_facets2(self):
        q = self.facets.search("starbucks").select("locality,region").filters({"country":"US"})
        locality = q.data()['locality']
        region = q.data()['region']
        self.assertTrue(locality['chicago'] > 50)
        self.assertTrue(region['tx'] > 200)

    def test_facets_geo(self):
        q = self.facets.select("locality").geo(circle(34.06018, -118.41835, 1000))
        locality = q.data()['locality']
        self.assertTrue(locality['los angeles'] > 3000)
        self.assertTrue(locality['beverly hills'] > 500)

    def test_geopulse(self):
        geopulse = self.factual.geopulse(point(34.06021, -118.41828))
        income_only = geopulse.select('income')
        all_results = geopulse.data()
        income_results = income_only.data()
        self.assertIn('demographics', all_results)
        demographics = all_results['demographics']
        self.assertIn('area_statistics', demographics)
        area_statistics = demographics['area_statistics']
        self.assertIn('commercial_residential', area_statistics)
        commercial_residential = area_statistics['commercial_residential']
        self.assertTrue(0 <= commercial_residential['business'] <= 100)
        income_demographics = income_results['demographics']
        self.assertEqual(1, len(income_demographics))
        self.assertIn('income', income_demographics)

    def test_geocode(self):
        geocode = self.factual.geocode(point(34.06021, -118.41828))
        result = geocode.data()[0]
        self.assertEqual('1801 Avenue Of The Stars', result['address'])
        self.assertLess(result['$distance'], 20)

    def test_monetize(self):
        monetize = self.factual.monetize().filters({'place_locality':'Los Angeles'})
        result = monetize.data()
        self.assertGreaterEqual(len(result), 1)
        self.assertTrue(all(row['place_locality'] == 'Los Angeles' for row in result))

    def test_submit_without_id(self):
        values = {'longitude': 100}
        submit = self.factual.submit('us-sandbox', values=values).user('python_driver_tester')
        response = submit.write()
        if 'new_entity' in response:
            self.assertTrue(response['new_entity'])
        else:
            self.assertIn('status', response)
            self.assertEqual('warning', response['status'])

    def test_submit_with_id(self):
        values = {'longitude': 100}
        submit = self.factual.submit('us-sandbox', factual_id='1007462b-dd79-44f5-a69f-e0b6041fa8bd', values=values).user('python_driver_tester')
        response = submit.write()
        if 'new_entity' in response:
            self.assertFalse(response['new_entity'])
        else:
            self.assertIn('status', response)
            self.assertEqual('warning', response['status'])

    def test_clear(self):
        clear = self.factual.clear('us-sandbox', '1007462b-dd79-44f5-a69f-e0b6041fa8bd', 'latitude,longitude').user('python_driver_tester')
        response = clear.write()
        self.assertIn('commit_id', response)
        self.assertGreater(len(response['commit_id']), 0)

    def test_flag(self):
        flag = self.factual.flag('us-sandbox', '1007462b-dd79-44f5-a69f-e0b6041fa8bd').user('python_driver_tester').other().debug(True)
        response = flag.write()
        self.assertEqual('ok', response['status'])

    def test_diffs(self):
        diffs = self.factual.diffs('2EH4Pz', 1339123455775, 1339136968687).data()
        self.assertGreater(len(diffs), 0)

    def test_diffs_streaming(self):
        diff_request = self.factual.diffs('2EH4Pz', 1339123455775, 1339136968687)
        batch = diff_request.data()
        streamed = list(diff_request.stream())
        self.assertItemsEqual(batch, streamed)

    def test_match(self):
        match = self.factual.match('places-v3', {'name':'McDonalds','address':'10451 Santa Monica Blvd','locality':'Los Angeles','region':'CA'})
        match_id = match.get_id()
        self.assertEqual('c730d193-ba4d-4e98-8620-29c672f2f117', match_id)

    def test_multi(self):
        q1 = self.factual.table('places').filters({'postcode':'90067'})
        q2 = self.factual.facets('places').filters({'postcode':'90067'}).select('category_labels')
        response = self.factual.multi({'query1':q1,'query2':q2})
        self.assertTrue('query1' in response and 'query2' in response)

    def test_get_row(self):
        row = self.factual.get_row('places', '03c26917-5d66-4de9-96bc-b13066173c65')
        self.assertEqual('Factual', row['name'])
Esempio n. 41
0
 def __init__(self, key, secret):
     self.factual = Factual(key, secret)
Esempio n. 42
0
 def __init__(self):
     factual_id, factual_secret = DATASOURCES['factual']['app_id'], DATASOURCES['factual']['app_secret']
     places = Factual(factual_id, factual_secret)
     self.places = places.table('places')
Esempio n. 43
0
class FactualAPITestSuite(unittest.TestCase):
    def setUp(self):
        self.factual = Factual(KEY, SECRET)
        self.places = self.factual.table('places')
        self.facets = self.factual.facets('global')

    def test_search(self):
        q = self.places.search('factual')
        row = q.data()[0]
        self.assertRegexpMatches(row['name'], 'Factual')

    # full text search for "Mcdonald's, Santa Monica" (test spaces, commas, and apostrophes)
    def test_search2(self):
        q = self.places.search("McDonald's,Santa Monica").limit(20)
        included_rows = q.included_rows()
        self.assertEqual(20, included_rows)

    def test_limit(self):
        q = self.places.search('sushi').limit(3)
        self.assertEqual(3, len(q.data()))

    def test_select(self):
        q = self.places.select('name,address')
        row = q.data()[0]
        self.assertEqual(2, len(row.keys()))

    def test_sort(self):
        q1 = self.places.sort_asc('name')
        self.assertTrue(q1.data()[0]['name'] < q1.data()[1]['name'])

        q2 = self.places.sort_desc('name')
        self.assertTrue(q2.data()[0]['name'] > q2.data()[1]['name'])

    def test_paging(self):
        q1 = self.places.offset(30)
        r1 = q1.data()[0]

        q2 = self.places.page(3, 15)
        r2 = q2.data()[0]
        self.assertEqual(r1['name'], r2['name'])

    def test_filters(self):
        q = self.places.filters({'region': 'NV'})
        for r in q.data():
            self.assertEqual('NV', r['region'])

    def test_geo(self):
        q = self.places.search('factual').geo(
            circle(34.06021, -118.41828, 1000))
        row = q.data()[0]
        self.assertEqual('Factual', row['name'])
        self.assertEqual('1801 Avenue Of The Stars', row['address'])

    def test_read_user(self):
        q = self.places.search('sushi').user('python_driver_tester')
        included_rows = q.included_rows()
        self.assertEqual(20, included_rows)

    def test_resolve(self):
        q = self.factual.resolve('places-v3', {
            'name': 'factual inc',
            'locality': 'los angeles'
        })
        row = q.data()[0]
        self.assertTrue(row['resolved'])
        self.assertEqual('1801 Avenue Of The Stars', row['address'])

    def test_crosswalk(self):
        q = self.factual.crosswalk()
        result = q.filters({
            'factual_id': '03c26917-5d66-4de9-96bc-b13066173c65',
            'namespace': 'simplegeo'
        }).data()
        self.assertEqual(1, len(result))
        self.assertEqual('SG_3ueEyOH4YN3ob9ryHjV1ey',
                         result[0]['namespace_id'])

    def test_schema(self):
        schema = self.places.schema()
        self.assertGreater(len(schema['fields']), 20)
        self.assertIn('title', schema)
        self.assertIn('locality', set(f['name'] for f in schema['fields']))

    # full text search for things where locality equals 大阪市 (Osaka: test unicode)
    def test_unicode(self):
        q = self.places.filters({'locality': u'大阪市'})
        for r in q.data():
            self.assertEqual(u'大阪市', r['locality'])

    def test_bw_encoding(self):
        q = self.places.filters({'name': {"$bw": "Star"}})
        row = q.data()[0]
        self.assertRegexpMatches(row['name'], "Star")

    def test_in(self):
        q = self.places.filters({
            "locality": {
                "$in": ["Santa Monica", "Los Angeles", "Culver City"]
            }
        })
        row = q.data()[0]
        self.assertIn(row['locality'],
                      ["Santa Monica", "Los Angeles", "Culver City"])

    def test_and(self):
        q = self.places.filters(
            {"$and": [{
                "country": "US"
            }, {
                "website": {
                    "$blank": False
                }
            }]})
        row = q.data()[0]
        self.assertEqual('US', row['country'].upper())
        self.assertRegexpMatches(row['website'], 'http')

    def test_includes(self):
        q = self.places.filters({'category_ids': {'$includes': 10}})
        rows = q.data()
        for row in rows:
            self.assertIn(10, row['category_ids'])

    def test_raw_read(self):
        # filters here is url encoded {"name":"Starbucks"}
        response = self.factual.raw_read(
            't/places/read',
            'limit=15&filters=%7B%22name%22%3A%22Starbucks%22%7D')
        payload = json.loads(response)
        data = payload['response']['data']
        self.assertEqual(15, payload['response']['included_rows'])
        self.assertTrue(all(row['name'] == 'Starbucks' for row in data))

    def test_raw_read_with_map(self):
        response = self.factual.raw_read('t/places/read', {
            'limit': 15,
            'filters': {
                "name": "Starbucks"
            }
        })
        payload = json.loads(response)
        data = payload['response']['data']
        self.assertEqual(15, payload['response']['included_rows'])
        self.assertTrue(all(row['name'] == 'Starbucks' for row in data))

    def test_raw_write(self):
        uuid = '1007462b-dd79-44f5-a69f-e0b6041fa8bd'
        params = {
            'problem': 'other',
            'user': '******',
            'debug': True
        }
        response = self.factual.raw_write('t/us-sandbox/' + uuid + '/flag',
                                          params)
        payload = json.loads(response)
        self.assertEqual('ok', payload['status'])

    def test_facets1(self):
        q = self.facets.search("starbucks").select("country")
        results = q.data()['country']
        self.assertTrue(results['us'] > 5000)
        self.assertTrue(results['ca'] > 200)

    def test_facets2(self):
        q = self.facets.search("starbucks").select("locality,region").filters(
            {"country": "US"})
        locality = q.data()['locality']
        region = q.data()['region']
        self.assertTrue(locality['chicago'] > 50)
        self.assertTrue(region['tx'] > 200)

    def test_facets_geo(self):
        q = self.facets.select("locality").geo(
            circle(34.06018, -118.41835, 1000))
        locality = q.data()['locality']
        self.assertTrue(locality['los angeles'] > 3000)
        self.assertTrue(locality['beverly hills'] > 500)

    def test_geopulse(self):
        geopulse = self.factual.geopulse(point(34.06021, -118.41828))
        income_only = geopulse.select('income')
        all_results = geopulse.data()
        income_results = income_only.data()
        self.assertIn('demographics', all_results)
        demographics = all_results['demographics']
        self.assertIn('area_statistics', demographics)
        area_statistics = demographics['area_statistics']
        self.assertIn('commercial_residential', area_statistics)
        commercial_residential = area_statistics['commercial_residential']
        self.assertTrue(0 <= commercial_residential['business'] <= 100)
        income_demographics = income_results['demographics']
        self.assertEqual(1, len(income_demographics))
        self.assertIn('income', income_demographics)

    def test_geocode(self):
        geocode = self.factual.geocode(point(34.06021, -118.41828))
        result = geocode.data()[0]
        self.assertEqual('1801 Avenue Of The Stars', result['address'])
        self.assertLess(result['$distance'], 20)

    def test_monetize(self):
        monetize = self.factual.monetize().filters(
            {'place_locality': 'Los Angeles'})
        result = monetize.data()
        self.assertGreaterEqual(len(result), 1)
        self.assertTrue(
            all(row['place_locality'] == 'Los Angeles' for row in result))

    def test_submit_without_id(self):
        values = {'longitude': 100}
        submit = self.factual.submit(
            'us-sandbox', values=values).user('python_driver_tester')
        response = submit.write()
        if 'new_entity' in response:
            self.assertTrue(response['new_entity'])
        else:
            self.assertIn('status', response)
            self.assertEqual('warning', response['status'])

    def test_submit_with_id(self):
        values = {'longitude': 100}
        submit = self.factual.submit(
            'us-sandbox',
            factual_id='1007462b-dd79-44f5-a69f-e0b6041fa8bd',
            values=values).user('python_driver_tester')
        response = submit.write()
        if 'new_entity' in response:
            self.assertFalse(response['new_entity'])
        else:
            self.assertIn('status', response)
            self.assertEqual('warning', response['status'])

    def test_clear(self):
        clear = self.factual.clear(
            'us-sandbox', '1007462b-dd79-44f5-a69f-e0b6041fa8bd',
            'latitude,longitude').user('python_driver_tester')
        response = clear.write()
        self.assertIn('commit_id', response)
        self.assertGreater(len(response['commit_id']), 0)

    def test_flag(self):
        flag = self.factual.flag(
            'us-sandbox', '1007462b-dd79-44f5-a69f-e0b6041fa8bd').user(
                'python_driver_tester').other().debug(True)
        response = flag.write()
        self.assertEqual('ok', response['status'])

    def test_diffs(self):
        diffs = self.factual.diffs('2EH4Pz', 1339123455775,
                                   1339136968687).data()
        self.assertGreater(len(diffs), 0)

    def test_diffs_streaming(self):
        diff_request = self.factual.diffs('2EH4Pz', 1339123455775,
                                          1339136968687)
        batch = diff_request.data()
        streamed = list(diff_request.stream())
        self.assertItemsEqual(batch, streamed)

    def test_match(self):
        match = self.factual.match(
            'places-v3', {
                'name': 'McDonalds',
                'address': '10451 Santa Monica Blvd',
                'locality': 'Los Angeles',
                'region': 'CA'
            })
        match_id = match.get_id()
        self.assertEqual('c730d193-ba4d-4e98-8620-29c672f2f117', match_id)

    def test_multi(self):
        q1 = self.factual.table('places').filters({'postcode': '90067'})
        q2 = self.factual.facets('places').filters({
            'postcode': '90067'
        }).select('category_labels')
        response = self.factual.multi({'query1': q1, 'query2': q2})
        self.assertTrue('query1' in response and 'query2' in response)

    def test_get_row(self):
        row = self.factual.get_row('places',
                                   '03c26917-5d66-4de9-96bc-b13066173c65')
        self.assertEqual('Factual', row['name'])
Esempio n. 44
0
from factual import Factual
from django.utils.encoding import smart_str
import pandas as pd
import string
import json

# set up api
factual = Factual('HShWyOdPbw6OdXNEvR1ZR4px2RbGC95xvKbpgUTV',
                  'xN2HcgnG6nueorqRw584jHUSf3SIg5BJriFoNyc5')

# UK postalcodes
postalcodes = pd.read_csv('/Users/artsy/Desktop/factual/UK/UK_codes.csv')
postalcodes = postalcodes['Postalcode'][0:]

# country codes
countries = {
    'United States': 'places-us',
    'China': 'places-cn',
    'Japan': 'places-jp',
    'Germany': 'places-de',
    'Italy': 'places-it',
    'France': 'places-fr',
    'Spain': 'places-es',
    'United Kingdom': 'places-gb',
    'Brazil': 'places-br',
    'Canada': 'places-ca',
    'Australia': 'places-au',
    'South Korea': 'places-kr',
    'Portugal': 'places-pt',
    'Taiwan': 'places-tw',
    'Switzerland': 'places-ch',
Esempio n. 45
0
from geopy.geocoders import Nominatim
import decimal
from factual import Factual
from factual.utils import circle
import json
from datetime import date
import calendar
import datetime

my_date = date.today()
my_day = calendar.day_name[my_date.weekday()]
my_day = my_day.lower()
factual = Factual('OAwHffzwwkan9LpstDBhOGOGmit7plcERqZpvE2J',
                  'gw28zCrtijknGrGDm9dVzC1v8HyFHi0HbEtGacL7',
                  timeout=10.0)
s = factual.table('places').schema()
places = factual.table('places')
a = raw_input("Location A: ")
b = raw_input("Location B: ")
c = raw_input("What are you looking for: ")


#d = raw_input("Proximity from the midpoint (mtrs): ")
def findMidPlace(a, b, c, d):
    geolocator = Nominatim()
    locationA = geolocator.geocode(a)
    locationB = geolocator.geocode(b)
    midpointLat = (locationA.latitude + locationB.latitude) / 2
    print midpointLat
    midpointLong = (locationA.longitude + locationB.longitude) / 2
    print midpointLong
Esempio n. 46
0
import sys,json;
sys.path.append('C:\Python27\Lib\site-packages')

Rname=[]
hrateRes=[]
numbers=["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine","ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"]

from factual import Factual
factual = Factual('ke8x5LeW6GVnU1aPjTUvJS5Sr12YT2aTA9dDc18r', '4eDL2G2HlRm6ZOTkvxkj2xg86BRgOGUTVLoC5OsF')   #[email protected]
#factual = Factual('O7OPvoeCZbUC3Zsy5qld8zZIuZX99w0SKkT6APy8','V7eHUV01tJUyFqFcaLZrXN8cnIEGbUmdyE1ZO2KB')    #[email protected]

#print("Hello")
#places = factual.table('places')


cuisine="Wings"
postcode="90007"
rating=4   #possible values 1-5
price="expensive"    #possible values Cheap, moderate, Expensive
locality="Seattle"

if(price.lower()=="expensive"):
    price =5
elif(price.lower()=="cheap"):
    price =1

elif(price.lower()=="moderate"):
    price =3

Esempio n. 47
0
from factual import Factual
import datetime
import sys
import csv
import googlemaps
import os
from googleplaces import GooglePlaces, types, lang
from datetime import datetime as dt
from dateutil import tz
import random

google_places = GooglePlaces("AIzaSyDuSfwq0Nli3CzitI3SZob0t90dprS8JiQ")
factual = Factual("gfdD2lYBQ21Cs5M9eRpdEZCgDDswPvDzfeFOqYko", "qkWK3Bv2wK7Jpz4e5JCSlKQVANbev8FHsCkoTSxZ")
places = factual.table("places")
gmaps = googlemaps.Client(key="AIzaSyDuSfwq0Nli3CzitI3SZob0t90dprS8JiQ")


def run_recurse(input1):
    file_location = os.getcwd()
    f = open(file_location + "/templates/pages/results_cluster.html", "w")
    f.write("{%  extends 'layouts/main.html' %}\n")
    f.write("{%  block title %}Search Results{%  endblock %}\n")
    f.write("{%  block content %}\n")

    ##Start the initial javascript to hide the driving instructions
    f.write('<script type="text/javascript">\n')
    f.write("function toggle(obj) {\n")
    f.write("var obj=document.getElementById(obj);\n")
    f.write('if (obj.style.display == "block") obj.style.display = "none";\n')
    f.write('else obj.style.display = "block";\n')
    f.write("}\n")
Esempio n. 48
0
 def setUp(self):
     self.factual = Factual(KEY, SECRET)
     self.places = self.factual.table('places')
     self.facets = self.factual.facets('global')
Esempio n. 49
0
import googlemaps
from factual import Factual
from datetime import datetime as dt
import sys
import datetime
import inspect
from factual.utils import circle
from dateutil import tz
import random
import os
import pytz

factual = Factual('gfdD2lYBQ21Cs5M9eRpdEZCgDDswPvDzfeFOqYko','qkWK3Bv2wK7Jpz4e5JCSlKQVANbev8FHsCkoTSxZ')
gmaps = googlemaps.Client(key = 'AIzaSyDuSfwq0Nli3CzitI3SZob0t90dprS8JiQ')

places = factual.table('places')


def printresults_food(data,f, counter):

	identifier = "places"+str(counter)

	f.write("<a href=\"javascript: void(0);\" onClick=\"toggle('"+identifier+"')\">Click for Name and Phone Number</a>\n")


	track = str(data[u'category_labels'][0][-1])
	name = str(data[u'name'])

	try:
		telephone = str(data[u'tel'])
	except:
Esempio n. 50
0
 def setUp(self):
     self.factual = Factual(KEY, SECRET)
     self.places = self.factual.table('places')
     self.facets = self.factual.facets('global')
Esempio n. 51
0
    instructions = wtf.TextAreaField('Instructions')


#####################################
# FACTUAL API ACCESS FOR ASSIGNMENT 4
#####################################

# load creds from client_secrets.json
secrets_file = open('client_secrets.json', 'r')
factual_creds = json.loads(secrets_file.read())
secrets_file.close()

api_key = factual_creds['api_key']
api_secret = factual_creds['api_secret']

factual = Factual(api_key, api_secret)


# route to hit Factual api
# based on Factual documentation: https://github.com/Factual/factual-python-driver
@app.route('/mobile/factual', methods=['POST'])
def factual_restaurants():
    latitude = float(request.form.get('latitude'))
    longitude = float(request.form.get('longitude'))
    search_term = request.form.get('search_term')
    if not latitude or not longitude:
        return json.dumps({"error": "No location provided"}), 500
    if not search_term:
        return json.dumps({"error": "No search term provided"}), 500

    radius = 1000
Esempio n. 52
0
class FactualAPITestSuite(unittest.TestCase):
    def setUp(self):
        self.factual = Factual(KEY, SECRET)
        self.places = self.factual.table('places')
        self.facets = self.factual.facets('global')

    def test_search(self):
        q = self.places.search('factual')
        row = q.data()[0]
        self.assertRegexpMatches(row['name'], 'Factual')

    # full text search for "Mcdonald's, Santa Monica" (test spaces, commas, and apostrophes)
    def test_search2(self):
        q = self.places.search("McDonald's,Santa Monica").limit(20)
        included_rows = q.included_rows()
        self.assertEqual(20, included_rows)

    def test_limit(self):
        q = self.places.search('sushi').limit(3)
        self.assertEqual(3, len(q.data()))

    def test_select(self):
        q = self.places.select('name,address')
        row = q.data()[0]
        self.assertEqual(2, len(row.keys()))

    def test_sort(self):
        q1 = self.places.sort_asc('name')
        self.assertTrue(q1.data()[0]['name'] < q1.data()[1]['name'])

        q2 = self.places.sort_desc('name')
        self.assertTrue(q2.data()[0]['name'] > q2.data()[1]['name'])

    def test_paging(self):
        q1 = self.places.offset(30)
        r1 = q1.data()[0]

        q2 = self.places.page(3, 15)
        r2 = q2.data()[0]
        self.assertEqual(r1['name'], r2['name'])

    def test_filters(self):
        q = self.places.filters({'region': 'NV'})
        for r in q.data():
            self.assertEqual('NV', r['region'])

    def test_geo(self):
        q = self.places.search('factual').geo(
            circle(34.06021, -118.41828, 1000))
        row = q.data()[0]
        self.assertEqual('Factual', row['name'])
        self.assertEqual('1999 Avenue Of The Stars', row['address'])

    def test_read_user(self):
        q = self.places.search('sushi').user('python_driver_tester')
        included_rows = q.included_rows()
        self.assertEqual(20, included_rows)

    def test_resolve(self):
        q = self.factual.resolve('places-us', {
            "name": "factual inc",
            "locality": "los angeles",
            "postcode": "90067"
        })
        row = q.data()[0]
        self.assertTrue(row['resolved'])
        self.assertEqual('1999 Avenue Of The Stars', row['address'])

    def test_resolve_debug(self):
        q = self.factual.resolve('places-v3', {
            'name': 'factual inc',
            'locality': 'los angeles'
        },
                                 debug=True)
        row = q.data()[0]
        self.assertIn('similarity', row)

    def test_crosswalk(self):
        q = self.factual.crosswalk()
        result = q.filters({
            'factual_id': FACTUAL_UUID,
            'namespace': 'facebook'
        }).data()
        self.assertGreaterEqual(len(result), 1)
        self.assertEqual('151202829333', result[0]['namespace_id'])

    def test_schema(self):
        schema = self.places.schema()
        self.assertGreater(len(schema['fields']), 20)
        self.assertIn('title', schema)
        self.assertIn('locality', set(f['name'] for f in schema['fields']))

    # full text search for things where locality equals 大阪市 (Osaka: test unicode)
    def test_unicode(self):
        q = self.places.filters({'locality': u'大阪市'})
        for r in q.data():
            self.assertEqual(u'大阪市', r['locality'])

    def test_bw_encoding(self):
        q = self.places.filters({'name': {"$bw": "Star"}})
        row = q.data()[0]
        self.assertRegexpMatches(row['name'], "Star")

    def test_in(self):
        q = self.places.filters({
            "locality": {
                "$in": ["Santa Monica", "Los Angeles", "Culver City"]
            }
        })
        row = q.data()[0]
        self.assertIn(row['locality'],
                      ["Santa Monica", "Los Angeles", "Culver City"])

    def test_and(self):
        q = self.places.filters(
            {"$and": [{
                "country": "US"
            }, {
                "website": {
                    "$blank": False
                }
            }]})
        row = q.data()[0]
        self.assertEqual('US', row['country'].upper())
        self.assertRegexpMatches(row['website'], 'http')

    def test_includes(self):
        q = self.places.filters({'category_ids': {'$includes': 10}})
        rows = q.data()
        for row in rows:
            self.assertIn(10, row['category_ids'])

    def test_raw_read(self):
        # filters here is url encoded {"name":"Starbucks"}
        response = self.factual.raw_read(
            't/places/read',
            'limit=15&filters=%7B%22name%22%3A%22Starbucks%22%7D')
        payload = json.loads(response)
        data = payload['response']['data']
        self.assertEqual(15, payload['response']['included_rows'])
        self.assertTrue(all(row['name'] == 'Starbucks' for row in data))

    def test_raw_read_with_map(self):
        response = self.factual.raw_read('t/places/read', {
            'limit': 15,
            'filters': {
                "name": "Starbucks"
            }
        })
        payload = json.loads(response)
        data = payload['response']['data']
        self.assertEqual(15, payload['response']['included_rows'])
        self.assertTrue(all(row['name'] == 'Starbucks' for row in data))

    def test_raw_write(self):
        uuid = SANDBOX_UUID
        params = {
            'problem': 'other',
            'user': '******',
            'debug': True
        }
        response = self.factual.raw_write('t/us-sandbox/' + uuid + '/flag',
                                          params)
        payload = json.loads(response)
        self.assertEqual('ok', payload['status'])

    def test_facets1(self):
        q = self.facets.search("starbucks").select("country")
        results = q.data()['country']
        self.assertTrue(results['us'] > 5000)
        self.assertTrue(results['ca'] > 200)

    def test_facets2(self):
        q = self.facets.search("starbucks").select("locality,region").filters(
            {"country": "US"})
        locality = q.data()['locality']
        region = q.data()['region']
        self.assertTrue(locality['chicago'] > 50)
        self.assertTrue(region['tx'] > 200)

    def test_facets_geo(self):
        q = self.facets.select("locality").geo(
            circle(34.06018, -118.41835, 1000))
        locality = q.data()['locality']
        self.assertTrue(locality['los angeles'] > 3000)
        self.assertTrue(locality['beverly hills'] > 500)

    def test_geocode(self):
        geocode = self.factual.geocode(point(34.058744, -118.416937))
        result = geocode.data()[0]
        self.assertEqual('1999 Avenue Of The Stars', result['address'])
        self.assertLess(result['$distance'], 20)

    def test_submit_without_id(self):
        values = {'longitude': 100}
        submit = self.factual.submit(
            'us-sandbox', values=values).user('python_driver_tester')
        response = submit.write()
        if 'new_entity' in response:
            self.assertTrue(response['new_entity'])
        else:
            self.assertIn('status', response)
            self.assertEqual('warning', response['status'])

    def test_submit_with_id(self):
        values = {'longitude': 100}
        submit = self.factual.submit(
            'us-sandbox', factual_id=SANDBOX_UUID,
            values=values).user('python_driver_tester')
        response = submit.write()
        if 'new_entity' in response:
            self.assertFalse(response['new_entity'])
        else:
            self.assertIn('status', response)
            self.assertEqual('warning', response['status'])

    def test_clear(self):
        clear = self.factual.clear(
            'us-sandbox', SANDBOX_UUID,
            'latitude,longitude').user('python_driver_tester')
        response = clear.write()
        self.assertIn('commit_id', response)
        self.assertGreater(len(response['commit_id']), 0)

    def test_flag(self):
        flag = self.factual.flag(
            'us-sandbox',
            SANDBOX_UUID).user('python_driver_tester').other().debug(True)
        response = flag.write()
        self.assertEqual('ok', response['status'])

    def test_flag_preferred(self):
        flag = self.factual.flag('us-sandbox', SANDBOX_UUID).duplicate(
            FACTUAL_UUID).user('python_driver_tester').debug(True)
        response = flag.write()
        self.assertEqual('ok', response['status'])

    def test_diffs(self):
        diffs = self.factual.diffs('places-us', 1400788800000,
                                   1400792400000).data()
        self.assertGreater(len(diffs), 0)

    def test_diffs_streaming(self):
        diff_request = self.factual.diffs('places-us', 1400788800000,
                                          1400792400000)
        batch = diff_request.data()
        streamed = list(diff_request.stream())
        self.assertSequenceEqual(batch, streamed)

    def test_match(self):
        match = self.factual.match(
            'places-us', {
                'name': 'McDonalds',
                'address': '10451 Santa Monica Blvd',
                'locality': 'Los Angeles',
                'region': 'CA'
            })
        match_id = match.get_id()
        self.assertEqual('c730d193-ba4d-4e98-8620-29c672f2f117', match_id)

    def test_multi(self):
        q1 = self.factual.table('places').filters({'postcode': '90067'})
        q2 = self.factual.facets('places').filters({
            'postcode': '90067'
        }).select('category_labels')
        response = self.factual.multi({'query1': q1, 'query2': q2})
        self.assertTrue('query1' in response and 'query2' in response)

    def test_get_row(self):
        row = self.factual.get_row('places', FACTUAL_UUID)
        self.assertEqual('Factual', row['name'])

    def test_boost(self):
        boost = self.factual.boost('us-sandbox',
                                   SANDBOX_UUID).user('python_driver_tester')
        response = boost.write()
        self.assertEqual('ok', response['status'])
Esempio n. 53
0
import io
import json

from factual import Factual

from yelp.client import Client
from yelp.oauth1_authenticator import Oauth1Authenticator

with io.open('keys.local.json') as cred:
    creds = json.load(cred)
    yelpCreds = creds["yelp"]
    auth = Oauth1Authenticator(**yelpCreds)
    yelpClient = Client(auth)
    factualCreds = creds["factual"]
    factualClient = Factual(factualCreds["key"], factualCreds["secret"])
    googleapikey = creds["googleapikey"]

import sys, json
sys.path.append('C:\Python27\Lib\site-packages')

Rname = []
hrateRes = []
numbers = [
    "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
    "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
    "sixteen", "seventeen", "eighteen", "nineteen", "twenty"
]

from factual import Factual
#factual = Factual('ke8x5LeW6GVnU1aPjTUvJS5Sr12YT2aTA9dDc18r', '4eDL2G2HlRm6ZOTkvxkj2xg86BRgOGUTVLoC5OsF')   #[email protected]
factual = Factual(
    'O7OPvoeCZbUC3Zsy5qld8zZIuZX99w0SKkT6APy8',
    'V7eHUV01tJUyFqFcaLZrXN8cnIEGbUmdyE1ZO2KB')  #[email protected]

#print("Hello")
#places = factual.table('places')

cuisine = "Italian"
postcode = "90007"
rating = 4  #possible values 1-5
price = "expensive"  #possible values Cheap, moderate, Expensive

if (price.lower() == "expensive"):
    price = 5
elif (price.lower() == "cheap"):
    price = 1

elif (price.lower() == "moderate"):
Esempio n. 55
0
from factual import Factual
import pandas as pd
import os

# factual API credentials
factual = Factual(os.environ["FACTUAL_KEY"], os.environ["FACTUAL_SECRET"])
tbl = factual.table('products')

data = []
for cat in ["Pet Care", "Frozen Foods", "Hair Shampoo", "Soda"]:
    q = tbl.filters({"category": cat })
    for i in range(1, 15):
        q = q.page(2)
        data = data + q.data()

df = pd.DataFrame(data)
# df.to_csv("product_data.csv", index=False)
df[['product_name', 'category']].to_csv("./products.csv", index=False)
Esempio n. 56
0
import googlemaps
from factual import Factual
from datetime import datetime as dt
import sys
import datetime
import inspect
from factual.utils import circle
from dateutil import tz
import random
import os
import pytz

factual = Factual('gfdD2lYBQ21Cs5M9eRpdEZCgDDswPvDzfeFOqYko',
                  'qkWK3Bv2wK7Jpz4e5JCSlKQVANbev8FHsCkoTSxZ')
gmaps = googlemaps.Client(key='AIzaSyDuSfwq0Nli3CzitI3SZob0t90dprS8JiQ')

places = factual.table('places')


def printresults_food(data, f, counter):

    identifier = "places" + str(counter)

    f.write("<a href=\"javascript: void(0);\" onClick=\"toggle('" +
            identifier + "')\">Click for Name and Phone Number</a>\n")

    track = str(data[u'category_labels'][0][-1])
    name = str(data[u'name'])

    try:
        telephone = str(data[u'tel'])
Esempio n. 57
0
 def __init__(self):
     factual_id, factual_secret = DATASOURCES['factual'][
         'app_id'], DATASOURCES['factual']['app_secret']
     places = Factual(factual_id, factual_secret)
     self.places = places.table('places')