-
Notifications
You must be signed in to change notification settings - Fork 2
/
skynet.py
executable file
·260 lines (190 loc) · 7.48 KB
/
skynet.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
#!/usr/bin/python3
import argparse
import logging
import sys
sys.path.append('/home/autoc4/.pyenv/versions/3.4.0/lib/python3.4/site-packages/')
from paho.mqtt import client as mqtt_client
from bs4 import BeautifulSoup
from datetime import datetime
import json
import re
import threading
import time
import urllib.request
import config
import helpers
MONTH_NAME_TO_INT = {
'Jan': 1,
'Feb': 2,
'Mar': 3,
'Apr': 4,
'May': 5,
'Jun': 6,
'Jul': 7,
'Aug': 8,
'Sep': 9,
'Oct': 10,
'Nov': 11,
'Dec': 12,
}
class IssParser():
url = 'http://www.heavens-above.com/PassSummary.aspx?satid=25544&lat=50.9502&lng=6.9131&loc=6A&alt=51&tz=CET'
base_url = 'http://www.heavens-above.com/'
@staticmethod
def get_iss_data():
response = urllib.request.urlopen(IssParser.url)
bs = BeautifulSoup(response, 'html.parser')
if bs.text.find('No visible passes found within the search period') >= 0:
return []
table, = bs.select('table.standardTable')
keys = [
'Date',
'Brightness (mag)',
'Start Time',
'Start Alt.',
'Start Az.',
'Highest Point Time',
'Highest Point Alt.',
'Highest Point Az.',
'End Time',
'End Alt.',
'End Az.',
'Pass type',
]
rows = table.select('tr.clickableRow')
row_dicts = []
for row in rows:
values = [td.getText().strip() for td in row.findAll('td')]
row_dict = IssParser.augment_row_info(dict(zip(keys, values)))
row_dict['url'] = IssParser.base_url + row.find('a').get('href')
row_dict['type'] = 'iss'
row_dicts.append(row_dict)
return row_dicts
ALT_RE = re.compile(r'(\d+)°')
@staticmethod
def augment_row_info(row_dict):
row_dict['Start timestamp'] = int(IssParser.parse_time(row_dict, 'Start Time').timestamp())
row_dict['Highest Point timestamp'] = int(IssParser.parse_time(row_dict, 'Highest Point Time').timestamp())
row_dict['End timestamp'] = int(IssParser.parse_time(row_dict, 'End Time').timestamp())
row_dict['timestamp'] = row_dict['Highest Point timestamp']
row_dict['altitude_deg'] = int(re.match(IssParser.ALT_RE, row_dict['Highest Point Alt.']).group(1))
row_dict['brightness_float'] = float(row_dict['Brightness (mag)'])
return row_dict
TIME_RE = re.compile(r'(\d\d):(\d\d):(\d\d)')
DATE_RE = re.compile(r'(\d+) ([A-Z][a-z][a-z])')
@staticmethod
def parse_time(r, time_field):
day, month = re.match(IssParser.DATE_RE, r['Date']).groups()
hour, minute, second = re.match(IssParser.TIME_RE, r[time_field]).groups()
dt = datetime(
datetime.now().year,
MONTH_NAME_TO_INT[month],
int(day),
int(hour),
int(minute),
int(second),
)
return dt
class IridiumParser():
url = 'http://www.heavens-above.com/IridiumFlares.aspx?lat=50.9502&lng=6.9131&loc=6A&alt=51&tz=CET'
base_url = 'http://www.heavens-above.com/'
@staticmethod
def get_iridium_data():
response = urllib.request.urlopen(IridiumParser.url)
bs = BeautifulSoup(response, 'html.parser')
#data = response.read().decode()
table, = bs.select('table.standardTable')
thead = table.find('thead')
tbody = table.find('tbody')
keys = [td.getText().strip() for td in thead.find('tr').findAll('td')]
rows = tbody.findAll('tr')
row_dicts = []
for row in rows:
values = [td.getText().strip() for td in row.findAll('td')]
row_dict = IridiumParser.augment_row_info(dict(zip(keys, values)))
row_dict['url'] = IridiumParser.base_url + row.find('a').get('href')
row_dict['type'] = 'iridium'
row_dicts.append(row_dict)
return row_dicts
ALT_RE = re.compile(r'(\d+)°')
AZI_RE = re.compile(r'(\d+)° \((\w+)\)')
SAT_RE = re.compile(r'Iridium (\d+)')
@staticmethod
def augment_row_info(row_dict):
dt = IridiumParser.parse_time_string(row_dict['Time'])
row_dict['timestamp'] = int(dt.timestamp())
row_dict['altitude_deg'] = int(re.match(IridiumParser.ALT_RE, row_dict['Altitude']).group(1))
row_dict['azimuth_deg'] = int(re.match(IridiumParser.AZI_RE, row_dict['Azimuth']).group(1))
row_dict['satellite_num'] = int(re.match(IridiumParser.SAT_RE, row_dict['Satellite']).group(1))
row_dict['brightness_float'] = float(row_dict['Brightness'])
return row_dict
TIME_RE = re.compile(r'([A-Z][a-z][a-z]) (\d+), (\d\d):(\d\d):(\d\d)')
@staticmethod
def parse_time_string(s):
month, day, hour, minute, second = re.match(IridiumParser.TIME_RE, s).groups()
dt = datetime(
datetime.now().year,
MONTH_NAME_TO_INT[month],
int(day),
int(hour),
int(minute),
int(second),
)
return dt
class MQTT_Skynet_Thread(threading.Thread):
"""
Publishes the current time in regular intervals.
"""
interval = 60 * 60 * 3
topic = 'skynet'
def __init__(self, mqtt_thread, *args, **kwargs):
super(MQTT_Skynet_Thread, self).__init__(*args, daemon=True, **kwargs)
self.mqtt_thread = mqtt_thread
def run(self):
try:
self.main_loop()
except:
logging.exception('Skynet thread exception, exiting.')
def main_loop(self):
while not self.mqtt_thread.connection_established:
time.sleep(0.1)
logging.info('skynet thread started')
while True:
try:
self.poll_data()
except (urllib.error.URLError, TimeoutError) as e:
logging.info('error fetching data: {}'.format(e.__class__.__name__))
time.sleep(60)
continue
time.sleep(self.interval)
def poll_data(self):
# data = IssParser.get_iss_data() + IridiumParser.get_iridium_data()
data = IssParser.get_iss_data()
data.sort(key=lambda x:x['timestamp'])
self.mqtt_thread.mqtt_client.publish(self.topic, json.dumps(data), retain=True)
def to_str(i):
i['ptime'] = datetime.fromtimestamp(i['timestamp']).strftime('%b %d, %H:%M:%S')
if i['type'] == 'iss':
return 'ISS {brightness_float: 1.1f} {altitude_deg:2}° - {ptime}'.format(**i)
else:
return 'Ir{satellite_num} {brightness_float: 1.1f} {altitude_deg:2}° {azimuth_deg:3}° {ptime}'.format(**i)
def main():
parser = argparse.ArgumentParser(
description='MQTT Skynet Poller',
parents=[helpers.get_default_parser()],
)
args = parser.parse_args()
helpers.configure_logging(args.logging_type, args.loglevel, args.logfile)
logging.info('starting')
mqtt_thread = helpers.MQTT_Client('skynet', keepalive=60, heartbeat=True, daemon=True)
mqtt_thread.start()
skynet_thread = MQTT_Skynet_Thread(mqtt_thread)
skynet_thread.start()
while mqtt_thread.is_alive() and skynet_thread.is_alive():
time.sleep(1)
logging.info('exiting')
sys.stdout.flush()
sys.stderr.flush()
sys.exit(1)
if __name__ == "__main__":
main()