-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathz_manage_alerts.py
82 lines (69 loc) · 2.79 KB
/
z_manage_alerts.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
import json
import datetime
from urllib.request import urlopen, Request
import time
from api_keys import coinigykey, coinigysec
class AlertManager:
def __init__(self, coinigikey, coinigysec, print_output=True):
"""
Object used to manage coinigy alerts
:param coinigikey: string, coinigy key
:param coinigysec: string, coinigy secret
"""
self.coinigykey = coinigykey
self.coinigysec = coinigysec
self.print_output = print_output
@staticmethod
def _get_old_alerts():
# get old alerts
headers = {'Content-Type': 'application/json', 'X-API-KEY': coinigykey, 'X-API-SECRET': coinigysec}
values = '{"exch_code": "BTRX"}'
values = bytes(values, encoding='utf-8')
request = Request('https://api.coinigy.com/api/v1/alerts', data=values, headers=headers)
old_alerts = urlopen(request).read()
old_alerts = old_alerts.decode("utf-8")
old_alerts = json.loads(old_alerts)
return old_alerts
@staticmethod
def _api_delete_alert(alert_id):
"""
Api call to delete alert
:param alert_id: srting, id of the alert to delete
:return:
"""
body = '{"alert_id": ' + alert_id + '}'
body = bytes(body, encoding='utf-8')
headers = {'Content-Type': 'application/json', 'X-API-KEY': coinigykey, 'X-API-SECRET': coinigysec}
request = Request('https://api.coinigy.com/api/v1/deleteAlert', data=body, headers=headers)
response_body = urlopen(request).read()
time.sleep(1)
return response_body
def delete_alerts_newer_than(self, newer_than):
"""
Deletes all alerts created after 'newer_than'
:param newer_than: datetime object
:return:
"""
old_alerts = self._get_old_alerts()
for alert in old_alerts['data']['open_alerts']:
if datetime.datetime.strptime(alert['alert_added'], '%Y-%m-%d %H:%M:%S') > newer_than:
resp_body = self._api_delete_alert(alert['alert_id'])
if self.print_output:
print(resp_body)
def delete_scanner_alerts(self):
"""
Deletes all alerts created by the scanner
:return:
"""
old_alerts = self._get_old_alerts()
for alert in old_alerts['data']['open_alerts']:
if "z_base_scanner" in alert['alert_note']:
resp_body = self._api_delete_alert(alert['alert_id'])
if self.print_output:
print(resp_body)
if __name__ == "__main__":
##############VARIABLES TO SET
from_date = datetime.datetime(year=2017, month=12, day=1, hour=23)
##############VARIABLES TO SET
alerts = AlertManager(coinigykey, coinigysec)
alerts.delete_alerts_newer_than(from_date)