This repository has been archived by the owner on Oct 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
function.py
2040 lines (1954 loc) · 73.3 KB
/
function.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#coding=utf-8
from mirai import Mirai, Plain, MessageChain, Friend, Image, Group, protocol, Member, At, Face, JsonMessage, LightApp
import os, random, shutil
from os.path import join, getsize
from PIL import Image as IMG
from io import BytesIO
import re
import requests
from urllib import parse
import pymysql
from itertools import chain
import wmi
import hashlib
import string
from urllib.parse import quote
from pynvml import *
import base64
import io
from variable import *
import imagehash
# 从config中获取配置
def getConfig(config):
with open('config.json', 'r', encoding='utf-8') as f: # 从json读配置
configs = json.loads(f.read())
if config in configs.keys():
return configs[config]
else:
print("getConfig Error:%s"%config)
BotQQ = getConfig("BotQQ") # 字段 qq 的值
HostQQ = getConfig("HostQQ") #主人QQ
dbPass = getConfig("dbPass")
host = getConfig("dbHost")
user = getConfig("dbUser")
db = getConfig("dbName")
settingCode={"Disable":0,"Enable":1,"on":1,"off":0,"Local":1,"Net":0,"normal":"normal","zuanLow":"zuanLow","zuanHigh":"zuanHigh","rainbow":"rainbow","chat":"chat","online":"online","offline":"offline","wyy":"wyy","qq":"qq","off":"off"}
# 初始化city列表
city=[]
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "select cityZh from city"
cur.execute(sql)
data = cur.fetchall()
city=list(chain.from_iterable(data))
cur.close()
conn.close()
# 数据更新
def updateData(data,operationType):
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
if operationType=='setu':
sql = "UPDATE calledCount SET setuCalled=%d"%data
elif operationType=='real':
sql = "UPDATE calledCount SET realCalled=%d"%data
elif operationType=='bizhi':
sql = "UPDATE calledCount SET bizhiCalled=%d"%data
elif operationType=='weather':
sql = "UPDATE calledCount SET weatherCalled=%d"%data
elif operationType=='response':
sql = "UPDATE calledCount SET responseCalled=%d"%data
elif operationType=='clock':
sql = "UPDATE calledCount SET clockCalled=%d"%data
elif operationType=='search':
sql = "UPDATE calledCount SET searchCount=%d"%data
elif operationType=='botSetuCount':
sql = "UPDATE calledCount SET botSetuCount=%d"%data
elif operationType=='predict':
sql = "UPDATE calledCount SET predictCount=%d"%data
elif operationType=='yellow':
sql = "UPDATE calledCount SET yellowPredictCount=%d"%data
elif operationType=='quotes':
sql = "UPDATE calledCount SET quotesCount=%d"%data
else:
print("error: none operationType named %s!"%operationType)
return
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
# 日志记录
def record(operation,picUrl,sender,groupId,result,operationType):
timeNow = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(timeNow)
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
if operationType=='img':
sql = "INSERT INTO imgCalled (time,operation,picUrl,sender,groupId,result) VALUES ('%s','%s','%s',%d,%d,%d)"%(timeNow,operation,pymysql.escape_string(picUrl),sender,groupId,result)
elif operationType=='function':
sql = "INSERT INTO functionCalled (time,operation,sender,groupId,result) VALUES ('%s','%s',%d,%d,%d)"%(timeNow,operation,sender,groupId,result)
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
print("data recorded!")
# 添加群信息(数据库)
def addGroupinit(groupId,groupName):
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = """
INSERT INTO setting
(groupId,groupName,`repeat`,setuLocal,bizhiLocal,countLimit,`limit`,setu,bizhi,`real`,r18,search,speakMode,switch,forbiddenCount)
VALUES
(%d,'%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,'%s','%s',0)
"""%(groupId,groupName,True,True,True,True,6,True,True,True,False,True,"normal","online")
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
print("add group info init finished!")
# 检查有无群组变更(初始化)
def checkGroupInit(groupList):
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "select groupId from setting"
cur.execute(sql)
data = cur.fetchall()
groupId=list(chain.from_iterable(data))
print(groupId)
for i in groupList:
print(i.id,':',i.name)
if i.id not in groupId:
sql = """
INSERT INTO setting
(groupId,groupName,`repeat`,setuLocal,bizhiLocal,countLimit,`limit`,setu,bizhi,`real`,r18,search,imgPredict,yellowPredict,imgLightning,speakMode,switch,forbiddenCount)
VALUES
(%d,'%s',%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,'%s','%s',0)
"""%(i.id,i.name,True,True,True,True,6,False,False,False,False,False,False,False,False,"normal","online")
cur.execute(sql)
sql = """
INSERT INTO admin
(groupId,adminId)
VALUES
(%d,%d)
"""%(i.id,HostQQ)
cur.execute(sql)
sql = "select groupId from admin"
cur.execute(sql)
data = cur.fetchall()
groupId=list(chain.from_iterable(data))
for i in groupList:
if i.id not in groupId:
sql = """
INSERT INTO admin
(groupId,adminId)
VALUES
(%d,%d)
"""%(i.id,HostQQ)
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
# 获取调用次数数据
def getData(data):
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "SELECT %s from calledCount"%data
cur.execute(sql)
data=cur.fetchone()[0]
cur.close()
conn.close()
return data
# 获取本群设置
def getSetting(groupId,name):
sqlKeyWord=["repeat","real","limit"]
if name in sqlKeyWord:
name='`'+name+'`'
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "SELECT %s from setting WHERE groupId=%d"%(name,groupId)
# print(sql)
cur.execute(sql)
data=cur.fetchone()[0]
cur.close()
conn.close()
return data
# 更新本群设置
def updateSetting(groupId,name,new):
strKeyWord=["speakMode","switch","music"]
sqlKeyWord=["repeat","real","limit"]
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
if name in sqlKeyWord:
name='`'+name+'`'
if name in strKeyWord:
sql = "UPDATE setting SET %s='%s' WHERE groupId=%d"%(name,new,groupId)
else:
sql = "UPDATE setting SET %s=%s WHERE groupId=%d"%(name,new,groupId)
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
# 获取本群管理员
def getAdmin(groupId):
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "SELECT adminId from admin WHERE groupId=%d"%groupId
cur.execute(sql)
data=cur.fetchall()
admin=list(chain.from_iterable(data))
cur.close()
conn.close()
return admin
# 是否要对图片做响应
def getReady(groupId,sender,targetDB):
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "SELECT `status` from %s WHERE groupId=%d and memberId=%d"%(targetDB,groupId,sender)
cur.execute(sql)
try:
result=cur.fetchone()[0]
except TypeError:
sql="INSERT INTO %s (groupId,memberId,`status`) VALUES (%d,%d,%d)"%(targetDB,groupId,sender,False)
cur.execute(sql)
conn.commit()
return False
cur.close()
conn.close()
return result
# 修改判断状态
def setReady(groupId,sender,status,targetDB):
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "SELECT `status` from %s WHERE groupId=%d and memberId=%d"%(targetDB,groupId,sender)
cur.execute(sql)
try:
result=cur.fetchone()[0]
sql="UPDATE %s SET `status`=%d WHERE groupId=%d and memberId=%d"%(targetDB,status,groupId,sender)
cur.execute(sql)
except TypeError:
sql="INSERT INTO %s (groupId,memberId,Status) VALUES (%d,%d,%d)"%(targetDB,groupId,sender,status)
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
# 随机图片路径
def randomPic(dir):
pathDir = os.listdir(dir)
# seed = int(time.time())
# random.seed(seed)
dist = random.sample(pathDir, 1)[0]
return dir+dist
# 获取天气
def getWeather(message,sender):
global city
point=message.toString()[25:]
print("天气查询,城市:",point)
if point not in city:
return [
At(target=sender),
Plain(text="请检查城市名称,只支持中国城市及部分地区哦~")
]
weather_src=weatherSrc+point
response=requests.get(weather_src)
html=response.text
html.replace("\/","/")
html.replace("//",'/')
html=html.encode('utf-8').decode('unicode_escape')
wea=re.findall(r'wea":"(.*?)"',html,re.S)[0]
tem=re.findall(r'"tem":"(.*?)"',html,re.S)[0]
tem_day=re.findall(r'"tem_day":"(.*?)"',html,re.S)[0]
tem_night=re.findall(r'"tem_night":"(.*?)"',html,re.S)[0]
win=re.findall(r'"win":"(.*?)"',html,re.S)[0]
win_speed=re.findall(r'"win_speed":"(.*?)"',html,re.S)[0]
win_meter=re.findall(r'"win_meter":"(.*?)"',html,re.S)[0]
air=re.findall(r'"air":"(.*?)"',html,re.S)[0]
return [
Plain(text="%s今日天气\n"%point),
Plain(text="天气情况:%s\n"%wea),
Plain(text="实时温度:%s℃\n"%tem),
Plain(text="最高温:%s℃\n"%tem_day),
Plain(text="最低温:%s℃\n"%tem_night),
Plain(text="风向:%s\n"%win),
Plain(text="风力等级:%s\n"%win_speed),
Plain(text="风速:%s\n"%win_meter),
Plain(text="空气质量:%s"%air)
]
# 营销号生成器
def yingxiaohao(somebody,something,other_word):
txt = ''' {somebody}{something}是怎么回事呢?{somebody}相信大家都很熟悉,但是{somebody}{something}是怎么回事呢,下面就让小编带大家一起了解吧。
{somebody}{something},其实就是{somebody}{other_word},大家可能会很惊讶{somebody}怎么会{something}呢?但事实就是这样,小编也感到非常惊讶。
这就是关于{somebody}{something}的事情了,大家有什么想法呢,欢迎在评论区告诉小编一起讨论哦!'''
return [Plain(text=txt.format(somebody=somebody, something=something, other_word=other_word))]
# 问你点儿事儿
def askSth(sender,question):
return [
At(target=sender),
Plain(text="啧啧啧,都多大了,还不会百度嘛,不会的话谷歌也行啊\n"),
Plain(text="什么?你说还不会?你可真是个小憨批呢\n"),
Plain(text="没办法呢,就让聪明的我来帮帮你吧!\n"),
Plain(text="https://baidu.sagiri-web.com/?%s"%question)
]
# 图片搜索
def searchImage(groupId,sender,img):
setReady(groupId,sender,False,"searchReady")
searchCount=getData("searchCount")+1
print(searchCount)
updateData(searchCount,"search")
dist="%s%s.png"%(searchDist,searchCount)
img_content=requests.get(img.url).content
image=IMG.open(BytesIO(img_content))
image.save(dist)
#url for headers
url = 'https://saucenao.com/search.php'
#picture url
picUrl = img.url
#json requesting url
url2 = f'https://saucenao.com/search.php?db=999&output_type=2&testmode=1&numres=1&url={picUrl}'
#data for posting.
data = {
'url' : picUrl,
'numres' : 1,
'testmode' : 1,
'db' : 999,
'output_type' : 2,
}
#header to fool the website.
headers = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
'Sec-Fetch-Dest' : 'document',
'Sec-Fetch-Mode' : 'navigate',
'Sec-Fetch-Site' : 'none',
'Sec-Fetch-User' : '?1',
'Referer' : url,
'Origin' : 'https://saucenao.com',
'Host' : 'saucenao.com'
}
page = requests.post(url, headers=headers, data=data)
# html=page.text
json_data = page.json()
#print thumbnail URL.
# print(json_data)
# print(json_data['results'][0]['header']['thumbnail'])
dist="M:\pixiv\Thumbnail\\%s.png"%searchCount
response=requests.get(json_data['results'][0]['header']['thumbnail'])
imgContent=response.content
image=IMG.open(BytesIO(imgContent))
image.save(dist)
similarity=json_data['results'][0]['header']['similarity']
try:
pixiv_url=json_data['results'][0]['data']['ext_urls'][0]
except KeyError:
pixiv_url="None"
if 'pixiv_id' not in json_data['results'][0]['data']:
if 'source' not in json_data['results'][0]['data']:
record("search",dist,sender,groupId,False,"img")
return [Plain(text="8好意思~没有找到相似的图诶~")]
else:
try:
creator=json_data['results'][0]['data']['creator'][0]
except Exception:
creator="Unknown!"
record("search",dist,sender,groupId,True,"img")
return [
At(target=sender),
Plain(text="这个结果相似度很低诶。。。。要不你康康?\n"),
Image.fromFileSystem(dist),
Plain(text="\n相似度:%s%%\n"%(similarity)),
Plain(text="原图地址:%s\n"%pixiv_url),
Plain(text="作者:%s\n"%creator),
Plain(text="如果不是你想找的图的话可能因为这张图是最近才画出来的哦,网站还未收录呢~过段日子再来吧~")
]
else:
pixiv_id=json_data['results'][0]['data']['pixiv_id']
user_name=json_data['results'][0]['data']['member_name']
user_id=json_data['results'][0]['data']['member_id']
record("search",dist,sender,groupId,True,"img")
return [
At(target=sender),
Image.fromFileSystem(dist),
Plain(text="\n相似度:%s%%\n"%(similarity)),
Plain(text="原图地址:%s\n"%pixiv_url),
Plain(text="作品id:%s\n"%pixiv_id),
Plain(text="作者名字:%s\n"%user_name),
Plain(text="作者id:%s\n"%user_id)
]
# 碧蓝航线wiki网址
def blhxWiki(sender,name):
return [
At(target=sender),
Plain(text="以下是%s的wiki网址,可在其中查到%s的各种信息哦:\n"%(name,name)),
Plain(text="https://wiki.biligame.com/blhx/%s"%parse.quote(name))
]
# 获取全部数据
def getAllData(groupId):
setuCalled=getData("setuCalled") #响应setu请求次数
bizhiCalled=getData("bizhiCalled") #响应壁纸请求次数
weatherCalled=getData("weatherCalled") #响应天气请求次数
realCalled=getData("realCalled") #响应real请求次数
responseCalled=getData("responseCalled") #响应请求次数
clockCalled=getData("clockCalled") #响应time次数
text="""Current State:
setu:{setu}
r18:{r18}
real:{real}
bizhi:{bizhi}
repeat:{repeat}
setuPosition:{setuPosition}
bizhiPosition:{bizhiPosition}
setuStored:{setuCount}
setuR18Stored:{setuR18Count}
bizhiStored:{bizhiCount}
realStored:{realCount}
totalStored:{totalCount}
setuCalls:{setuCalls}
realCalls:{realCalls}
bizhiCalls:{bizhiCalls}
weatherCalls:{weatherCalls}
clockCalls:{clockCalls}
totalResponseTimes:{totalEesponseTimes}
Console-Pure Version: 0.5.2"""
if getSetting(groupId,"setu"):
setu="True"
else:
setu="False"
if getSetting(groupId,"r18"):
r18="True"
else:
r18="False"
if getSetting(groupId,"real"):
real="True"
else:
real="False"
if getSetting(groupId,"bizhi"):
bizhi="True"
else:
bizhi="False"
if getSetting(groupId,"repeat"):
repeat="True"
else:
repeat="False"
if getSetting(groupId,"setuLocal"):
setuPosition="Local"
else:
setuPosition="Net"
if getSetting(groupId,"bizhiLocal"):
bizhiPosition="Local"
else:
bizhiPosition="Net"
setuCount=len(os.listdir(os.path.dirname(setuDist)))
setuR18Count=len(os.listdir(os.path.dirname(setu18Dist)))
bizhiCount=len(os.listdir(os.path.dirname(bizhiDist)))
realCount=len(os.listdir(os.path.dirname(realDist)))
totalCount=setuCount+setuR18Count+bizhiCount+realCount
setuCalls=setuCalled
bizhiCalls=bizhiCalled
weatherCalls=weatherCalled
realCalls=realCalled
clockCalls=clockCalled
totalResponseTimes=responseCalled
return text.format(
setu=setu,
r18=r18,
real=real,
bizhi=bizhi,
repeat=repeat,
setuPosition=setuPosition,
bizhiPosition=bizhiPosition,
setuCount=setuCount,
setuR18Count=setuR18Count,
bizhiCount=bizhiCount,
realCount=realCount,
totalCount=totalCount,
setuCalls=setuCalls,
realCalls=realCalls,
bizhiCalls=bizhiCalls,
weatherCalls=weatherCalls,
clockCalls=clockCalls,
totalResponseTimes=totalResponseTimes
)
# 获取表盘选择
def getClockChoice(groupId,sender):
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "SELECT choice from clockChoice WHERE groupId=%d and memberId=%d"%(groupId,sender)
cur.execute(sql)
try:
result=cur.fetchone()[0]
except TypeError:
# sql="INSERT INTO clockChoice (groupId,memberId,choice) VALUES (%d,%d,%d)"%(groupId,sender,False)
# cur.execute(sql)
# conn.commit()
return "none"
print(result)
cur.close()
conn.close()
return result
# 展示表盘
def showClock(sender):
clockMessage=[
At(target=sender),
Plain(text="看中后直接发送选择表盘+序号即可哦~\n"),
Plain(text="如:选择表盘1\n"),
Plain(text="表盘预览:")
]
clockList = os.listdir(clockPreviewDist)
clockList.sort(key=lambda x:int(x[:-4]))
index=1
for i in clockList:
clockMessage.append(Plain(text="\n%s."%index))
clockMessage.append(Image.fromFileSystem(clockPreviewDist+i))
index+=1
return clockMessage
# 记录表盘选择
def recordClock(groupId,sender,choice):
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "SELECT choice from clockChoice WHERE groupId=%d and memberId=%d"%(groupId,sender)
cur.execute(sql)
try:
result=cur.fetchone()[0]
sql="UPDATE clockChoice SET choice=%d WHERE groupId=%d and memberId=%d"%(choice,groupId,sender)
cur.execute(sql)
except TypeError:
sql="INSERT INTO clockChoice (groupId,memberId,choice) VALUES (%d,%d,%d)"%(groupId,sender,choice)
cur.execute(sql)
conn.commit()
cur.close()
conn.close()
# 判断setting选项合法性
def configChangeJudge(config,change):
if (config=="limit" or config=="tributeQuantity") and change.isnumeric():
return True
if change not in settingCode:
return False
change=settingCode[change]
if config=="repeat" and (change==True or change==False):
return True
elif (config=="setuLocal" or config=="bizhiLocal") and (change==True or change==False):
return True
elif config=="countLimit" and (change==True or change==False):
return True
elif config=="tribute" and (change==True or change==False):
return True
elif config=="listen" and (change==True or change==False):
return True
elif config=="music" and (change=="wyy" or change=="qq" or change=="off"):
return True
elif (config=="setu" or config=="real" or config=="bizhi" or config=="r18" or config=="search" or config=="imgPredict" or config=="imgLightning") and (change==True or change==False):
return True
elif config=="speakMode" and (change=="normal" or change=="zuanHigh" or change=="zuanLow" or change=="rainbow" or change=="chat"):
return True
elif config=="switch" and (change=="online" or change=="offline"):
return True
return False
# 判断info选项合法性
def infoCheckJudge(check):
info=["sys","setu","real","bizhi","switch","all","group","countLimit","speakMode","r18"]
if check in info:
return True
return False
# 获取文件夹大小
def getFileSize(dir):
size = 0
for root, dirs, files in os.walk(dir):
size += sum([getsize(join(root, name)) for name in files])
return size
# 返回主机状态
def getSysInfo():
w=wmi.WMI()
processor=w.Win32_Processor()
m=w.Win32_ComputerSystem()
operator = w.Win32_OperatingSystem()
text=" systemInfo \n"
text+="--------------------\n"
text+="CPU:\n"
for cpu in processor:
text+="CPU Model:%s\n"%cpu.Name
text+="Frequency:%sMHz\n"%cpu.CurrentClockSpeed
text+="Number of cores:%s\n"%cpu.NumberOfCores
text+="Usage rate:%s%%\n"%cpu.LoadPercentage
text+="--------------------\n"
text+="GPU:\n"
for gpu in w.Win32_VideoController():
text+="GPU Model:%s\n"%gpu.caption
nvmlInit()
handle = nvmlDeviceGetHandleByIndex(0)
meminfo = nvmlDeviceGetMemoryInfo(handle)
text+="Total memory:%2.2fG\n"%(float(meminfo.total)/1024/1024/1024)
text+="Used memory:%2.2fG\n"%(float(meminfo.used)/1024/1024/1024)
text+="Free memory:%2.2fG\n"%(float(meminfo.free)/1024/1024/1024)
text+="--------------------\n"
text+="Memory:\n"
for memory in m:
tm=float(memory.TotalPhysicalMemory)/1024/1024/1024
text+="Total memory:%.2fG\n"%tm
for os in operator:
text+="Used memory:%.2fG\n"%(tm-float(os.FreePhysicalMemory)/1024/1024)
text+="Free memory:%.2fG\n"%(float(os.FreePhysicalMemory)/1024/1024)
text+="--------------------\n"
text+="Disk:\n"
diskSize="300.00G"
setuSize=getFileSize("M:\pixiv\pxer_new\\")
setu18Size=getFileSize("M:\pixiv\pxer18_new\\")
bizhiSize=getFileSize("M:\pixiv\\bizhi\\")
realSize=getFileSize("M:\pixiv\\reality\\")
text+="Drive Size:%s\n"%diskSize
text+="Setu folder Size:%.2fG\n"%(setuSize/1024/1024/1024)
text+="Setu18 folder Size:%.2fG\n"%(setu18Size/1024/1024/1024)
text+="Bizhi folder Size:%.2fG\n"%(bizhiSize/1024/1024/1024)
text+="Real folder Size:%.2fG\n"%(realSize/1024/1024/1024)
text+="Total folder Size:%.2fG\n"%((setuSize+setu18Size+bizhiSize+realSize)/1024/1024/1024)
text+="--------------------\n"
time_now=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
text+="time:%s\n"%time_now
text+="---------------End\n"
return text
# 返回群组全部设置
def getGroupAllSetting(groupId):
groupName=getSetting(groupId,"groupName")
repeat=getSetting(groupId,"repeat")
setuLocal=getSetting(groupId,"setuLocal")
bizhiLocal=getSetting(groupId,"bizhiLocal")
countLimit=getSetting(groupId,"countLimit")
setu=getSetting(groupId,"setu")
bizhi=getSetting(groupId,"bizhi")
real=getSetting(groupId,"real")
r18=getSetting(groupId,"r18")
speakMode=getSetting(groupId,"speakMode")
groupSetting=Plain(text="""
groupId:%s
groupName:%s
repeat:%s
setuLocal:%s
bizhiLocal:%s
countLimit:%s
setu:%s
real:%s
bizhi:%s
r18:%s
speakMode:%s"""%(groupId,groupName,repeat,setuLocal,bizhiLocal,countLimit,setu,bizhi,real,r18,speakMode))
return groupSetting
# 返回设置等信息
def showSetting(groupId,sender,check):
settingList=["groupId","groupName","repeat","setuLocal","bizhiLocal","countLimit","setu","bizhi","real","r18","speakMode","switch"]
if check=="sys":
return [
At(target=sender),
Plain(text=getSysInfo())
]
elif check=="all":
title=Plain(text="\n-----------setting-----------\n")
groupSetting=getGroupAllSetting(groupId)
split=Plain(text="\n-----------System-----------\n")
sysInfo=getSysInfo()
return [
At(target=sender),
title,
groupSetting,
split,
Plain(text=sysInfo)
]
elif check=="group":
Json="""
{
"app":"com.tencent.miniapp",
"desc":"",
"view":"notification",
"ver":"0.0.0.1",
"prompt":"[群设置]",
"appID":"",
"sourceName":"",
"actionData":"",
"actionData_A":"",
"sourceUrl":"",
"meta":{
"notification":{
"appInfo":{
"appName":"%d群设置",
"appType":4,
"appid":1109659848,
"iconUrl":"http://gchat.qpic.cn/gchatpic_new/719328335/-2010394141-6383A777BEB79B70B31CE250142D740F/0"
},
"data":[
{
"title":"复读",
"value":"%d"
},
{
"title":"setu",
"value":"%d"
},
{
"title":"real",
"value":"%d"
},
{
"title":"bizhi",
"value":"%d"
},
{
"title":"r18",
"value":"%d"
},
{
"title":"搜图",
"value":"%d"
},
{
"title":"图片预测",
"value":"%d"
},
{
"title":"图片鉴黄",
"value":"%d"
},
{
"title":"图片闪电",
"value":"%s"
},
{
"title":"数量限制",
"value":"%d"
},
{
"title":"限制数量",
"value":"%d"
},
{
"title":"监听",
"value":"%d"
},
{
"title":"上贡",
"value":"%d"
},
{
"title":"说话模式",
"value":"%s"
}],
"title":"纱雾赛高!",
"emphasis_keyword":""
}
},
"text":"",
"sourceAd":""
}"""%(groupId,getSetting(groupId,"repeat"),getSetting(groupId,"setu"),getSetting(groupId,"real"),getSetting(groupId,"bizhi"),getSetting(groupId,"r18"),getSetting(groupId,"search"),getSetting(groupId,"imgPredict"),getSetting(groupId,"yellowPredict"),getSetting(groupId,"imgLightning"),getSetting(groupId,"countLimit"),getSetting(groupId,"limit"),getSetting(groupId,"listen"),getSetting(groupId,"tribute"),getSetting(groupId,"speakMode"))
return [
LightApp(Json)
]
else:
setting=getSetting(groupId,check)
return [
At(target=sender),
Plain(text="group:%d %s:%d"%(groupId,check,setting))
]
# 判断成员能否要图(countLimit模式下)
def getMemberPicStatus(groupId,sender):
limit=getSetting(groupId,"limit")
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "select count from memberPicCount where groupId=%d and memberId=%d"%(groupId,sender)
cur.execute(sql)
data = cur.fetchone()
if not data==None:
data=data[0]
sql = "select `time` from memberPicCount where groupId=%d and memberId=%d"%(groupId,sender)
cur.execute(sql)
time = cur.fetchone()[0]
if int(data)>=limit and (datetime.datetime.now()-time).seconds<60:
return False
elif int(data)<limit and (datetime.datetime.now()-time).seconds<60:
sql = "update memberPicCount set count=%d where groupId=%d and memberId=%d"%(int(data)+1,groupId,sender)
elif (datetime.datetime.now()-time).seconds>60:
sql = "update memberPicCount set count=1,`time`='%s' where groupId=%d and memberId=%d"%(datetime.datetime.now(),groupId,sender)
cur.execute(sql)
else:
sql = "insert memberPicCount set groupId=%d,memberId=%d,time='%s',count=1"%(groupId,sender,datetime.datetime.now())
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
return True
# qq号转名字
def qq2name(memberList,qq):
if qq==0:
return "public"
elif qq==80000000:
return "Anonymous"
for i in memberList:
if i.id==qq:
return i.memberName
return "qq2Name::Error"
# 秒数转时间str
def sec2Str(seconds):
if seconds<60:
return str(int(seconds))+"秒"
elif seconds<3600:
return str(int(seconds/60))+"分"+str(int(seconds%60))+"秒"
elif seconds<86400:
return str(int(seconds/3600))+"时"+str(int(seconds%3600/60))+"分"+str(int(seconds%60))+"秒"
else:
return str(int(seconds/86400))+"天"+str(int(seconds%86400/3600))+"时"+str(int(seconds%3600/60))+"分"+str(int(seconds%60))+"秒"
# 将得到的MD5值所有字符转换成大写
def curlmd5(src):
m = hashlib.md5(src.encode('UTF-8'))
return m.hexdigest().upper()
def getParams(groupId,sender,plus_item):
# 请求时间戳(秒级),用于防止请求重放(保证签名5分钟有效)
t = time.time()
time_stamp = str(int(t))
# 请求随机字符串,用于保证签名不可预测
nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 10))
# 应用标志,这里修改成自己的id和key
app_id=getConfig("app_id")
app_key =getConfig("app_key")
params = { 'app_id' : app_id,
'question' : plus_item,
'time_stamp':time_stamp,
'nonce_str':nonce_str,
'session':getChatSession(groupId,sender)
}
sign_before = ''
# 要对key排序再拼接
for key in sorted(params):
# 键值拼接过程value部分需要URL编码,URL编码算法用大写字母,例如%E8。quote默认大写。
sign_before += '{}={}&'.format(key, quote(params[key], safe=''))
# 将应用密钥以app_key为键名,拼接到字符串sign_before末尾
sign_before += 'app_key={}'.format(app_key)
# 对字符串sign_before进行MD5运算,得到接口请求签名
sign = curlmd5(sign_before)
params['sign'] = sign
return params
# 智能闲聊返回文字
def getChatText(groupId,sender,plus_item):
# 聊天的API地址
url = "https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat"
# 获取请求参数
plus_item = plus_item.encode('utf-8')
payload = getParams(groupId,sender,plus_item)
r = requests.get(url,params=payload)
# r = requests.post(url,data=payload)
# print(r.text)
# print(r.json()["data"]["answer"])
return r.json()["data"]["answer"]
# 获取智能闲聊session
def getChatSession(groupId,sender):
conn = pymysql.connect(host=host, user=user, passwd=dbPass, db=db, port=3306, charset="utf8")
cur = conn.cursor()
sql = "select `session` from chatSession where groupId=%d and memberId=%d"%(groupId,sender)
cur.execute(sql)
data = cur.fetchone()
print(data)
if not data==None:
session=data[0]
cur.close()
conn.close()
print("智能闲聊 sender:%s,session:%s"%(sender,session))
return str(session)
else:
sql = "select MAX(`session`) from chatSession"
cur.execute(sql)
data = cur.fetchone()[0]
print(data)
if data==None:
session=1
else:
session=int(data)+1
print("session:",session)
sql="INSERT INTO chatSession (groupId,memberId,`session`) VALUES (%d,%d,%d)"%(groupId,sender,session)
cur.execute(sql)
cur.close()
conn.commit()
conn.close()
print("智能闲聊 sender:%s,session:%s"%(sender,session))
return str(session)
# 翻译功能
def translate(groupId,sender,text,source,target):
url="https://api.ai.qq.com/fcgi-bin/nlp/nlp_texttranslate"
# 请求时间戳(秒级),用于防止请求重放(保证签名5分钟有效)
t = time.time()
time_stamp = str(int(t))
# 请求随机字符串,用于保证签名不可预测
nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 10))
# 应用标志,这里修改成自己的id和key
app_id=getConfig("app_id")
app_key =getConfig("app_key")
params = { 'app_id' : app_id,
'text' : text,
'time_stamp':time_stamp,
'nonce_str':nonce_str,
'source':source,
'target':target
}
sign_before = ''
for key in sorted(params):
sign_before += '{}={}&'.format(key, quote(params[key], safe=''))
sign_before += 'app_key={}'.format(app_key)
sign = curlmd5(sign_before)
params['sign'] = sign
r = requests.get(url,params=params)
# print(r.text)
record("translate %s->%s"%(source,target),"none",sender,groupId,True,"function")
return [
At(target=sender),
Plain("translate:\n"),
Plain(text="%s"%r.json()["data"]["target_text"])
]
# 检测语言
def textDetect(text):
url="https://api.ai.qq.com/fcgi-bin/nlp/nlp_textdetect"
# 请求时间戳(秒级),用于防止请求重放(保证签名5分钟有效)
t = time.time()
time_stamp = str(int(t))
# 请求随机字符串,用于保证签名不可预测
nonce_str = ''.join(random.sample(string.ascii_letters + string.digits, 10))
# 应用标志,这里修改成自己的id和key
app_id=getConfig("app_id")
app_key =getConfig("app_key")
params = { 'app_id' : app_id,
'candidate_langs':'',
'force':'1',
'nonce_str':nonce_str,
'text' : text,
'time_stamp':time_stamp
}
sign=getSign(params)
params['sign'] = sign
r = requests.get(url,params=params)
# print(r.text)
return r.json()["data"]["lang"]
# 接口签名算法
def getSign(params):
APP_KEY = getConfig("app_key")
app_key = getConfig("app_key")
paramsKeys=sorted(params.keys())
# print(paramsKeys)
# print(params)
sign=""
for i in paramsKeys:
if not params[i]=='':
sign+="%s=%s&"%(i,parse.quote(params[i], safe=''))
sign+="app_key=%s"%app_key
# print("sign:",sign)
sign=curlmd5(sign)
print("signMD5:",sign)
return sign
# 获取图片大小
def get_size(file):
# 获取文件大小:KB
size = os.path.getsize(file)
return size / 1024
# 拼接输出文件地址
def get_outfile(infile, outfile):
if outfile:
return outfile
dir, suffix = os.path.splitext(infile)
outfile = '{}-out{}'.format(dir, suffix)
return outfile
# 图片压缩
def compress_image(infile, outfile='', mb=900, step=10, quality=80):
"""不改变图片尺寸压缩到指定大小
:param infile: 压缩源文件
:param outfile: 压缩文件保存地址