def weather(): username = flask.session.get('username', None) if username is None: return flask.redirect('/') msg = None if flask.request.args.get('error', None): msg = "<font color=red>Can't save recipe to favorites. Please check a recipe you want to add it to your favorites.</font><br />" # Checking if user navigated from home route from_home = flask.request.form.get('from_home', None) zipcode = flask.request.form.get('zipcode') current_recipe = api.Recipe_from_input(flask.request.form.get("recipe")) current_weather = api.get_weather_pretty(zipcode) # The user entered an invalid ZIP in the home route if current_weather == "Unavailable" and from_home: return flask.redirect('/home?error=1') temperature = 0 # Handling redirection and entry cases to this route ('/weather') if current_weather == "Unavailable": current_weather = api.retreive_user(username)["Weather"] if current_weather is None: return flask.redirect('/home?error=1') # Grabbing the temperature from the current_weather string for val in current_weather.split(): if val.isdigit(): temperature = int(val) if current_recipe == None: current_recipe = api.Recipe_from_input( api.grab_temp_recipe(temperature)) # Attempt to use the cached zipcode: zipcode = api.retreive_user(username)["Zipcode"] else: api.update_zipcode(username, zipcode) api.update_weather(username, current_weather) # Grabbing the temperature from the current_weather string for val in current_weather.split(): if val.isdigit(): temperature = int(val) if current_recipe == None: current_recipe = api.Recipe_from_input( api.grab_temp_recipe(temperature)) if zipcode: return flask.render_template('result.html', msg=msg, weather=current_weather, zipcode=zipcode, recipe=current_recipe) else: # The user entered an invalid ZIP, and never previously entered a valid ZIP during a prior session return flask.redirect('/home?error=1')
def yelpform(): username = flask.session.get('username', None) if username is None: return flask.redirect('/') zipcode = api.retreive_user(username)["Zipcode"] cached_results = api.retrieve_yelp_data(zipcode) now = datetime.datetime.now() fetch_new = False if cached_results is None: fetch_new = True else: difference = now - dateutil.parser.parse(cached_results['Updated']) if difference.seconds >= app.config['CACHE_EXPIRY_TIME']: fetch_new = True if fetch_new: URL = "https://api.yelp.com/v3/businesses/search" PARAMS = {'term': 'restaurants', 'location': zipcode, 'radius': 2000} HEADERS = {'Authorization': app.config['YELP_TOKEN']} r = requests.get(url=URL, params=PARAMS, headers=HEADERS) data = r.json() api.save_yelp_data(zipcode, r.content, now.isoformat()) else: data = json.loads(cached_results['YelpData']) data = sorted(data['businesses'], key=lambda e: 'delivery' in e['transactions'], reverse=True) return flask.render_template('yelp.html', data=data, zipcode=zipcode)
def index(): username = flask.session.get('username', None) if username is None: return flask.redirect('/') msg = None if flask.request.args.get('error', None): msg = "<font color=red>Can't fetch weather. Was that a valid zipcode?</font><br />" zipcode = api.retreive_user(username)["Zipcode"] return flask.render_template('index.html', msg=msg, zipcode=zipcode)
def show_favorites(): username = flask.session.get('username', None) if username is None: return flask.redirect('/') return flask.render_template( 'favorites.html', favorites=api.retreive_user(username)["Fav_Recipes"])