def test_hourly_forecasts(): # with valid geography, returns the city name and forecast info: results = get_hourly_forecasts(country_code="US", zip_code="20057") assert results["city_name"] == "Washington, DC" assert len(results["hourly_forecasts"]) == 24 forecast = results["hourly_forecasts"][0] assert sorted(list( forecast.keys())) == ["conditions", "image_url", "temp", "timestamp"] assert forecast["timestamp"].endswith(":00") assert f"{DEGREE_SIGN}F" in forecast["temp"] # with invalid geography, fails gracefully and returns nothing: invalid_results = get_hourly_forecasts(country_code="US", zip_code="OOPS") assert invalid_results == None
def weather_forecast(): print("WEATHER FORECAST...") if request.method == "GET": print("URL PARAMS:", dict(request.args)) request_data = dict(request.args) elif request.method == "POST": # the form will send a POST print("FORM DATA:", dict(request.form)) request_data = dict(request.form) country_code = request_data.get("country_code") or "US" zip_code = request_data.get("zip_code") or "20057" unit = request_data.get("unit") or "F" results = get_hourly_forecasts(country_code=country_code, zip_code=zip_code, unit=unit) if results: flash(f"Weather Forecast Generated Successfully!", "success") return render_template("weather_forecast.html", country_code=country_code, zip_code=zip_code, unit=unit, results=results) else: flash(f"Geography Error. Please try again!", "danger") return redirect("/weather/form")
def weather_forecast_api(): print("WEATHER FORECAST (API)...") print("URL PARAMS:", dict(request.args)) country_code = request.args.get("country_code") or "US" zip_code = request.args.get("zip_code") or "20057" results = get_hourly_forecasts(country_code=country_code, zip_code=zip_code) if results: return jsonify(results) else: return jsonify({"message":"Invalid Geography. Please try again."}), 404
def weather_forecast(): print("GENERATING A WEATHER FORECAST...") if request.method == "POST": print("FORM DATA:", dict(request.form)) #> {'zip_code': '20057'} zip_code = request.form["zip_code"] elif request.method == "GET": print("URL PARAMS:", dict(request.args)) zip_code = request.args["zip_code"] results = get_hourly_forecasts(zip_code) print(results.keys()) return render_template("weather_forecast.html", zip_code=zip_code, results=results)
#from pprint import pprint from app import APP_ENV from app.weather_service import get_hourly_forecasts from app.email_service import send_email load_dotenv() MY_NAME = os.getenv("MY_NAME", default="Player 1") if __name__ == "__main__": if APP_ENV == "development": zip_code = input("PLEASE INPUT A ZIP CODE (e.g. 06510): ") weather_results = get_hourly_forecasts(zip_code=zip_code) # invoke with custom params else: weather_results = get_hourly_forecasts() # invoke with default params #print(weather_results) html = "" html += f"<h3>Good Morning, {MY_NAME}!</h3>" html += "<h4>Today's Date</h4>" html += f"<p>{date.today().strftime('%A, %B %d, %Y')}</p>" html += f"<h4>Weather Forecast for {weather_results['city_name'].title()}</h4>" html += "<ul>" for hourly in weather_results["hourly_forecasts"]: html += f"<li>{hourly['timestamp']} | {hourly['temp']} | {hourly['conditions'].upper()}</li>"
if __name__ == "__main__": print(f"RUNNING THE DAILY BRIEFING APP IN {APP_ENV.upper()} MODE...") # CAPTURE INPUTS user_country, user_zip, unit = set_geography() print("COUNTRY:", user_country) print("ZIP CODE:", user_zip) print("UNIT:", unit) # FETCH DATA result = get_hourly_forecasts(country_code=user_country, zip_code=user_zip, unit=unit) if not result: print("INVALID GEOGRAPHY. PLEASE CHECK YOUR INPUTS AND TRY AGAIN!") exit() # DISPLAY OUTPUTS todays_date = date.today().strftime('%A, %B %d, %Y') html = "<html>" html += f"<h3>Good Morning, {USER_NAME}!</h3>" html += "<h4>Today's Date</h4>" html += f"<p>{todays_date}</p>" html += f'<h2>Weather Forecast for {result["city_name"]}</h2>' html += f'<p>Zip Code: {user_zip}</p>' html += '<table style="width:100%">'