-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
49 lines (34 loc) · 1.35 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
from flask import Flask, render_template
from datetime import datetime
from urllib.parse import urlencode
import csv
import requests
GMAPS_URL = 'https://maps.googleapis.com/maps/api/staticmap?'
USGS_FEED_URL = 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_month.csv'
## helper functions
# should probably go into their own file but too lazy to do that
def get_quake_data():
resp = requests.get(USGS_FEED_URL)
data = list(csv.DictReader(resp.text.splitlines()))
# we need to format time separately
return data
def simple_static_gmap_url(location='Stanford,CA'):
# This just returns a URL string, it doesn't get the URL via requests
mydict = {'size': '300x200', 'maptype': 'hybrid', 'markers': location}
url = GMAPS_URL + urlencode(mydict, doseq=True)
return url
# Normal flask app stuff
app = Flask(__name__)
@app.route('/')
def homepage():
the_time = datetime.now().strftime("%A, %d %b %Y %l:%M %p")
the_quakes = get_quake_data();
# iterate through each quake, give them a "latlng" attribute
# and then a separate Google Map URL
for q in the_quakes:
q['gmap_url'] = simple_static_gmap_url()
html = render_template('homepage.html',
time=the_time, quakes=the_quakes)
return html
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)