-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.py
69 lines (50 loc) · 1.6 KB
/
checker.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
#!/usr/bin/python3.4
from urllib.request import urlopen
from bs4 import BeautifulSoup
from dateutil.parser import parse
from datetime import datetime
import re
class AuroraChecker:
url = ''
kpThreshold = 0
def __init__(self, url, kpThreshold):
self.url = url
self.kpThreshold = kpThreshold
def checkFuture(self, endTime):
isFuture = False
currentTime = datetime.utcnow()
if currentTime <= endTime:
isFuture = True
return(isFuture)
def getAlertList(self):
try:
response = urlopen(self.url, timeout=5)
except:
return False
html = response.read()
soup = BeautifulSoup(html, 'html.parser')
unformatedTable = soup.find_all('pre')[0].get_text()
tableList = unformatedTable.replace(" ", "").split('\n')
unformatedDates = tableList.pop(0)
dateLength = 5
formatedDates = [unformatedDates[i:i+dateLength] for i in range(0, len(unformatedDates), dateLength)]
for obj in enumerate(formatedDates, start = 0):
formatedDates[obj[0]] = parse(obj[1])
alertList = list()
forecastList = list()
for data in tableList:
if data:
data = re.sub(r'\([^)]*\)', '', data)
unformated = data.split('UT')
hours = unformated[0].split('-')
values = re.findall('.', unformated[1])
for obj in enumerate(values, start = 0):
kp = int(obj[1])
forecast = formatedDates[obj[0]], hours[0], hours[1], kp
endTime = formatedDates[obj[0]].replace(hour = int(hours[1]))
forecastList.append(forecast)
isFuture = self.checkFuture(endTime)
if isFuture and kp >= self.kpThreshold:
alertList.append(forecast)
alertList.sort()
return(alertList)