def create_promo(promotion_request): """ :param promotion: (json object) promotion information as a JSON object """ db_promo = { "name": promotion_request['name'], "type": promotion_request["type"], "month": promotion_request["month"], "minimum": promotion_request["minimum"], "desc": promotion_request["desc"], "discount_percent": promotion_request["discount_percent"], "qr_code_id": promotion_request["qr_code_id"] } categories = yelp.search(promotion_request["name"]) categories = categories['categories'] categories = [category["alias"] for category in categories] db_promo["categories"] = categories db_promo["curr_amount"] = 0 db_promo["qualify"] = "false" # push to database result = promos.insert_one(db_promo) # return promotion_request return db_promo
def trans_add_category(transaction): """ Populates the "categories" key in the transaction json and pushes to db """ # find name in yelp name = preprocess(transaction["name"]) name = match(name) if name[1] < 90: # try again w/ autocomplete print("Confidence not high enough, trying again") autocomplete = yelp.autocomplete(name[0]) if autocomplete != None: matches = [result["text"] for result in autocomplete] best = match(name[0], matchlist=matches) else: best = ("", 0) if name[1] < 90: print("No close matches for {}".format(name)) return False else: name = name[0] categories = yelp.search(name)["businesses"][0] categories = categories['categories'] categories = [category["alias"] for category in categories] transaction["categories"] = categories return transaction
def do_restaurant_search(): """Get search results using Yelp API.""" search_term = request.args.get('term') username = request.args.get('username') user_location = get_user_location(username) city = user_location[0] state = user_location[1] search_location = city + ', ' + state results = search(search_term, search_location) business_results = results['businesses'] results_dict = {'rests': []} for item in business_results: results_dict['rests'].append({ 'name': item['name'], 'id': item['id'], 'location': item['location']['display_address'][0] }) return jsonify(results_dict)
def run_model(): # Generate the list of restaurants in the community and return a list term = request.args.get('user_search_term') yelp_call = search(term=term, location='San Francisco') restaurants = yelp_call['businesses']#[0]['id'] name_id = [res['id'] for res in restaurants] business_ids = name_id # Generate basic restaurant statistics ratings = business_ratings[name_id[0]] votes = business_votes[name_id[0]] # Generate the network graph somehow... return jsonify(business_ids=business_ids, ratings=ratings, votes=votes)
def yelp_search(): # user_request = str(request.form['restaurant_id']) term = request.args.get('user_search_term') print term yelp_call = search(term=term, location='San Francisco') restaurants = yelp_call['businesses']#[0]['id'] name_id = [res['name'] for res in restaurants if res['review_count']>=500] img = [res['image_url'] for res in restaurants if res['review_count']>=500] url = [res['url'] for res in restaurants if res['review_count']>=500] rating = [res['rating'] for res in restaurants if res['review_count']>=500] lat = [res['location']['coordinate']['latitude'] for res in restaurants if res['review_count']>=500] log = [res['location']['coordinate']['longitude'] for res in restaurants if res['review_count']>=500] print name_id return jsonify(name_id=name_id, img=img, url=url, rating=rating, lat=lat, log=log)
def get_yelp_url(restaurant_name, location, consumer_key, consumer_secret, token, token_secret): response = yelp_api.search(query=restaurant_name, location=location, consumer_key=consumer_key, consumer_secret=consumer_secret, token=token, token_secret=token_secret) # We assume that the 1st search result will be # the corresponding restaurant on Yelp if 'businesses' in response and response['businesses']: return response['businesses'][0]['url'] else: return ''
def match(name, matchlist=None): """ Matches given name to business name from Yelp search """ if name == None: return ("", 0) try: stores = yelp.search(name) if matchlist == None: matches = [] for store in stores["businesses"]: matches.append(store["name"]) return process.extractOne(name, matches) else: return process.extractOne(name, matchlist) except RuntimeError: return ("", 0)
def add_transaction(transaction): """ Populates the "categories" key in the transaction json and pushes to db """ db_trans = { "name": transaction['name'], "amount": transaction['amount'], "month": transaction["month"], "day": transaction["day"], } # find name in yelp name = preprocess(transaction["name"]) name = match(name) if name[1] < 90: # try again w/ autocomplete # print("Confidence not high enough, trying again") autocomplete = yelp.autocomplete(name[0]) if autocomplete != None: matches = [result["text"] for result in autocomplete] best = match(name[0], matchlist=matches) else: best = ("", 0) if name[1] < 90: # print("No close matches for {}".format(name)) return False else: name = name[0] categories = yelp.search(name)["businesses"][0] categories = categories['categories'] categories = [category["alias"] for category in categories] db_trans["categories"] = categories # push to database result = trans.insert_one(db_trans) return db_trans
# Get Access Token access_token = obtain_access_token(API_HOST, TOKEN_PATH) cat_dfs = [] # Loop over all categories and gather reviews for cat in CATEGORIES: cat_businesses = [] # Yelp API only returns ~20 businesses per search, so you need to search multiple times and offset the results for offset in np.arange(10): response = search(access_token, cat, DEFAULT_LOCATION, offset * SEARCH_LIMIT) cat_businesses.extend(response['businesses']) print("{} found!".format(len(response['businesses']))) cat_reviews = [] print("Found {0} results for {1}".format(len(cat_businesses), cat)) count = 1 start = time.time() # For all business IDs gathered for this category for b in cat_businesses: if count % 10 == 0: print("Scraping business {0} out of {1}".format(count, len(cat_businesses))) try: # Scrape the reviews from Yelp's site
from yelp_api import search # Test search test_results = search( url_params={ 'location': "Boston", 'term': "bars" } ) # Print results for business in test_results.businesses: print business.name