-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathapp.py
More file actions
139 lines (121 loc) · 4.5 KB
/
app.py
File metadata and controls
139 lines (121 loc) · 4.5 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os
from flask import Flask, request, render_template, send_from_directory
import iss
from util import safe_float, json, jsonp
app = Flask(__name__)
# APIs:
API_DEFS = [
{
"title": "ISS Location Now",
"link": "/iss-now.json",
"desc": "Current ISS location over Earth (latitude/longitude)",
"doclink": "http://open-notify.org/Open-Notify-API/ISS-Location-Now",
"docname": "ISS-Location-Now"},
{
"title": "ISS Pass Times",
"link": "/iss-pass.json?lat=45.0&lon=-122.3",
"desc": "Predictions when the space station will fly over a particular location",
"doclink": "http://open-notify.org/Open-Notify-API/ISS-Pass-Times",
"docname": "ISS-Pass-Times"},
{
"title": "People in Space Right Now",
"link": "/astros.json",
"desc": "The number of people in space at this moment. List of names when known.",
"doclink": "http://open-notify.org/Open-Notify-API/People-In-Space",
"docname": "People-In-Space"},
]
@app.route("/")
def index():
return render_template('index.html', apis=API_DEFS)
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
################################################################################
# Current ISS Location
################################################################################
@app.route("/iss-now.json")
@app.route("/iss-now/")
@app.route("/iss-now/v1/")
@jsonp
@json
def iss_now():
loc = iss.get_location()
return dict({'message': "success"}, **loc), 200
################################################################################
# ISS Orbit Debug
################################################################################
@app.route("/iss-tle-info.json")
@jsonp
@json
def tle_info():
info = {'tle': iss.get_tle()}
info['tle-time'] = iss.get_tle_time()
info['tle-update'] = iss.get_tle_update()
return dict({'message': "success"}, **info), 200
################################################################################
# ISS Pass Predictions
################################################################################
@app.route("/iss-pass.json")
@app.route("/iss/")
@app.route("/iss/v1/")
@jsonp
@json
def iss_pass():
try:
# Sanitize inputs
lat = request.args.get('lat', False)
if lat:
lat = safe_float(lat, (-90.0, 90.0))
if not lat:
raise ValueError("Latitude must be number between -90.0 and 90.0")
else:
raise ValueError("Latitude must be specified.")
lon = request.args.get('lon', False)
if lon:
lon = safe_float(lon, (-180.0, 180.0))
if not lon:
ValueError("Longitude must be number between -180.0 and 180.0")
else:
raise ValueError("Longitude must be specified.")
alt = request.args.get('alt', False)
if alt:
alt = safe_float(alt, (0, 10000))
if not alt:
raise ValueError("Altitude must be number between 0 and 10,000")
else:
alt = 100
n = request.args.get('n', False)
if n:
n = safe_float(n, (1, 100))
if not n:
raise ValueError("Number of passes must be number between 1 and 100")
else:
n = 5
except ValueError as e:
error_response = {"message": "failure", "reason": e.message, "request": request.args}
return error_response, 400
# Calculate data and return
d = iss.get_passes(lon, lat, alt, int(n))
return dict({"message": "success"}, **d), 200
################################################################################
# Current People In Space
################################################################################
@app.route("/astros.json")
@app.route("/astros/")
@app.route("/astros/v1/")
@jsonp
@json
def astros():
Astros = [
{'name': "Mikhail Kornienko", 'craft': "ISS"},
{'name': "Scott Kelly", 'craft': "ISS"},
{'name': "Sergey Volkov", 'craft': "ISS"},
{'name': "Yuri Malenchenko", 'craft': "ISS"},
{'name': "Timothy Kopra", 'craft': "ISS"},
{'name': "Timothy Peake", 'craft': "ISS"},
]
return {'message': "success", 'number': len(Astros), 'people': Astros}, 200
if __name__ == "__main__":
app.debug = True
app.run()