-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalerts.py
137 lines (105 loc) · 3.3 KB
/
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
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
import sys, json, asyncio
import configurator
log = configurator.LOG.get()
cfg = configurator.CFG().load("settings.json")
class Alerts():
def __init__(self):
modules = ("websockets", "requests")
# Check if required modules are imported
for module in modules:
if module not in sys.modules or module not in globals():
log.info(f"Importing modules: {modules}")
global websockets, requests
import websockets, requests
break
async def startReceiving(self):
if "json" not in sys.modules:
global json
import json
log.info("Connecting to Pushbullet WSS stream")
async with websockets.connect(f"wss://stream.pushbullet.com/websocket/{cfg['pb_token']}") as websocket:
while True:
data = await websocket.recv()
log.info(f"Received data! :{data}:")
print(f"Received data! :{data}:")
if "push" and "application_name" in data:
self.handleData(data)
def handleData(self, data):
json_data = json.loads(data)
name, msg, amount = "", "", ""
if json_data["push"]["application_name"] == "MobilePay" or json_data["push"]["title"] == "MobilePay":
log.info("Received donation!")
log.info(f"Raw data: {json_data}")
log.info("Splitting donation data")
body = json_data["push"]["body"]
body = body.split()
# Amount
log.info("Getting donation amount")
amount = body[3]
amount = amount.replace(",", ".", 1)
# Name
log.info("Getting donation name")
if body[7] == "billede":
name = body[9][1:]
else:
name = body[6][1:]
name = name.strip()
# Message
log.info("Getting donation message")
for i in range(len(body)):
if ":" in body[i]:
msg = body[i+1:]
msg = " ".join(msg)
break
else:
msg = ""
log.info(f"Donation data: {name} - {amount} - \"{msg}\"")
elif json_data["push"]["title"].lower() == "test notification" and json_data["push"]["application_name"].lower() == "pushbullet":
log.info("Received test notification via the Pushbullet API")
print("Received test notification via the Pushbullet API")
self.testAlert()
else:
log.info(f"Unknown data received: {json_data}")
if name.strip() == "":
name = cfg["default_name"]
if msg.strip() == "":
msg = cfg["default_msg"]
if amount.strip() == "":
amount = None
else:
log.info("Triggering donation alert")
self.triggerAlert(name, msg, amount)
def triggerAlert(self, name, msg, amount):
url = "https://streamlabs.com/api/v1.0/donations"
data = {
"access_token" :cfg["sl_token"],
"name" :name,
"identifier" :"MobilePay",
"amount" :amount,
"currency" :"DKK",
"message" :msg,
}
self.request(url, data)
def testAlert(self):
print("Test alert!")
log.info("Sending test alert")
url = "https://streamlabs.com/api/v1.0/alerts/send_test_alert"
data = {
"access_token":cfg['sl_token'],
"type":"donation",
}
self.request(url, data)
def request(self, url, data):
response = requests.post(url, data)
print(f"Alert request returned: \"{response.text}\"")
log.info(f"Alert request returned: {response}")
if len(sys.argv) < 2:
print("No function was specified")
log.error("No function was specified")
else:
log.info(f"Calling function '{sys.argv[1]}'")
# Start function specified in argument
try:
asyncio.run(getattr(Alerts(), sys.argv[1])())
except Exception as err:
print(err)