def post(self): data = BusinessRegister.parser.parse_args() if BusinessModel.find_by_business_name(data['business_name']): return {'message': "A business with name '{}' already exists.".format(data['business_name'])}, 400 # business = BusinessModel(data['username'], sha256_crypt.hash(data['password']), data['email'], data['business_name'], data['address'], data['description'], data['url'], data['avatar']) business.save_to_db() # except: # return {"message": "An error occurred creating the store."}, 500 return business.json(), 201
def post(self): """ receives longitude, lattitude, price, and range needs to make call to yelp. save each business to the db scrape and save return objects in form url, name """ data = Start.parser.parse_args() if data["low_price"] < 1 or data["low_price"] > 4: return {"message": "price should be in range of 1 to 4"}, 400 if data["high_price"] < 1 or data["high_price"] > 4: return {"message": "price should be in range of 1 to 4"}, 400 price_range = '' for price in range(data['low_price'], data['high_price']): price_range += (str(price) + ', ') price_range += str(data['high_price']) url_params = { 'term': 'restaurants', 'latitude': data['lat'], 'longitude': data['long'], 'price': price_range, 'limit': 50, 'radius': data['radius'] } headers = { 'Authorization': 'Bearer %s' % TOKEN, } url = API_HOST + SEARCH_PATH response = requests.request('GET', url, headers=headers, params=url_params) if response is None: return {"message": "Yelp api call went wrong"}, 500 try: businesses = response.json()['businesses'] except: return response.json(), 500 unrandomized = [] for each in businesses: business = BusinessModel.find_by_yelp_id(each['id']) if business is None: address = '\n'.join( each['location']['display_address']).strip('[]') business = BusinessModel(each['name'], address, each['id'], each['rating'], each['price'], each['display_phone']) print("new! " + business.yelp_id) photo_ids = get_id(business.yelp_id) print("done scraping photo urls!") for x in range(0, 3): photomodel = PhotoModel(photo_ids[x], business.yelp_id) photomodel.save_to_db() print("done saving photo urls to db!") business.photo_ids = str(photo_ids) business.save_to_db() elif PhotoModel.find_by_photo_yelp_id( "http://kstatic.inven.co.kr/upload/2017/06/22/bbs/i13335449248.jpg", business.yelp_id ) and business.yelp_id == PhotoModel.find_by_photo_yelp_id( "http://kstatic.inven.co.kr/upload/2017/06/22/bbs/i13335449248.jpg", business.yelp_id).yelp_id: print(business.yelp_id + " currently has 0 photos") print("Rescrapting starts now") photo_ids = get_id(business.yelp_id) for x in range(0, 3): photomodel = PhotoModel(photo_ids[x], business.yelp_id) photomodel.save_to_db() PhotoModel.find_by_photo_yelp_id( "http://kstatic.inven.co.kr/upload/2017/06/22/bbs/i13335449248.jpg", business.yelp_id).delete_from_db() business.photo_ids = str(photo_ids) business.save_to_db() print("done saving actual photo urls to db!") for x in range(0, 3): unrandomized.append(ast.literal_eval(business.photo_ids)[x]) randomized = [] for x in range(0, 30): photo_id = unrandomized[random.randint(0, 149)] randomized.append(PhotoModel.find_by_photo_id(photo_id).json()) return randomized