-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathupdate.py
73 lines (57 loc) · 1.95 KB
/
update.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
import redis
import json
import urllib2
import datetime
from calendar import timegm
import time
import os
import sys
REDIS_URL = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
r = redis.StrictRedis.from_url(REDIS_URL)
# NASA's station FDO updates this page with very precise data. Only using a
# small bit of it for now.
url = "http://spaceflight.nasa.gov/realdata/sightings/SSapplications/Post/JavaSSOP/orbit/ISS/SVPOST.html"
def update_tle():
# Open a http request
req = urllib2.Request(url)
response = urllib2.urlopen(req)
data = response.read()
# parse the HTML
data = data.split("<PRE>")[1]
data = data.split("</PRE>")[0]
data = data.split("Vector Time (GMT): ")[1:]
for group in data:
# Time the vector is valid for
datestr = group[0:17]
# parse date string
tm = time.strptime(datestr, "%Y/%j/%H:%M:%S")
# change into more useful datetime object
dt = datetime.datetime(tm[0], tm[1], tm[2], tm[3], tm[4], tm[5])
# Debug
#print dt
# More parsing
tle = group.split("TWO LINE MEAN ELEMENT SET")[1]
tle = tle[8:160]
lines = tle.split('\n')[0:3]
# Most recent TLE
now = datetime.datetime.utcnow()
if (dt - now).days >= 0:
# Debug Printing
"""
print dt
for line in lines:
print line.strip()
print "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
"""
tle = json.dumps([lines[0].strip(), lines[1].strip(), lines[2].strip()])
r.set("iss_tle", tle)
r.set("iss_tle_time", timegm(dt.timetuple()))
r.set("iss_tle_last_update", timegm(now.timetuple()))
break
if __name__ == '__main__':
print "Updating ISS TLE from JSC..."
try:
update_tle()
except:
exctype, value = sys.exc_info()[:2]
print "Error:", exctype, value