-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
134 lines (119 loc) · 7.52 KB
/
main.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
import json
import schedule
import time
import requests
from random import choice
import logging
import datetime
prev = None
counter = 0
def check_service_status():
now = datetime.datetime.now()
global counter
print(counter, '. i am working', now)
counter += 1
logging.basicConfig(level=logging.INFO,
filename="py_log.log", filemode="w", format="%(asctime)s %(levelname)s %(message)s")
desktop_agents = ['Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0']
def random_headers():
return {'User-Agent': choice(desktop_agents), 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'}
URL_TEMPLATE = "https://www.apple.com/support/systemstatus/data/developer/system_status_en_US.js"
r = requests.get(URL_TEMPLATE, headers=random_headers())
text = r.text
if "jsonCallback" in r.text:
text = text.replace("jsonCallback", "", 1)
text = text.replace('(', "")
text = text.replace(')', "")
text = text.replace(';', "")
now = json.loads(text)
global prev
if prev is None:
prev = now
return
def add_circle_to_status(word):
if word == 'Available':
new_word = ':large_green_circle:'
elif word == 'Outage':
new_word = ':red_circle:'
elif word == 'Issue':
new_word = ':large_yellow_circle:'
elif word == 'Maintenance':
new_word = ':wrench:'
elif word == "Performance":
new_word = ":large_blue_circle:"
return new_word
def send_message_slack(service_name, service_status, service_message):
headers = {'Content-type': 'application/json', }
json_data = {
'text': f':mega: Name: *{service_name} * \n:desktop_computer: Status: {service_status} {add_circle_to_status(service_status)} \n:email: Message: {service_message} '}
requests.post('your_slack_hoock',
headers=headers, json=json_data,)
return
for index, key in enumerate(now["services"]):
if key['events']:
if len(prev['services'][index]['events']) == 0: # Если раньше было пусто
serv_stat = now['services'][index]['events'][0]['statusType']
start = now['services'][index]['events'][0]['startDate']
end = now['services'][index]['events'][0]['endDate']
serv_name = now['services'][index]['serviceName']
if now['services'][index]['events'][0]['eventStatus'] == 'upcoming': # Если обслуживание
send_message_slack(
serv_name, serv_stat, f'Maintenance scheduled. Start at *{start}*, end at *{end}*')
logging.info('From 0 to Maintenance')
else:
send_message_slack(
serv_name, serv_stat, f'Repair work on the server has been started *{start}*')
logging.info('From 0 to Issue or Outage')
print(now['services'][index]['events'][0]['eventStatus'])
else: # Если пусто не было
for index2, key2 in enumerate(key['events']):
# считаем длину у было и стало
if len(prev['services'][index]['events']) == len(key['events']):
serv_stat = now['services'][index]['events'][index2]['statusType']
start = now['services'][index]['events'][index2]['startDate']
end = now['services'][index]['events'][index2]['endDate']
serv_name = now['services'][index]['serviceName']
# Если ongoing->resolved
if key2['eventStatus'] == 'resolved' and prev['services'][index]['events'][index2]['eventStatus'] != 'resolved':
send_message_slack(
serv_name, 'Available', f'Repair work completed *{end}*')
logging.info('Ongoing -> resolved')
if key2['eventStatus'] == 'ongoing' and prev['services'][index]['events'][index2]['eventStatus'] == 'resolved':
send_message_slack(
serv_name, serv_stat, f'Repair work on the server has been started *{start}*')
logging.info('Resolved->Ongoing')
if key2['eventStatus'] == 'upcoming' and prev['services'][index]['events'][index2]['eventStatus'] != 'upcoming':
send_message_slack(
serv_name, serv_stat, f'Maintenance scheduled. Start at *{start}*, end at *{end}*')
logging.info('Maintenance after somethink')
else: # Если len now > len prev
if len(prev['services'][index]['events']) < len(key['events']):
if key2['eventStatus'] == 'upcoming' or key2['eventStatus'] == 'ongoing':
serv_stat = now['services'][index]['events'][index2]['statusType']
start = now['services'][index]['events'][index2]['startDate']
end = now['services'][index]['events'][index2]['endDate']
serv_name = now['services'][index]['serviceName']
if key2['eventStatus'] == 'upcoming':
send_message_slack(
serv_name, serv_stat, f'Maintenance scheduled. Start at *{start}*, end at *{end}*')
logging.info('New maintenance')
else:
send_message_slack(
serv_name, serv_stat, f'Repair work on the server has been started *{start}*')
print(key2['eventStatus'])
logging.info('New ongoing')
prev = now
check_service_status()
schedule.every(10).minutes.do(check_service_status)
while True:
schedule.run_pending()
time.sleep(1)