-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_beta.py
More file actions
202 lines (174 loc) · 6.03 KB
/
main_beta.py
File metadata and controls
202 lines (174 loc) · 6.03 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Author: i2cy(i2cy@outlook.com)
# Filename: test_main
# Created on: 2020/9/14
from api.lewdity_api import *
from api.qqbot_api import *
import threading
import time
import os
LOOP_TIME = 1
CACHE_DIR = "codyCache/"
REFRESHING_TIME = 3600*24
MUTE_THRESHOLD = 2
MUTE_TIME = 3600*2*0
EVENTS = []
LIVE = True
MUTE_LIST = {}
TOTAL_CLASSIFICATED = 0
"""
{"<QQID>":{
"lewd_time": <lewd_count>
}}
"""
def path_fixer(path): # path checker
chk = ""
for i in path:
chk += i
if i in ("/", "\\"):
if not os.path.exists(chk):
os.mkdir(chk)
def event_loop():
global EVENTS, Cody
while LIVE:
try:
EVENTS += Cody.getEvent()
except:
continue
time.sleep(LOOP_TIME)
for i in Cody.managingGroups:
Cody.sendGroupMsg("event listening loop thread stopped", i)
def check_and_mute(event):
global Cody, TOTAL_CLASSIFICATED, MUTE_LIST
if event["Type"] != "GroupMsg":
return "ignored"
groupID = str(event["FromGroup"]["GIN"])
if not groupID in Cody.managingGroups:
return "ignored group {}".format(groupID)
msg = event["Msg"]["Text"]
pic_urls = Cody.getPicUrl(msg, groupID)
if pic_urls == []:
return "not picture msg"
info = ""
for pic_url in pic_urls:
print(pic_url)
pic_hash = pic_url["hash"]
pic_url = pic_url["url"]
pic_filename = "{}{}".format(CACHE_DIR, pic_hash)
if os.path.exists(pic_filename+".jpeg"):
pic_filename = pic_filename+".jpeg"
res = {"lewd": True}
elif os.path.exists(pic_filename+".png"):
pic_filename = pic_filename+".png"
res = {"lewd": True}
elif os.path.exists(pic_filename+".gif"):
pic_filename = pic_filename+".gif"
res = {"lewd": True}
else:
try:
pic_filename = Cody.downloadPic(pic_url, pic_filename)
except:
try:
pic_filename = Cody.downloadPic(pic_url, pic_filename)
except Exception as err:
info = "failed twice to get picture file, {}".format(str(err))
continue
if pic_filename == None:
info = "failed to get picture file"
continue
TOTAL_CLASSIFICATED += 1
print("classificating picture \"{}\", total amount {}".format(
pic_filename, TOTAL_CLASSIFICATED
))
res = classificateAPI(pic_filename)
print("classification result: \n{}".format(res))
if not res["lewd"]:
os.remove(pic_filename)
info = "not lewd"
continue
targetQQID = str(event["FromQQ"]["UIN"])
msgReq = event["Msg"]["Req"]
msgRandom = event["Msg"]["Random"]
Cody.sendGroupMsg("Cody beta have detected something lewd", groupID)
Cody.sendGroupMsg("internal data:\n{}".format(str(res)), groupID)
Cody.deleteGroupMsg(groupID, msgRandom, msgReq)
if targetQQID in MUTE_LIST.keys():
MUTE_LIST[targetQQID]["lewd_time"] += 1
else:
MUTE_LIST.update({targetQQID:{
"lewd_time": 1
}})
if MUTE_LIST[targetQQID]["lewd_time"] >= MUTE_THRESHOLD:
print("muting {} for {} seconds in group {}".format(
targetQQID,
MUTE_TIME,
groupID
))
ret = Cody.mute(targetQQID, MUTE_TIME, groupID)
print("muting status: {}".format(str(ret)))
return "lewd detected"
return info
def check_loop():
global EVENTS, MUTE_LIST, Cody
time_stamp = time.time()
while LIVE:
try:
if time.time() - time_stamp > REFRESHING_TIME:
for i in MUTE_LIST:
MUTE_LIST[i]["lewd_time"] = MUTE_LIST[i]["lewd_time"] - 1
events = EVENTS
EVENTS = []
if events == []:
time.sleep(LOOP_TIME/4)
continue
for i in events:
try:
ret = check_and_mute(i)
print("event checking status with text \"{}\": {}".format(
i["Msg"]["Text"], ret
))
except Exception as err:
try:
print("failed to check event with text \"{}\", {}, retring".format(
i["Msg"]["Text"], err
))
ret = check_and_mute(i)
except Exception as err:
print("failed again to check event with text \"{}\", {}".format(
i["Msg"]["Text"], err))
except Exception as err:
print("error in checking loop,", err)
for i in Cody.managingGroups:
try:
Cody.sendGroupMsg("checking loop thread stopped", i)
except Exception as err:
print("failed to send start message,", err)
def main():
global LIVE
init_cnn()
init_qqbot()
print("checking cache path")
path_fixer(CACHE_DIR)
eventLoop = threading.Thread(target=event_loop)
eventLoop.start()
print("events listening loop started")
checkloop = threading.Thread(target=check_loop)
checkloop.start()
print("qqbot api watchdog started")
time.sleep(2)
for i in Cody.managingGroups:
try:
Cody.sendGroupMsg("all threads started", i)
except Exception as err:
print("failed to send boot message, ", err)
while True:
try:
ctrl = input('(use Ctrl+C to exit)\n')
except KeyboardInterrupt:
LIVE = False
time.sleep(LOOP_TIME)
Cody.closeAPI()
exit(0)
if __name__ == "__main__":
main()