-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_data.wsgi
executable file
·59 lines (47 loc) · 1.83 KB
/
get_data.wsgi
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
#!/usr/bin/env python
import sys, os, os.path, urlparse, json, time, re, cgi, urllib, datetime
from collections import Sequence
sys.path.append('.')
import windgraphs
from dtpythonutil.misc import *
# WSGI entry point.
def application(environ, start_response):
try:
if '.' not in sys.path:
# This was necessary once. Not sure if it still is.
sys.path.append('.')
query_vars = urlparse.parse_qs(environ['QUERY_STRING'])
target_time_of_day = int(query_vars['target_time_of_day'][0])
weather_check_num_hours_in_advance = int(query_vars['weather_check_num_hours_in_advance'][0])
num_days = int(query_vars['num_days'][0])
end_date = query_vars['end_date'][0]
if end_date == 'today':
end_date = datetime.date.today()
else:
end_date = datetime.date(int(end_date[:4]), int(end_date[4:6]), int(end_date[6:8]))
r = windgraphs.get_data(target_time_of_day, weather_check_num_hours_in_advance, end_date, num_days)
response_headers = [('Content-type', 'application/json')]
start_response('200 OK', response_headers)
r = json.dumps(r)
return [r]
except:
printerr('[client %s] (pid=%s) Request url: %s' % (environ['REMOTE_ADDR'], os.getpid(), get_request_url(environ)))
raise
# Thanks to Ian Becking via https://www.python.org/dev/peps/pep-0333/
def get_request_url(environ):
url = environ['wsgi.url_scheme']+'://'
if environ.get('HTTP_HOST'):
url += environ['HTTP_HOST']
else:
url += environ['SERVER_NAME']
if environ['wsgi.url_scheme'] == 'https':
if environ['SERVER_PORT'] != '443':
url += ':' + environ['SERVER_PORT']
else:
if environ['SERVER_PORT'] != '80':
url += ':' + environ['SERVER_PORT']
url += urllib.quote(environ.get('SCRIPT_NAME', ''))
url += urllib.quote(environ.get('PATH_INFO', ''))
if environ.get('QUERY_STRING'):
url += '?' + environ['QUERY_STRING']
return url