-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
56 lines (39 loc) · 1.53 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# import the module
import python_weather
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/forecast/<city>/temperature")
async def get_weather_temp(city):
data = await get_weather_forecast_current(city)
response = jsonify(data)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route("/forecast/<city>/temperature/hourly")
async def get_weather_forecast(city):
data = await get_weather_forecast_hourly(city)
response = jsonify(data)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
@app.route("/forecast/<city>/ultraviolet")
async def get_weather_ultraviolet(city):
data = await get_weather_forecast_ultraviolet(city)
response = jsonify(data)
response.headers.add('Access-Control-Allow-Origin', '*')
return response
async def get_weather_forecast_current(city):
async with python_weather.Client() as client:
weather = await client.get(city)
return weather.current.temperature
async def get_weather_forecast_hourly(city):
async with python_weather.Client() as client:
weather = await client.get(city)
temps = []
for forecast in weather.forecasts:
temps.extend(hourly.temperature for hourly in forecast.hourly)
return temps
async def get_weather_forecast_ultraviolet(city):
async with python_weather.Client() as client:
weather = await client.get(city)
ultraviolet_list = []
print(weather.current.ultraviolet)
return weather.current.ultraviolet.value