This repository has been archived by the owner on May 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
component_notification_faker.py
93 lines (66 loc) · 2.76 KB
/
component_notification_faker.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
import asyncio
from autobahn.asyncio.wamp import ApplicationSession, ApplicationRunner
from autobahn.wamp.types import RegisterOptions, PublishOptions
from autobahn.wamp import auth
import aioredis
from config import config
import random
import uuid
class NotificationFakerComponent:
@classmethod
def run(cls):
print(f"Starting {cls.__name__}...")
url = f"ws://{config['crossbar']['host']}:{config['crossbar']['port']}"
runner = ApplicationRunner(url=url, realm=config["crossbar"]["realm"])
runner.run(NotificationFakerWAMPComponent)
class NotificationFakerWAMPComponent(ApplicationSession):
def __init__(self, c=None):
super().__init__(c)
self.channels = dict()
def onConnect(self):
self.join(config["crossbar"]["realm"], ["wampcra"], config["crossbar"]["auth"]["username"])
def onDisconnect(self):
print("Disconnected from Crossbar!")
def onChallenge(self, challenge):
secret = config["crossbar"]["auth"]["password"]
signature = auth.compute_wcs(secret.encode('utf8'), challenge.extra['challenge'].encode('utf8'))
return signature.decode('ascii')
async def onJoin(self, details):
self.redis_client = await self._initialize_redis_client()
for channel in config["channels"]:
self.channels[channel] = False
while True:
await asyncio.sleep(random.uniform(5.0, 30.0))
channel = random.choice(config["channels"])
online = not self.channels[channel]
self.channels[channel] = online
redis_key = f"{config['redis']['prefix']}:CHANNEL_ID:{channel}"
channel_id = await self.redis_client.get(redis_key)
channel_id = channel_id.decode("utf-8")
if online:
topic = "CHANNEL:ONLINE"
payload = {
"channel_id": channel_id,
"game_id": random.choice(["494717", "488191", "417752"]),
"type": random.choice(["live", "vodcast"]),
"title": f"{config['discord']['server']} - {uuid.uuid4()}"
}
else:
topic = "CHANNEL:OFFLINE"
payload = {
"channel_id": channel_id
}
message = {
"topic": topic,
"payload": payload
}
topic = message.pop("topic")
self.publish(topic, message["payload"])
print(topic, message)
async def _initialize_redis_client(self):
return await aioredis.create_redis(
(config["redis"]["host"], config["redis"]["port"]),
loop=asyncio.get_event_loop()
)
if __name__ == "__main__":
NotificationFakerComponent.run()