-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.py
79 lines (62 loc) · 2.15 KB
/
sign.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
import time
import requests
import threading
class Sign:
def __init__(self, host, port):
self.host = host
self.port = port
self.messages = []
def clear(self):
# Should probably implement this on the other side
requests.post("http://{}:{}/clear".format(self.host, self.port))
self.messages = []
def new_message(self, *args, **kwargs):
msg = Message(*args, sign=self, **kwargs)
msg.add()
self.messages.append(msg)
return msg
class Message:
def __init__(self, message, name=None, effects=[], priority=5,
lifetime=None, expiration=None, sign=None):
self.message = message
self.effects = effects
self.priority = priority
self.lifetime = lifetime
self.expiration = expiration
self.name = name
self.sign = sign
def update(self, local=False, **kwargs):
valid = {k: v for k, v in kwargs.items() if v != None and k in (
"message", "effects", "priority", "lifetime", "expiration"
)}
self.__dict__.update(valid)
if not local:
self.add()
return self
def get_expiration(self):
if self.lifetime:
return time.time() + self.lifetime
elif self.expiration:
return self.expiration
else:
return 2147483647 # meh
def add(self):
threading.Thread(target=self._add_request).start()
def remove(self):
threading.Thread(target=self._remove_request).start()
def _add_request(self):
data = {
"text": self.message,
"effects": ",".join(self.effects).lower(),
"priority": self.priority,
"expiration": self.get_expiration()
}
if self.name:
data["name"] = self.name
r = requests.post("http://{}:{}/add_message".format(self.sign.host, self.sign.port), data=data)
r.raise_for_status()
self.name = r.text
def _remove_request(self):
if self.name:
r = requests.post("http://{}:{}/remove_message/{}".format(
self.sign.host, self.sign.port, self.name))