-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.py
334 lines (296 loc) · 12.7 KB
/
hello.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import os
import click
from flask.cli import with_appcontext
import requests
import json
import webbrowser
from datetime import datetime, timedelta
from datetime import date
import time
import calendar
import numpy as np
import math
from flask import Flask, request, render_template, redirect, url_for, flash
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import TextAreaField
import random
import string
from flask_sqlalchemy import SQLAlchemy
from urllib import request as ur
from flask import session
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
@app.before_request
def func():
session.modified = True
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY') or 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('HEROKU_POSTGRESQL_CRIMSON_URL').replace('postgres://', 'postgresql://')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
class State(db.Model):
__tablename__='states'
id=db.Column(db.Integer, primary_key=True)
report=db.Column(db.Text)
calday = db.Column(db.String(64))
complete_date = db.Column(db.String(64))
ld = db.Column(db.Integer)
daysago = db.Column(db.Integer)
count = db.Column(db.Integer)
lastpage=db.Column(db.Integer)
pn=db.Column(db.Integer)
@app.shell_context_processor
def make_shell_context():
return dict(db=db, State=State)
def get_random_string(length):
# choose from all lowercase letter
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
def createList(r1, r2):
# Testing if range r1 and r2
# are equal
if (r1 == r2):
return r1
else:
# Create empty list
res = []
# loop to append successors to
# list until r2 is reached.
while(r1 > r2-1 ):
res.append(r1)
r1 -= 1
return res
# Driver Code
class ReportForm(FlaskForm):
report = TextAreaField()
# This decorator tells Flask to use this function as a webpage handler/renderer
@app.route('/', methods=['GET', 'POST'])
def daysnlunar():
global pagenum
#for all records
db.session.query(State).delete()
db.session.commit()
#day_selection=[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,'']
#r1, r2 = 9000, 0
r1, r2 = 3650, 0
day_selection=createList(r1, r2)
r1, r2 = 50, 0
LD_selection=createList(r1, r2)
format = '%Y-%m-%d'
if request.method == 'POST':
#GET DAYS AGO AND LUNAR DISTANCE PARAMETERS
# Check which button was pressed
submit = request.form.get('submit')
clear = request.form.get('clear')
if submit == 'Submit':
daysago = int(request.form.get('daysago'))
LD_str = str(request.form.get('LD'))
LD = int(request.form.get('LD'))
if daysago>50 and LD>1:
flash("You selected " + str(daysago) + " days for your time window. To keep results manageable, time windows greater than 50 will be processed with 1 lunar distance.")
LD_str="1"
LD=1
sort=request.form.get("sort")
if sort=='size':
s1='h'
s1_desc='size'
else:
s1='dist'
s1_desc='proximity to Earth'
#Process the date
d1 = str((datetime.utcnow() - timedelta(days=daysago)).strftime('%Y-%m-%d'))
d2 = str((datetime.utcnow() + timedelta(days=daysago)).strftime('%Y-%m-%d'))
datef=datetime.strptime(d1,format)
complete_date=datef.strftime("%b %d, %Y")
calday=calendar.day_name[datef.weekday()]
f = (r"https://ssd-api.jpl.nasa.gov/cad.api?dist-max=" + LD_str + "LD&date-min=" + d1 + "&date-max=" + d2 + "&sort=" + s1)
data = requests.get(f)
t = json.loads(data.text)
source=t['signature']['source']
count = int(t['count'])
#MAKE A REPORT ABOUT ASTEROIDS
report=[]
for n in range(count):
object = t['data'][n]
object_name=object[0]
if '99942' in object_name:
object_name='99942 Apophis'
elif object_name=='153814':
object_name='(153814) 2001 WN5'
elif object_name=='367943':
object_name='367943 Duende'
elif object_name=='2019 OD':
object_name='2019 OK'
try:
dlow = str("{0:,.0f}".format((1329/math.sqrt(.25))*(10**(-0.2*float(object[10])))*3280.84))
except:
dlow=''
try:
dhigh = str("{0:,.0f}".format((1329/math.sqrt(.05))*(10**(-0.2*float(object[10])))*3280.84))
except:
dhigh=''
if (0 <= n <= 24) and daysago>10:
try:
ur.urlopen("https://en.wikipedia.org/wiki/" + object_name.replace(" ","_"))
wiki="https://en.wikipedia.org/wiki/" + object_name.replace(" ","_")
except ur.HTTPError as e:
wiki=""
except ur.URLError as e:
wiki=""
else:
wiki=""
#HTML will not print out empty strings
report=report + ['The object named (' + object_name +') ']
#if wiki:
report=report+[wiki]
miles=str("{0:,.0f}".format(np.round(float(object[4])*92955807.267433,decimals=2)))
format2 = '%Y-%b-%d'
if datetime.strptime(object[3][:11],format2) < datetime.today(
) and datetime.strptime(object[3][:11],format2) + timedelta(
days=1) > datetime.today():
timeref=' is now '
elif datetime.strptime(object[3][:11],format2) < datetime.today():
timeref=' was '
elif datetime.strptime(object[3][:11],format2) > datetime.today():
timeref=' will be '
report=report+[timeref[1:] + miles + ' miles from Earth on ' + object[3][:11] + ' (the moon is 238,854 miles away)']
if int(miles.replace(",", ""))<238854:
report=report+['--This object' + timeref + 'closer to the Earth than the Moon!--']
else:
report=report+['--This object' + timeref + 'beyond the orbit of the Moon.--']
report=report+['and is between ' + dlow + ' and ' + dhigh + ' feet across.']
report=report+['Within search parameters, this near-Earth object' + timeref + 'ranked #' + str(n+1) + ' in ' + s1_desc + '.']
#report=report+["https://watchers.news/?s=" + object[0] + "&post_type=post"]
if LD<=50 and daysago<=10:
try:
if int(miles.replace(',',''))>4000:
ur.urlopen("https://theskylive.com/will-asteroid-" + object[0].replace(' ','').lower() + "-impact-earth")
report=report+["https://theskylive.com/will-asteroid-" + object[0].replace(' ','').lower() + "-impact-earth"]
else:
report=report+["https://www.google.com/search?q=" + object[0] + "+asteroid"]
except ur.HTTPError as e:
#print(e.code)
report=report+["no skylive weblink available"]
except ur.URLError as e:
#print(e.args)
report=report+["no skylive weblink available"]
else:
report=report+["For skylive weblink, select a time window <=10."]
if LD<=50 and daysago<=10:
try:
ur.urlopen("https://www.spacereference.org/asteroid/" + object[0].replace(' ','-').lower())
report=report+["https://www.spacereference.org/asteroid/" + object[0].replace(' ','-').lower()]
except ur.HTTPError as e:
report=report+["no spacereference weblink available"]
except ur.URLError as e:
report=report+["no spacereference weblink available"]
else:
report=report+["For spacereference weblink, select a time window <=10."]
report=report+['break']
#if LD==1:
#ceiling: will need compensation in the form. For example for 27 objects on 3-per-page, this gives 10
if count%2==0:
lastpage=int(str(count/2).replace('.0',''))
else:
lastpage=int(str(count/2+(1-(count%2/2))).replace('.0',''))
#else:
#if count%3==0:
#lastpage=int(str(count/3).replace('.0',''))
#else:
#lastpage=int(str(count/3+(1-(count%3/3))).replace('.0',''))
if count==0:
report = ['None']
if daysago>365:
pn=18
else:
pn=18
state=State(report=str(report), calday=calday, complete_date=complete_date,
ld=LD, daysago=daysago, count=count, lastpage=lastpage, pn=pn)
db.session.add(state)
db.session.commit()
if daysago==None:
return 'The NASA server is busy right now. Try again later, or try reducing the size of your parameters.'
else:
try:
#return pagenum
#return str(len(report))
return redirect(url_for('reportout', pagenum=1))
except Exception as e:
#return pagenum
return str(e)
#return 'The NASA server is busy right now. Try again later, or try reducing the size of your parameters.'
elif clear=='Clear':
return render_template('form.html',day_selection=day_selection,LD_selection=LD_selection)#params to populate drop-downs
try:
return render_template('form.html',day_selection=day_selection,LD_selection=LD_selection)#params to populate drop-downs
except:
return "Please enter both your asteroidal parameters."
class PageResult:
def __init__(self, data, page = 1, number = 21):
self.__dict__ = dict(zip(['data', 'page', 'number'], [data, page, number]))
self.full_listing = [self.data[i:i+number] for i in range(0, len(self.data), number)]
def __iter__(self):
for i in self.full_listing[self.page-1]:
yield i
def __repr__(self): #used for page linking
return "/reportout/{}".format(self.page+1) #view the next page
@app.route('/reportout/<pagenum>', methods=['GET'])
def reportout(pagenum):
#time.sleep(6+count*.5)
table_name='states'
column_name='report'
query= f'SELECT {column_name} FROM {table_name}'
reportd=db.session.execute(query)
report=reportd.fetchall()
report = eval(report[0][0])
column_name='pn'
query= f'SELECT {column_name} FROM {table_name}'
pnd=db.session.execute(query)
pn=pnd.fetchall()
pn = int(str(pn[0][0]))
column_name='lastpage'
query= f'SELECT {column_name} FROM {table_name}'
lastpaged=db.session.execute(query)
lastpage=lastpaged.fetchall()
lastpage = int(str(lastpage[0][0]))
column_name='calday'
query= f'SELECT {column_name} FROM {table_name}'
caldayd=db.session.execute(query)
calday=caldayd.fetchall()
calday = str(calday[0][0])
column_name='complete_date'
query= f'SELECT {column_name} FROM {table_name}'
complete_dated=db.session.execute(query)
complete_date=complete_dated.fetchall()
complete_date = str(complete_date[0][0])
column_name='ld'
query= f'SELECT {column_name} FROM {table_name}'
LDd=db.session.execute(query)
LD=LDd.fetchall()
LD=int(str(LD[0][0]))
column_name='daysago'
query= f'SELECT {column_name} FROM {table_name}'
daysagod=db.session.execute(query)
daysago=daysagod.fetchall()
daysago=int(str(daysago[0][0]))
column_name='count'
query= f'SELECT {column_name} FROM {table_name}'
countd=db.session.execute(query)
count=countd.fetchall()
count=int(str(count[0][0]))
#return str(lastpage)
return render_template('form2.html', report=PageResult(report, int(pagenum), pn),
calday=calday,complete_date=complete_date,LD=LD,
daysago=daysago,count=count,lastpage=int(str(lastpage).replace('.0','')))
@app.errorhandler(500)
def server_overloaded(error):
return render_template('500.html'), 500
@click.command(name='create_tables')
@with_appcontext
def create_tables():
db.create_all()
app.cli.add_command(create_tables)
if __name__ == '__main__':
app.run()