-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
80 lines (71 loc) · 3.34 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import urllib2,json
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
#home page with the form about what city the user would like to know about
@app.route("/")
def base():
return render_template("base.html")
#info about the city
#wikipedia, weather, google images
@app.route("/explore", methods = ["GET", "POST"])
def explore():
if request.form.has_key("city") and request.form["city"] != "":
city = request.form["city"].title()
wikiError = False
weatherError = False
imageError = False
#Wikipedia Info
url = "https://en.wikipedia.org/w/api.php?action=query&titles=%s&prop=extracts&exintro=&explaintext=&rvprop=content&format=json"
url = url%(city.replace(" ", "%20"))
req = urllib2.urlopen(url)
result = req.read()
r = json.loads(result)["query"]["pages"]
wiki = []
for i in r:
if r[i].has_key("extract") and r[i]["extract"] != "":
wiki.append(r[i]["extract"])
else:
wikiError = True
wiki = []
#Weather data
url = "http://api.openweathermap.org/data/2.5/weather?q=%s&units=Imperial&appid=7243b666b6841ed373ea8cd1289cc06d"
url = url%(city.replace(" ", "%20"))
req = urllib2.urlopen(url)
result = req.read()
r = json.loads(result)
if r.has_key("main"):
main = r["main"]
weather = []
weather.append("Current Temperature: " + str(main["temp"]) + " Farenheit")
weather.append("Humidity: " + str(main["humidity"]))
weather.append("Weather: " + r["weather"][0]["main"])
weather.append("Cloudiness: " + str(r["clouds"]["all"]))
weather.append("Wind Speed: " + str(r["wind"]["speed"]))
else:
weatherError = True
weather = []
#Google Images
url = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=%s"
url = url%(city.replace(" ", "%20"))
req = urllib2.urlopen(url)
result = req.read()
r = json.loads(result)
images = r["responseData"]["results"]
if len(images) > 0:
imageURLs = []
for i in range(0, 3):
if len(images) > i:
imageURLs.append(images[i]["unescapedUrl"])
else:
imageError = True
imageURLs = []
return render_template("explore.html", city = city, wikiError = wikiError, weatherError = weatherError, imageError = imageError, wiki = wiki, weather = weather, images = imageURLs)
else:
return redirect("/")
#returns about pages
@app.route("/about")
def about():
return render_template("about.html")
if __name__ == "__main__":
app.debug = True
app.run(host="0.0.0.0", port=8000)