def get_yelp_api_client(): """ Get the Yelp API client :return: Yelp API client :rtype: Yelp Client """ FUNC_NAME = inspect.currentframe().f_code.co_name try: if 'OPENSHIFT_REPO_DIR' in os.environ or 'TRAVIS' in os.environ: auth = Oauth1Authenticator( consumer_key=os.environ['YELP_CONSUMER_KEY'], consumer_secret=os.environ['YELP_CONSUMER_SECRET'], token=os.environ['YELP_TOKEN'], token_secret=os.environ['YELP_TOKEN_SECRET']) else: file_path = get_yelp_api_keys_filepath() with io.open(file_path, 'r') as cred: creds = json.load(cred) auth = Oauth1Authenticator(**creds) client = Client(auth) except Exception as ex: log_exception(MODULE_NAME, FUNC_NAME, ex) return client
def yelp_search(): # read API keys if "consumer_key" in os.environ: auth = Oauth1Authenticator( consumer_key=os.environ['consumer_key'], consumer_secret=os.environ['consumer_secret'], token=os.environ['token'], token_secret=os.environ['token_secret']) else: with open('config_secret.json') as cred: creds = json.load(cred) auth = Oauth1Authenticator(**creds) client = Client(auth) client = Client(auth) params = {'term': request.args.get('category', 'food'), 'lang': 'en'} response = client.search('South Bend, IN', **params) businesses = response.businesses results = { b.name: { "business_url": b.url, "image_url": b.image_url, "snippet": b.snippet_text, "location": { "latitude": b.location.coordinate.latitude, "longitude": b.location.coordinate.longitude, }, } for b in businesses } return jsonify(**results)
def get_businesses(location, searchterm): auth = Oauth1Authenticator( consumer_key=os.environ['CONSUMER_KEY'], consumer_secret=os.environ['CONSUMER_SECRET'], token=os.environ['TOKEN'], token_secret=os.environ['TOKEN_SECRET'], ) client = Client(auth) params = {'term': searchterm, 'lang': 'en', 'limit': 3, 'sort': 2} # page_size = 3 response = client.search(location, **params) businesses = [] for business in response.businesses: #print(business.name, business.rating, business.phone) businesses.append({ "name": business.name, "rating": business.rating, "phone": business.phone }) return businesses #businesses = get_businesses('Melbourne', 'food') #print(businesses)
def main(): parser = argparse.ArgumentParser() parser.add_argument("output_filename", help="do not include extension (always .json)") args = parser.parse_args() # read API keys with io.open('config_secret.json') as cred: creds = json.load(cred) auth = Oauth1Authenticator(**creds) client = Client(auth) params = {'term': 'food', 'sort': '2'} response = client.search('Richmond', **params) compactJson = [] formats = ("id", "name", "rating") for bus in response.businesses: compacted = {} for format in formats: compacted[format] = getattr(bus, format) compacted["latitude"] = bus.location.coordinate.latitude compacted["longitude"] = bus.location.coordinate.longitude compactJson.append(compacted) jsonDump = json.dumps(compactJson, ensure_ascii=False, sort_keys=True, indent=4) with open(args.output_filename + ".json", "w") as f: f.write(jsonDump)
class YelpSearch: CONSUMER_KEY = "xr2AGjpXyVFSqOFZjukQjg" CONSUMER_SECRET = "LWKk-n_sXFJtpSxFvyNOK4yIChA" TOKEN = "mz6jzjOwwNiWHm9HM0SZL3lS2vAdFiv5" TOKEN_SECRET = "ffYjX0GfbQ7vrWnEjTPJFQiS93w" auth = Oauth1Authenticator(consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET, token=TOKEN, token_secret=TOKEN_SECRET) client = Client(auth) def searchStuff(self, place, stuff=None): if (stuff == None): resp = self.client.search(place).businesses else: resp = self.client.search(place, term=stuff).businesses dicts_to_output = [{ 'name': biz.name, 'id': biz.id, 'rating': biz.rating, 'review_count': biz.review_count, 'location': biz.location.display_address, } for biz in resp] return dicts_to_output
def get_random_bar_coords(lat, lon, radius): mpm = 1609.34 # meters per mile config = json.load(open('conf.json', 'rb')) auth = Oauth1Authenticator( consumer_key=config['yelp']['consumer_key'], consumer_secret=config['yelp']['consumer_secret'], token=config['yelp']['token'], token_secret=config['yelp']['token_secret'] ) client = Client(auth) param_offset = lambda x: { 'term': 'bars, All', 'lang': 'en', 'sort': 2, # 2: only the top rated in the area 'radius_filter': int(radius * mpm), 'offset': x } bus_list = [] for offset in [0, 20]: response = client.search_by_coordinates(lat, lon, **param_offset(offset)) bus_list.extend(response.businesses) bar = random.choice(bus_list) while bar.name in ["Peter's Pub", "Hemingway's Cafe"]: # Can we just bar = random.choice(bus_list) return bar.location.coordinate.latitude, bar.location.coordinate.longitude
def __init__(self, db): self.__db = db self.__auth = Oauth1Authenticator( consumer_key="REiAz8Bdo4ZDdqoSeZ_CHw", consumer_secret="TTbM3viyQyvT4ED4uEpPZEIgbXY", token="gdFMM-VX3R6nFblRMTFJj2cXELQxVFAI", token_secret="qaTy_1Y1PRVMfjLS4HaDy2HWuwk")
def get_businesses(address): auth = Oauth1Authenticator( consumer_key=os.environ['YELP_CONSUMER_KEY'], consumer_secret=os.environ['YELP_CONSUMER_SECRET'], token=os.environ['YELP_TOKEN'], token_secret=os.environ['YELP_TOKEN_SECRET']) client = Client(auth) params = {'term': "food", 'lang': 'en', 'limit': '3'} response = client.search(address, **params) businesses = [] for business in response.businesses: # print(business.name, business.rating, business.phone) businesses.append({ "name": business.name, "rating": business.rating, "phone": business.phone, "image": business.image_url, "stars": business.rating_img_url }) #return("The top 3 recommended resturants for {}, are: {}, {}, and {}".format(address, businesses[0]['name'], #businesses[1]['name'], businesses[2]['name'])) return businesses
def yelpsearchapi_by_location(loc): DEFAULT_LOCATION = loc CREDENTIALS_PATH = 'static/credentials.json' RADIUS_FILTER_METERS = 1000 NEAREST_SORT_OPTION = 1 LIMIT = 5 # read api keys credentials with io.open(CREDENTIALS_PATH) as cred: creds = json.load(cred) auth = Oauth1Authenticator(**creds) client = Client(auth) # enter parameters params = { 'category_filter': 'bars', 'radius_filter': RADIUS_FILTER_METERS, 'sort': NEAREST_SORT_OPTION, 'limit': LIMIT } if loc: response = client.search(loc, **params) else: response = client.search(DEFAULT_LOCATION, **params) return response
def main(): # read API keys with io.open('config_secret.json') as cred: creds = json.load(cred) session = Oauth1Authenticator(**creds) client = Client(session) locations = [(40.744, -73.985)] api_calls = [] neighborhood_counter = Counter() for lat,lon in locations: params = get_search_parameters(lat,lon) results = client.search_by_coordinates(lat, lon, **params) #Be a good internet citizen and rate-limit yourself for b in results.businesses: # business fields: 'categories', 'deals', 'display_phone', 'distance', 'eat24_url', 'gift_certificates', 'id', 'image_url', 'is_claimed', 'is_closed', 'location', 'menu_date_updated', 'menu_provider', 'mobile_url', 'name', 'phone', 'rating', 'rating_img_url', 'rating_img_url_large', 'rating_img_url_small', 'reservation_url', 'review_count', 'reviews', 'snippet_image_url', 'snippet_text', 'url' print b.name print "Street address: %s"%b.location.cross_streets print "Neighborhoods: %s"%b.location.neighborhoods print b.location.coordinate.latitude, b.location.coordinate.longitude neighborhood_counter.update(b.location.neighborhoods) # 'address', 'city', 'coordinate', 'country_code', 'cross_streets', 'display_address', 'geo_accuracy', 'neighborhoods', 'postal_code', 'state_code' print neighborhood_counter
def authenticate_api(): """check credentials and return a yelp client""" with io.open('static/yelp_config_secret.json') as cred: creds = json.load(cred) auth = Oauth1Authenticator(**creds) client = Client(auth) return client
def search_yelp(input): out = [] with io.open('config_secret.json') as cred: creds = json.load(cred) auth = Oauth1Authenticator(**creds) client = Client(auth) params = { 'term' : input } client = Client(auth) response = client.search('Atlanta', **params) print json.dumps(response, default=lambda o: o.__dict__, indent=2) for a in response.businesses: if a.image_url is None: a.image_url = 'http://http.cat/404.jpg' try: nav_link = ','.join([str(a.location.coordinate.latitude), str(a.location.coordinate.longitude)]) except AttributeError: # print json.dumps(a, default=lambda o: o.__dict__, indent=2) nav_link = '' out.append([a.name, a.image_url.replace("ms.jpg", "l.jpg"), a.url, a.rating, a.phone, nav_link]) if (len(out) == 0): out.append('No results found') return out
def get_businesses (location, term, limit): auth = Oauth1Authenticator( consumer_key=os.environ['CONSUMER_KEY'], consumer_secret=os.environ['CONSUMER_SECRET'], token=os.environ['TOKEN'], token_secret=os.environ.get('TOKEN_SECRET') ) client = Client(auth) params = { 'term': term, 'lang': 'en', 'limit': limit } response = client.search(location, **params) # create an empty list named businesses businesses = [] # add things to it. for business in response.businesses: businesses.append({"name": business.name, "address": business.location.display_address, "rating": business.rating, 'phone': business.display_phone}) return businesses
def get_businesses(location, term): auth = Oauth1Authenticator(consumer_key=os.environ['CONSUMER_KEY'], consumer_secret=os.environ['CONSUMER_SECRET'], token=os.environ['TOKEN'], token_secret=os.environ['TOKEN_SECRET']) client = Client(auth) params = {'term': term, 'lang': 'en', 'limit': 3} response = client.search(location, **params) businesses = [] # for business in response.businesses: # # print(business.name, business.rating, business.phone) # businesses.append(business.name) # return businesses # businesses = get_businesses('Danville', 'golf') # print(businesses) # question: what if i want to print out a list of each business as a dictionary for business in response.businesses: print(business.name, business.rating, business.phone) businesses.append({ "name": business.name, "rating": business.rating, "phone": business.phone }) return businesses
def call_yelp(midlat,midlong,radius): auth = Oauth1Authenticator( consumer_key='Qas4yX9C4dXU-rd6ABmPEA', consumer_secret='5Rtb9Gm5KuwRHSevprdb450E37M', token='xBq8GLMxtSbffTWPPXx7au7EUd2ed3MO', token_secret='Nee0b14NkDO9uSX5cVsS7c4j6GQ' ) print radius client = Client(auth) params = { 'term':'24 hour', 'sort':'0', 'radius_filter': str(radius) # 3 mi radius } #ex: response = client.search_by_coordinates(40.758895,-73.985131, **params) response = client.search_by_coordinates(float(midlat),float(midlong), **params) f = open('output_yelp.txt','w+') for i in range(0,len(response.businesses)): f.write(response.businesses[i].name) f.write(",") f.write('%f' % response.businesses[i].location.coordinate.latitude) f.write(",") f.write('%f' % response.businesses[i].location.coordinate.longitude) f.write("\n") f.close()
def grab_restaurants(city, offset): """grabs business information for the input city args: city (str): the input city offset (int): input value to change the search window for the city returns: 20 restaurants based on the search window for a given city """ # set up api call with given keys # I followed Yelp's advice to store api information in a json file for privacy with io.open('config_secret.json') as cred: creds = json.load(cred) auth = Oauth1Authenticator(**creds) client = Client(auth) # set parameters to search for restuarants in a given offset window params = { 'term': 'restaurant', 'offset': offset } return client.search(city, **params)
def search_yelp(secret_file, location, search_parameters): # Extract Yelp Data through the API import io import json from yelp.client import Client from yelp.oauth1_authenticator import Oauth1Authenticator # read API keys with io.open(secret_file) as cred: # Auth in file not shared on github creds = json.load(cred) auth = Oauth1Authenticator(**creds) client = Client(auth) # There are three ways to query the Search API: search, search_by_bounding_box, and # search_by_coordinates. For each of these methods, additional parameters are optional. # The full list of parameters can be found on the Search API Documentation. # https://github.com/Yelp/yelp-python # search_by_bounding_box takes a southwest latitude/longitude and a northeast # latitude/longitude as the location boundary response = client.search_by_bounding_box( location['sw_latitude'], location['sw_longitude'], location['ne_latitude'], location['ne_longitude'], **search_parameters ) return response
def get_yelp_info(yelp_id): origin = request.headers.get('Origin') referer = request.headers.get('Referer') if origin is None or referer is None: return abort(404) allowed_url = AllowedUrl.query(AllowedUrl.origin == origin, AllowedUrl.referer == referer).get() if allowed_url is None: return abort(404) yelp_key = Yelp.query().get() if yelp_key is None: return abort(404) auth = Oauth1Authenticator(consumer_key=yelp_key.consumer_key, consumer_secret=yelp_key.consumer_secret, token=yelp_key.token, token_secret=yelp_key.token_secret) client = Client(auth) result = client.get_business(yelp_id).business response = {} response['url'] = result.url response['image_url'] = result.image_url response['rating_img_url'] = result.rating_img_url return jsonify(response)
def get_businesses(location, term): auth = Oauth1Authenticator( consumer_key=environ['CONSUMER_KEY'], consumer_secret=environ['CONSUMER_SECRET'], token=environ['TOKEN'], token_secret=environ['TOKEN_SECRET'] ) client = Client(auth) params = { 'term': term, 'lang': 'en', 'limit': 5 } response = client.search(location, **params) businesses = [] for business in response.businesses: # print(business.name, business.rating, business.phone) businesses.append({"name": business.name, "rating": business.rating, "phone": business.phone, "address": business.location.address }) return businesses
def GetAuthAndClient(): auth = Oauth1Authenticator( consumer_key='_GHyOtIbZl0GCrjsQDYSwg', consumer_secret='WDqxsZJdKcg4BmI-bBBcfdQhNt4', token='EvYL_JqVpEAq0tzasuwdn_5JZ_adm59I', token_secret='xEvIsXujBvWEE_mYYqHaIc9olrU', ) return Client(auth)
def __init__(self, data): #authorization with io.open('config_secret2.json') as cred: creds = json.load(cred) auth = Oauth1Authenticator(**creds) self.client = Client(auth) self.data = data self.food_per_business = data['food_per_business']
def authenticate(config_filepath): with io.open(config_filepath) as cred: creds = json.load(cred) auth = Oauth1Authenticator(**creds) client = Client(auth) return client
def get_yelp_client(): auth = Oauth1Authenticator( consumer_key=os.environ['YELP_CONSUMER_KEY'], consumer_secret=os.environ['YELP_CONSUMER_SECRET'], token=os.environ['YELP_TOKEN'], token_secret=os.environ['YELP_TOKEN_SECRET']) client = Client(auth) return client
def group_recommend(request, event_id): try: event = Group.objects.get(id=event_id) except ObjectDoesNotExist: raise Http404 context = {} # if the request user has no relation with the event, redirect to homepage if not (request.user == event.user or event.members.filter(user=request.user).exists()): return redirect('/linterest/') # create a new chatroom associated with event if not already exist try: chatroom = GroupChatRoom.objects.get(group=event) except ObjectDoesNotExist: # create a new chatroom w.r.t. the event if not already exist chatroom = GroupChatRoom(group=event) members = event.members.all(); chatroom.users.add(Profile.objects.get(user=event.user)) for member in members: chatroom.users.add(member) chatroom.save() context["event_id"] = event_id context["user"] = request.user context["type"] = "group" with io.open('yelp_secret.json') as cred: creds = json.load(cred) auth = Oauth1Authenticator(**creds) client = Client(auth) params = { 'term': event.type, 'lang': 'en', 'limit': 10, } print('prepare to search') try: response = client.search(event.city, **params) except: defaultCoordinate = [40.44, -79.99] context['latitude'] = defaultCoordinate[0] context['longitude'] = defaultCoordinate[1] context['error_message'] = 'Recommendations are unavailable in the given location' return render(request, 'recommend.html', context) print('search successfully') context['latitude'] = response.region.center.latitude context['longitude'] = response.region.center.longitude context['businesses'] = response.businesses return render(request, 'recommend.html', context)
def create_client(): # Creates Yelp api client using a set of temporary keys I obtained for # this exercise auth = Oauth1Authenticator(consumer_key="QbwJZmMMjlyIzRZZFQzGPw", consumer_secret="n34uOEDkj-58esi4lscxWjxrLOQ", token="YXFlXv9023NiHhoYL5BbBf-ZTPz5FbWq", token_secret="jnAS4LPnLqZPk27yk7wTnTpGoXQ") client = Client(auth) return client
def fetchCoffeeBusinesses(self): with io.open(self.yelp_auth_path) as auth_file: creds = json.load(auth_file) auth = Oauth1Authenticator(**creds) client = Client(auth) params = {'term': 'coffee', 'lang': 'en'} coffee_shops = client.search('San Francisco', **params) return coffee_shops.businesses
def __init__(self, yelp_config): self.auth = Oauth1Authenticator( consumer_key=yelp_config['consumer_key'], consumer_secret=yelp_config['consumer_secret'], token=yelp_config['token'], token_secret=yelp_config['token_secret']) self.client = Client(self.auth) self.reader = csv.DictReader self.outfile = open('data/yelp/businesses.csv', "w+") self.db = []
def Add_nodes(city_name='San_Francisco', buss_type='Restaurants', number=100): # Ussing the Yelp library for Yelp authentification and requesting info auth = Oauth1Authenticator(consumer_key="", consumer_secret="", token="", token_secret="") client = Client(auth) buss_types = make_sure('buss_types') assert buss_type in buss_types.keys() locations = find_locations(city_name, number) if os.path.isfile('Data/{}_Data.json'.format(city_name)) == False: with open('Data/{}_Data.json'.format(city_name), 'w') as f: geojson.dump({"type": "FeatureCollection", "features": []}, f) with open('Data/{}_Data.json'.format(city_name)) as f: data = json.load(f) IDs = [] added = 0 nodes_added = [] for feature in [ feature for feature in data['features'] if feature['geometry']['type'] == 'point' ]: IDs.append(feature['properties']['name']) for lat, long in locations: places = client.search_by_coordinates(lat, long, buss_type) for business in places.businesses: if business.name not in IDs: IDs.append(business.name) node = Node(business.name, business.location.coordinate.latitude, business.location.coordinate.longitude, business.rating, buss_type, business.categories, business.image_url, city_name, comp_time=buss_types[buss_type]['durations']) data['features'].append(node.geo_interface()) nodes_added.append(node) added += 1 with open('Data/{}_Data.json'.format(city_name), 'w') as f: geojson.dump(data, f) return '{} nodes added'.format(added)
def __init__(self): self.name = "yelp" creds = self.loadCredentials() # Authenticate Yelp auth = Oauth1Authenticator(consumer_key=creds["consumer_key"], consumer_secret=creds["consumer_secret"], token=creds["token"], token_secret=creds["token_secret"]) self.yelpClient = Client(auth) self.mongoClient = MongoClient(creds["mongodb"])
def search_food(address): auth = Oauth1Authenticator(consumer_key=os.environ['CONSUMER_KEY'], consumer_secret=os.environ['CONSUMER_SECRET'], token=os.environ['TOKEN'], token_secret=os.environ['TOKEN_SECRET']) client = Client(auth) FORECASTIO_API_KEY = os.environ['FORECASTIO_API_KEY'] # print("What term are you looking for?") # term_input=raw_input() # print("And around what address are you looking at?") # area=raw_input() # print("Looking for {} in {}".format(term_input, area)) params = {'term': 'bar', 'radius_filter': 1200, 'lang': 'en'} response = client.search(address, **params) businesses_list = [] for business in response.businesses: if business.rating >= 3.5: businesses_list.append({ "name": business.name, "rating": business.rating, "phone": business.phone }) # Select a random number from the list of businesses rannum = randint(0, len(businesses_list) - 1) # print("Cool! Beep boop beep beep boop....") # print("calculating...") # print("calculating...") # print("calculating...") # print("Out of {} businesses I am choosing number {}".format(len(businesses_list),rannum)) # Caclulate weather at random selected resturant # def get_address(buisnessname): # location = geolocator.geocode(buisnessname) # print(location) # Select a random business from the business list, using the random int generated above return "Check out {}! \n Phone number is {}".format( businesses_list[rannum].get('name'), businesses_list[rannum].get('phone'))