-
Notifications
You must be signed in to change notification settings - Fork 0
/
broadcast_sql.py
140 lines (98 loc) · 3.64 KB
/
broadcast_sql.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
138
139
140
import threading
from sqlalchemy import Column, String, UnicodeText, distinct, func
from . import BASE, SESSION
class CatBroadcast(BASE):
__tablename__ = "catbroadcast"
keywoard = Column(UnicodeText, primary_key=True)
group_id = Column(String(14), primary_key=True, nullable=False)
def __init__(self, keywoard, group_id):
self.keywoard = keywoard
self.group_id = str(group_id)
def __repr__(self):
return "<Cat Broadcast channels '%s' for %s>" % (self.group_id, self.keywoard)
def __eq__(self, other):
return bool(
isinstance(other, CatBroadcast)
and self.keywoard == other.keywoard
and self.group_id == other.group_id
)
CatBroadcast.__table__.create(checkfirst=True)
CATBROADCAST_INSERTION_LOCK = threading.RLock()
class BROADCAST_SQL:
def __init__(self):
self.BROADCAST_CHANNELS = {}
BROADCAST_SQL_ = BROADCAST_SQL()
def add_to_broadcastlist(keywoard, group_id):
with CATBROADCAST_INSERTION_LOCK:
broadcast_group = CatBroadcast(keywoard, str(group_id))
SESSION.merge(broadcast_group)
SESSION.commit()
BROADCAST_SQL_.BROADCAST_CHANNELS.setdefault(keywoard, set()).add(str(group_id))
def rm_from_broadcastlist(keywoard, group_id):
with CATBROADCAST_INSERTION_LOCK:
if broadcast_group := SESSION.query(CatBroadcast).get(
(keywoard, str(group_id))
):
if str(group_id) in BROADCAST_SQL_.BROADCAST_CHANNELS.get(keywoard, set()):
BROADCAST_SQL_.BROADCAST_CHANNELS.get(keywoard, set()).remove(
str(group_id)
)
SESSION.delete(broadcast_group)
SESSION.commit()
return True
SESSION.close()
return False
def is_in_broadcastlist(keywoard, group_id):
with CATBROADCAST_INSERTION_LOCK:
broadcast_group = SESSION.query(CatBroadcast).get((keywoard, str(group_id)))
return bool(broadcast_group)
def del_keyword_broadcastlist(keywoard):
with CATBROADCAST_INSERTION_LOCK:
broadcast_group = (
SESSION.query(CatBroadcast.keywoard)
.filter(CatBroadcast.keywoard == keywoard)
.delete()
)
BROADCAST_SQL_.BROADCAST_CHANNELS.pop(keywoard)
SESSION.commit()
def get_chat_broadcastlist(keywoard):
return BROADCAST_SQL_.BROADCAST_CHANNELS.get(keywoard, set())
def get_broadcastlist_chats():
try:
chats = SESSION.query(CatBroadcast.keywoard).distinct().all()
return [i[0] for i in chats]
finally:
SESSION.close()
def num_broadcastlist():
try:
return SESSION.query(CatBroadcast).count()
finally:
SESSION.close()
def num_broadcastlist_chat(keywoard):
try:
return (
SESSION.query(CatBroadcast.keywoard)
.filter(CatBroadcast.keywoard == keywoard)
.count()
)
finally:
SESSION.close()
def num_broadcastlist_chats():
try:
return SESSION.query(func.count(distinct(CatBroadcast.keywoard))).scalar()
finally:
SESSION.close()
def __load_chat_broadcastlists():
try:
chats = SESSION.query(CatBroadcast.keywoard).distinct().all()
for (keywoard,) in chats:
BROADCAST_SQL_.BROADCAST_CHANNELS[keywoard] = []
all_groups = SESSION.query(CatBroadcast).all()
for x in all_groups:
BROADCAST_SQL_.BROADCAST_CHANNELS[x.keywoard] += [x.group_id]
BROADCAST_SQL_.BROADCAST_CHANNELS = {
x: set(y) for x, y in BROADCAST_SQL_.BROADCAST_CHANNELS.items()
}
finally:
SESSION.close()
__load_chat_broadcastlists()