-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
98 lines (89 loc) · 2.5 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from flask import Flask, render_template, request, redirect, jsonify
import requests
import json
import os
# Start the main app
app = Flask(__name__)
# Set the directory to retrieve static files from
app.static_folder = 'static'
# Variables to use for app
location = None
weather_api_key = "79b57a44308eea0e1f732061cf675137"
# Methods
'''
-- Can use for local enviornment, but not for heroku --
def getLocationIP():
url = 'http://freegeoip.net/json'
req = requests.get(url)
res = req.json()
return res
'''
def cleanAPI(_json):
# if exception is thrown here, then return error message
try:
cleaned = {}
cleaned['name'] = _json['name']
cleaned['temp'] = {
'current': int(_json['main']['temp']),
'min': int(_json['main']['temp_min']),
'max': int(_json['main']['temp_max'])
}
cleaned['features'] = _json['weather']
cleaned['humidity'] = _json['main']['humidity']
except:
return {"error": "bad location"}
# wind speed (meter/sec) and direction
try:
cleaned['wind'] = _json['wind']
except:
cleaned['wind'] = 0
# last 3hr rain volume
try:
cleaned['rain'] = _json['rain']
except:
cleaned['rain'] = 0
# last 3hr rain volume
try:
cleaned['snow'] = _json['snow']
except:
cleaned['snow'] = 0
# cloud coverage (%)
try:
cleaned['clouds'] = _json['clouds']['all']
except:
cleaned['clouds'] = "none"
return cleaned
def requestWeather(_data):
url = 'https://api.openweathermap.org/data/2.5/weather'
payload = _data
payload['APPID'] = weather_api_key
req = requests.get(url, params=payload)
res = cleanAPI(req.json())
return res
# Routes
@app.route('/')
def landing():
# render the landing page template here
return render_template('landing.html')
@app.route('/weather', methods=['GET'])
def weatherAPI():
_type = request.args.get('type')
if _type == 'zipcode':
_data = { 'zip': request.args.get('zip') }
_result = requestWeather(_data)
else:
'''
_geo = getLocationIP()
_data = {
'lat': _geo['latitude'],
'lon': _geo['longitude']
}
'''
_data = { 'lat': request.args.get('lat'), 'lon': request.args.get('lon') }
_result = requestWeather(_data)
return jsonify(_result)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
if __name__ == '__main__':
app.run()