-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (56 loc) · 1.97 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
57
58
59
60
61
62
63
64
65
66
from weatherapi.weatherapi_client import WeatherapiClient
from weatherapi.api_helper import APIHelper
import constants
key = constants.API_SERVICE_KEY
from flask import Flask, render_template ,request, url_for, flash, redirect
app = Flask(__name__)
app.debug = True
@app.route('/', methods=('GET', 'POST'))
def index():
if request.method == 'POST':
q = request.form['place']
lang = request.form['lang']
weather_info = getWeather(q,lang)
forecast_info = getForecast(q, 3)
if(weather_info==None):
q = "Mumbai"
lang = "en"
weather_info = getWeather(q,lang)
forecast_info = getForecast(q, 3)
return render_template('index.html', place=weather_info , forecast=forecast_info, error="Invalid place")
return render_template('index.html', place=weather_info , forecast=forecast_info)
q = "Mumbai"
lang = "en"
weather_info = getWeather(q,lang)
forecast_info = getForecast(q, 3)
return render_template('index.html', place=weather_info , forecast=forecast_info)
def getForecast(place, days):
"""
Returns json Forecast by taking input of place anf number of days
"""
#Create a client by passing the API key
client = WeatherapiClient(key)
ap_is_controller = client.ap_is
#get forecast raw data
try:
forecast= ap_is_controller.get_forecast_weather(place,days)
except:
return
#Convert data to python dictionary
forecast_info=APIHelper.to_dictionary(forecast)
return forecast_info
def getWeather(place, lang):
"""
Returns realtime weather
"""
#Create a client by passing the API key
client = WeatherapiClient(key)
ap_is_controller = client.ap_is
#get raw data for weather
try:
weather = ap_is_controller.get_realtime_weather(place, lang)
except:
return
#Convert response to python dictionary
weather_info = APIHelper.to_dictionary(weather)
return weather_info