-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathspider.py
2870 lines (2539 loc) · 146 KB
/
spider.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import copy
import datetime
import json
import os
import re
import struct
import threading
import time
import zlib
from urllib.parse import quote, unquote
import requests
import websocket
from bs4 import BeautifulSoup
# 仅从cfg和cfg_mod中获取参数,不会启动子监视器
class SubMonitor(threading.Thread):
def __init__(self, name, tgt, tgt_name, cfg, **cfg_mod):
super().__init__()
self.name = name
self.tgt = tgt
self.tgt_name = tgt_name
self.interval = 60
self.timezone = 8
self.vip_dic = {}
self.word_dic = {}
self.cookies = {}
self.proxy = {}
self.push_list = []
# 不要直接修改通过cfg引用传递定义的列表和变量,请deepcopy后再修改
for var in cfg:
setattr(self, var, cfg[var])
for var in cfg_mod:
setattr(self, var, cfg_mod[var])
self.stop_now = False
def checksubmonitor(self):
pass
def run(self):
while not self.stop_now:
time.sleep(self.interval)
def stop(self):
self.stop_now = True
# 保留cfg(cfg_mod并不修改cfg本身),可以启动子监视器
class Monitor(SubMonitor):
# 初始化
def __init__(self, name, tgt, tgt_name, cfg, **cfg_mod):
super().__init__(name, tgt, tgt_name, cfg, **cfg_mod)
self.cfg = copy.deepcopy(cfg)
self.submonitor_config_name = "cfg"
self.submonitor_threads = {}
self.submonitor_cnt = 0
self.submonitor_live_cnt = 0
self.submonitor_checknow = False
self.stop_now = False
# 重设submonitorconfig名字并初始化
def submonitorconfig_setname(self, submonitor_config_name):
self.submonitor_config_name = submonitor_config_name
submonitor_config = getattr(self, submonitor_config_name, {"submonitor_dic": {}})
setattr(self, self.submonitor_config_name, submonitor_config)
# 向submonitorconfig添加预设的config
def submonitorconfig_addconfig(self, config_name, config):
submonitor_config = getattr(self, self.submonitor_config_name)
submonitor_config[config_name] = config
setattr(self, self.submonitor_config_name, submonitor_config)
# 向submonitorconfig的submonitor_dic中添加子线程信息以启动子线程
def submonitorconfig_addmonitor(self, monitor_name, monitor_class, monitor_target, monitor_target_name,
monitor_config_name, **config_mod):
submonitor_config = getattr(self, self.submonitor_config_name)
if monitor_name not in submonitor_config["submonitor_dic"]:
submonitor_config["submonitor_dic"][monitor_name] = {}
submonitor_config["submonitor_dic"][monitor_name]["class"] = monitor_class
submonitor_config["submonitor_dic"][monitor_name]["target"] = monitor_target
submonitor_config["submonitor_dic"][monitor_name]["target_name"] = monitor_target_name
submonitor_config["submonitor_dic"][monitor_name]["config_name"] = monitor_config_name
for mod in config_mod:
submonitor_config["submonitor_dic"][monitor_name][mod] = config_mod[mod]
setattr(self, self.submonitor_config_name, submonitor_config)
# 从submonitorconfig的submonitor_dic中删除对应的子线程
def submonitorconfig_delmonitor(self, monitor_name):
submonitor_config = getattr(self, self.submonitor_config_name)
if monitor_name in submonitor_config["submonitor_dic"]:
submonitor_config["submonitor_dic"].pop(monitor_name)
setattr(self, self.submonitor_config_name, submonitor_config)
# 按照submonitorconfig检查子线程池
def checksubmonitor(self):
if not self.submonitor_checknow:
self.submonitor_checknow = True
submonitorconfig = getattr(self, self.submonitor_config_name)
if "submonitor_dic" in submonitorconfig:
self.submonitor_cnt = len(submonitorconfig["submonitor_dic"])
for monitor_name in submonitorconfig["submonitor_dic"]:
if monitor_name not in self.submonitor_threads:
# 按照submonitorconfig启动子线程并添加到子线程池
monitor_thread = createmonitor(monitor_name, submonitorconfig)
self.submonitor_threads[monitor_name] = monitor_thread
self.submonitor_live_cnt = 0
for monitor_name in list(self.submonitor_threads):
if monitor_name not in submonitorconfig["submonitor_dic"]:
# 按照submonitorconfig关闭子线程并清理子线程池
if self.submonitor_threads[monitor_name].is_alive():
self.submonitor_threads[monitor_name].stop()
self.submonitor_live_cnt += 1
else:
self.submonitor_threads.pop(monitor_name)
else:
# 从子线程池检查并重启
if self.submonitor_threads[monitor_name].is_alive():
self.submonitor_threads[monitor_name].checksubmonitor()
self.submonitor_live_cnt += 1
else:
self.submonitor_threads[monitor_name].stop()
monitor_thread = createmonitor(monitor_name, submonitorconfig)
self.submonitor_threads[monitor_name] = monitor_thread
if self.submonitor_live_cnt > 0 or self.submonitor_cnt > 0:
printlog(
'[Check] "%s" 子线程运行情况:%s/%s' % (self.name, self.submonitor_live_cnt, self.submonitor_cnt))
self.submonitor_checknow = False
# 启动
def run(self):
self.checksubmonitor()
while not self.stop_now:
time.sleep(self.interval)
# 停止线程
def stop(self):
self.stop_now = True
for monitor_name in self.submonitor_threads:
self.submonitor_threads[monitor_name].stop()
# vip=tgt, word=title+description, standby_chat="True"/"False", standby_chat_onstart="True"/"False", no_chat="True"/"False", status_push="等待|开始|结束|上传|删除", regen="False"/"间隔秒数", regen_amount="1"/"恢复数量"
class YoutubeLive(Monitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s.txt' % (self.__class__.__name__, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
# 重新设置submonitorconfig用于启动子线程,并添加频道id信息到子进程使用的cfg中
self.submonitorconfig_setname("youtubechat_submonitor_cfg")
self.submonitorconfig_addconfig("youtubechat_config", self.cfg)
self.is_firstrun = True
# video_id为字符
self.videodic = {}
# 是否检测待机直播间的弹幕
self.standby_chat = getattr(self, "standby_chat", "False")
# 是否检测在第一次检测时已开启的待机直播间的弹幕
self.standby_chat_onstart = getattr(self, "standby_chat_onstart", "False")
# 不记录弹幕
self.no_chat = getattr(self, "no_chat", "False")
# 需要推送的情况,其中等待|开始|结束是直播和首播才有的情况,上传是视频才有的情况,删除则都存在
self.status_push = getattr(self, "status_push", "等待|开始|结束|上传|删除")
# 推送惩罚恢复间隔
self.regen = getattr(self, "regen", "False")
# 每次推送惩罚恢复量
self.regen_amount = getattr(self, "regen_amount", 1)
def run(self):
while not self.stop_now:
# 更新视频列表
try:
videodic_new = getyoutubevideodic(self.tgt, self.cookies, self.proxy)
for video_id in videodic_new:
if video_id not in self.videodic:
self.videodic[video_id] = videodic_new[video_id]
if not self.is_firstrun or videodic_new[video_id][
"video_status"] == "等待" and self.standby_chat_onstart == "True" or videodic_new[video_id][
"video_status"] == "开始":
self.push(video_id)
if self.is_firstrun:
writelog(self.logpath,
'[Info] "%s" getyoutubevideodic %s: %s' % (self.name, self.tgt, videodic_new))
self.is_firstrun = False
writelog(self.logpath, '[Success] "%s" getyoutubevideodic %s' % (self.name, self.tgt))
except Exception as e:
printlog('[Error] "%s" getyoutubevideodic %s: %s' % (self.name, self.tgt, e))
writelog(self.logpath, '[Error] "%s" getyoutubevideodic %s: %s' % (self.name, self.tgt, e))
# 更新视频状态
for video_id in self.videodic:
if self.videodic[video_id]["video_status"] == "等待" or self.videodic[video_id]["video_status"] == "开始":
try:
video_status = getyoutubevideostatus(video_id, self.cookies, self.proxy)
if self.videodic[video_id]["video_status"] != video_status:
self.videodic[video_id]["video_status"] = video_status
self.push(video_id)
writelog(self.logpath, '[Success] "%s" getyoutubevideostatus %s' % (self.name, video_id))
except Exception as e:
printlog("[Error] %s getvideostatus %s: %s" % (self.name, video_id, e))
writelog(self.logpath, '[Error] "%s" getyoutubevideostatus %s: %s' % (self.name, video_id, e))
time.sleep(self.interval)
def push(self, video_id):
if self.status_push.count(self.videodic[video_id]["video_status"]):
# 获取视频简介
try:
video_description = getyoutubevideodescription(video_id, self.cookies, self.proxy)
writelog(self.logpath,
'[Success] "%s" getyoutubevideodescription %s' % (self.name, video_id))
except Exception as e:
printlog('[Error] "%s" getyoutubevideodescription %s: %s' % (self.name, video_id, e))
writelog(self.logpath, '[Error] "%s" getyoutubevideodescription %s: %s' % (self.name, video_id, e))
video_description = ""
# 计算推送力度
pushcolor_vipdic = getpushcolordic(self.tgt, self.vip_dic)
pushcolor_worddic = getpushcolordic("%s\n%s" % (self.videodic[video_id]["video_title"], video_description),
self.word_dic)
pushcolor_dic = addpushcolordic(pushcolor_vipdic, pushcolor_worddic)
# 进行推送
if pushcolor_dic:
pushtext = "【%s %s %s%s】\n标题:%s\n时间:%s\n网址:https://www.youtube.com/watch?v=%s" % (
self.__class__.__name__, self.tgt_name, self.videodic[video_id]["video_type"],
self.videodic[video_id]["video_status"], self.videodic[video_id]["video_title"],
formattime(self.videodic[video_id]["video_timestamp"], self.timezone), video_id)
pushall(pushtext, pushcolor_dic, self.push_list)
printlog('[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
writelog(self.logpath,
'[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
if self.no_chat != "True":
# 开始记录弹幕
if self.videodic[video_id]["video_status"] == "等待" and self.standby_chat == "True" or \
self.videodic[video_id]["video_status"] == "开始":
monitor_name = "%s - YoutubeChat %s" % (self.name, video_id)
if monitor_name not in getattr(self, self.submonitor_config_name)["submonitor_dic"]:
self.submonitorconfig_addmonitor(monitor_name, "YoutubeChat", video_id, self.tgt_name,
"youtubechat_config", tgt_channel=self.tgt, interval=2,
regen=self.regen, regen_amount=self.regen_amount)
self.checksubmonitor()
printlog('[Info] "%s" startsubmonitor %s' % (self.name, monitor_name))
writelog(self.logpath, '[Info] "%s" startsubmonitor %s' % (self.name, monitor_name))
# 停止记录弹幕
else:
monitor_name = "%s - YoutubeChat %s" % (self.name, video_id)
if monitor_name in getattr(self, self.submonitor_config_name)["submonitor_dic"]:
self.submonitorconfig_delmonitor(monitor_name)
self.checksubmonitor()
printlog('[Info] "%s" stopsubmonitor %s' % (self.name, monitor_name))
writelog(self.logpath, '[Info] "%s" stopsubmonitor %s' % (self.name, monitor_name))
# vip=userchannel, word=text, punish=tgt+push(不包括含有'vip'的类型)
class YoutubeChat(SubMonitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s/%s.txt' % (
self.__class__.__name__, self.tgt_name, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
if not os.path.exists('./log/%s/%s' % (self.__class__.__name__, self.tgt_name)):
os.mkdir('./log/%s/%s' % (self.__class__.__name__, self.tgt_name))
self.chatpath = './log/%s/%s/%s_chat.txt' % (
self.__class__.__name__, self.tgt_name, self.name)
# continuation为字符
self.continuation = False
self.key = False
self.pushpunish = {}
self.regen_time = 0
self.tgt_channel = getattr(self, "tgt_channel", "")
self.regen = getattr(self, "regen", "False")
self.regen_amount = getattr(self, "regen_amount", 1)
def run(self):
while not self.stop_now:
# 获取continuation
if not self.continuation:
try:
self.continuation, self.key = getyoutubechatcontinuation(self.tgt, self.proxy)
writelog(self.logpath,
'[Info] "%s" getyoutubechatcontinuation %s: %s(%s)' % (self.name, self.tgt, self.continuation, self.key))
writelog(self.logpath, '[Success] "%s" getyoutubechatcontinuation %s' % (self.name, self.tgt))
except Exception as e:
printlog('[Error] "%s" getyoutubechatcontinuation %s: %s' % (self.name, self.tgt, e))
writelog(self.logpath, '[Error] "%s" getyoutubechatcontinuation %s: %s' % (self.name, self.tgt, e))
time.sleep(5)
continue
# 获取直播评论列表
if self.continuation:
try:
chatlist, self.continuation = getyoutubechatlist(self.tgt, self.continuation, self.key, self.proxy)
for chat in chatlist:
self.push(chat)
# 目标每次请求获取5条评论,间隔时间应在0.1~2秒之间
if len(chatlist) > 0:
self.interval = self.interval * 5 / len(chatlist)
else:
self.interval = 2
if self.interval > 2:
self.interval = 2
if self.interval < 0.1:
self.interval = 0.1
except Exception as e:
printlog('[Error] "%s" getyoutubechatlist %s(%s): %s' % (self.name, self.continuation, self.key, e))
writelog(self.logpath, '[Error] "%s" getyoutubechatlist %s(%s): %s' % (self.name, self.continuation, self.key, e))
time.sleep(self.interval)
def push(self, chat):
writelog(self.chatpath, "%s\t%s\t%s\t%s\t%s" % (
chat["chat_timestamp"], chat["chat_username"], chat["chat_userchannel"], chat["chat_type"],
chat["chat_text"]))
pushcolor_vipdic = getpushcolordic(chat["chat_userchannel"], self.vip_dic)
pushcolor_worddic = getpushcolordic(chat["chat_text"], self.word_dic)
pushcolor_dic = addpushcolordic(pushcolor_vipdic, pushcolor_worddic)
if pushcolor_dic:
pushcolor_dic = self.punish(pushcolor_dic)
pushtext = "【%s %s 直播评论】\n用户:%s\n内容:%s\n类型:%s\n时间:%s\n网址:https://www.youtube.com/watch?v=%s" % (
self.__class__.__name__, self.tgt_name, chat["chat_username"], chat["chat_text"], chat["chat_type"],
formattime(chat["chat_timestamp"], self.timezone), self.tgt)
pushall(pushtext, pushcolor_dic, self.push_list)
printlog('[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
writelog(self.logpath, '[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
def punish(self, pushcolor_dic):
# 推送惩罚恢复
if self.regen != "False":
time_now = getutctimestamp()
regen_amt = int(int((time_now - self.regen_time) / float(self.regen)) * float(self.regen_amount))
if regen_amt:
self.regen_time = time_now
for color in list(self.pushpunish):
if self.pushpunish[color] > regen_amt:
self.pushpunish[color] -= regen_amt
else:
self.pushpunish.pop(color)
# 去除来源频道的相关权重
if self.tgt_channel in self.vip_dic:
for color in self.vip_dic[self.tgt_channel]:
if color in pushcolor_dic and not color.count("vip"):
pushcolor_dic[color] -= self.vip_dic[self.tgt_channel][color]
# 只对pushcolor_dic存在的键进行修改,不同于addpushcolordic
for color in self.pushpunish:
if color in pushcolor_dic and not color.count("vip"):
pushcolor_dic[color] -= self.pushpunish[color]
# 更新pushpunish
for color in pushcolor_dic:
if pushcolor_dic[color] > 0 and not color.count("vip"):
if color in self.pushpunish:
self.pushpunish[color] += 1
else:
self.pushpunish[color] = 1
return pushcolor_dic
# vip=tgt, word=text
class YoutubeCom(SubMonitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s.txt' % (self.__class__.__name__, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
self.is_firstrun = True
# post_id为字符
self.postlist = []
def run(self):
while not self.stop_now:
# 获取帖子列表
try:
postdic_new = getyoutubepostdic(self.tgt, self.cookies, self.proxy)
for post_id in postdic_new:
if post_id not in self.postlist:
self.postlist.append(post_id)
if not self.is_firstrun:
self.push(post_id, postdic_new)
if self.is_firstrun:
writelog(self.logpath,
'[Info] "%s" getyoutubepostdic %s: %s' % (self.name, self.tgt, postdic_new))
self.is_firstrun = False
writelog(self.logpath, '[Success] "%s" getyoutubepostdic %s' % (self.name, self.tgt))
except Exception as e:
printlog('[Error] "%s" getyoutubepostdic %s: %s' % (self.name, self.tgt, e))
writelog(self.logpath, '[Error] "%s" getyoutubepostdic %s: %s' % (self.name, self.tgt, e))
time.sleep(self.interval)
def push(self, post_id, postdic):
pushcolor_vipdic = getpushcolordic(self.tgt, self.vip_dic)
pushcolor_worddic = getpushcolordic(postdic[post_id]["post_text"], self.word_dic)
pushcolor_dic = addpushcolordic(pushcolor_vipdic, pushcolor_worddic)
# 进行推送
if pushcolor_dic:
pushtext = "【%s %s 社区帖子】\n内容:%s\n链接:%s\n时间:%s\n网址:https://www.youtube.com/post/%s" % (
self.__class__.__name__, self.tgt_name, postdic[post_id]["post_text"][0:3000],
postdic[post_id]["post_link"], postdic[post_id]["post_time"], post_id)
pushall(pushtext, pushcolor_dic, self.push_list)
printlog('[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
writelog(self.logpath, '[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
# word=text
class YoutubeNote(SubMonitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s.txt' % (self.__class__.__name__, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
self.is_firstrun = True
self.token = False
# note_id为整数
self.note_id_old = 0
def run(self):
while not self.stop_now:
# 获取token
if not self.token:
try:
self.token = getyoutubetoken(self.cookies, self.proxy)
writelog(self.logpath, '[Info] "%s" getyoutubetoken %s: %s' % (self.name, self.tgt, self.token))
writelog(self.logpath, '[Success] "%s" getyoutubetoken %s' % (self.name, self.tgt))
except Exception as e:
printlog('[Error] "%s" getyoutubetoken %s: %s' % (self.name, self.tgt, e))
writelog(self.logpath, '[Error] "%s" getyoutubetoken %s: %s' % (self.name, self.tgt, e))
time.sleep(5)
continue
# 获取订阅通知列表
if self.token:
try:
notedic_new = getyoutubenotedic(self.token, self.cookies, self.proxy)
if self.is_firstrun:
if notedic_new:
self.note_id_old = sorted(notedic_new, reverse=True)[0]
writelog(self.logpath,
'[Info] "%s" getyoutubenotedic %s: %s' % (self.name, self.tgt, notedic_new))
self.is_firstrun = False
else:
for note_id in notedic_new:
if note_id > self.note_id_old:
self.push(note_id, notedic_new)
if notedic_new:
self.note_id_old = sorted(notedic_new, reverse=True)[0]
writelog(self.logpath, '[Success] "%s" getyoutubenotedic %s' % (self.name, self.tgt))
except Exception as e:
printlog('[Error] "%s" getyoutubenotedic %s: %s' % (self.name, self.tgt, e))
writelog(self.logpath, '[Error] "%s" getyoutubenotedic %s: %s' % (self.name, self.tgt, e))
time.sleep(self.interval)
def push(self, note_id, notedic):
pushcolor_worddic = getpushcolordic(notedic[note_id]["note_text"], self.word_dic)
pushcolor_dic = pushcolor_worddic
if pushcolor_dic:
pushtext = "【%s %s 订阅通知】\n内容:%s\n时间:%s\n网址:https://www.youtube.com/watch?v=%s" % (
self.__class__.__name__, self.tgt_name, notedic[note_id]["note_text"],
notedic[note_id]["note_time"], notedic[note_id]["note_videoid"])
pushall(pushtext, pushcolor_dic, self.push_list)
printlog('[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
writelog(self.logpath, '[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
# vip=tgt, no_increase="True"/"False", no_repeat="False"/"间隔秒数"
class TwitterUser(SubMonitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s.txt' % (self.__class__.__name__, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
self.is_firstrun = True
self.userdata_dic = {}
# 是否不推送推文和媒体数量的增加
self.no_increase = getattr(self, "no_increase", "False")
# 是否不推送短时间内重复的推文和媒体数量
self.no_repeat = getattr(self, "no_repeat", "False")
self.statuses_dic = {}
self.media_dic = {}
self.favourites_dic = {}
def run(self):
while not self.stop_now:
# 获取用户信息
try:
user_datadic_new = gettwitteruser(self.tgt, self.cookies, self.proxy)
if self.is_firstrun:
self.userdata_dic = user_datadic_new
writelog(self.logpath,
'[Info] "%s" gettwitteruser %s: %s' % (self.name, self.tgt, user_datadic_new))
self.is_firstrun = False
else:
pushtext_body = ""
for key in user_datadic_new:
if key not in self.userdata_dic:
pushtext_body += "新键:%s\n值:%s\n" % (key, str(user_datadic_new[key]))
self.userdata_dic[key] = user_datadic_new[key]
elif self.userdata_dic[key] != user_datadic_new[key]:
if self.no_repeat != "False" and key == "statuses_count":
time_now = getutctimestamp()
if user_datadic_new[key] in self.statuses_dic:
if time_now < self.statuses_dic[user_datadic_new[key]] + float(self.no_repeat):
self.userdata_dic[key] = user_datadic_new[key]
self.statuses_dic[user_datadic_new[key]] = time_now
continue
self.statuses_dic[user_datadic_new[key]] = time_now
if self.no_repeat != "False" and key == "media_count":
time_now = getutctimestamp()
if user_datadic_new[key] in self.media_dic:
if time_now < self.media_dic[user_datadic_new[key]] + float(self.no_repeat):
self.userdata_dic[key] = user_datadic_new[key]
self.media_dic[user_datadic_new[key]] = time_now
continue
self.media_dic[user_datadic_new[key]] = time_now
if self.no_repeat != "False" and key == "favourites_count":
time_now = getutctimestamp()
if user_datadic_new[key] in self.favourites_dic:
if time_now < self.favourites_dic[user_datadic_new[key]] + float(self.no_repeat):
self.userdata_dic[key] = user_datadic_new[key]
self.favourites_dic[user_datadic_new[key]] = time_now
continue
self.favourites_dic[user_datadic_new[key]] = time_now
if self.no_increase == "True" and (key == "statuses_count" or key == "media_count"):
if self.userdata_dic[key] < user_datadic_new[key]:
self.userdata_dic[key] = user_datadic_new[key]
continue
pushtext_body += "键:%s\n原值:%s\n现值:%s\n" % (
key, str(self.userdata_dic[key]), str(user_datadic_new[key]))
self.userdata_dic[key] = user_datadic_new[key]
if pushtext_body:
self.push(pushtext_body.strip())
writelog(self.logpath, '[Success] "%s" gettwitteruser %s' % (self.name, self.tgt))
except Exception as e:
printlog('[Error] "%s" gettwitteruser %s: %s' % (self.name, self.tgt, e))
writelog(self.logpath, '[Error] "%s" gettwitteruser %s: %s' % (self.name, self.tgt, e))
time.sleep(self.interval)
def push(self, pushtext_body):
pushcolor_vipdic = getpushcolordic(self.tgt, self.vip_dic)
pushcolor_dic = pushcolor_vipdic
if pushcolor_dic:
pushtext = "【%s %s 数据改变】\n%s\n时间:%s\n网址:https://twitter.com/%s" % (
self.__class__.__name__, self.tgt_name, pushtext_body, formattime(None, self.timezone), self.tgt)
pushall(pushtext, pushcolor_dic, self.push_list)
printlog('[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
writelog(self.logpath, '[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
# vip=tgt+mention, word=text
class TwitterTweet(SubMonitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s.txt' % (self.__class__.__name__, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
self.is_firstrun = True
self.tgt_restid = False
# tweet_id为整数
self.tweet_id_old = 0
def run(self):
while not self.stop_now:
# 获取用户restid
if not self.tgt_restid:
try:
tgt_dic = gettwitteruser(self.tgt, self.cookies, self.proxy)
self.tgt_restid = tgt_dic["rest_id"]
writelog(self.logpath, '[Info] "%s" gettwitteruser %s: %s' % (self.name, self.tgt, self.tgt_restid))
writelog(self.logpath, '[Success] "%s" gettwitteruser %s' % (self.name, self.tgt))
except Exception as e:
printlog('[Error] "%s" gettwitteruser %s: %s' % (self.name, self.tgt, e))
writelog(self.logpath, '[Error] "%s" gettwitteruser %s: %s' % (self.name, self.tgt, e))
time.sleep(5)
continue
# 获取推特列表
if self.tgt_restid:
try:
tweetdic_new = gettwittertweetdic(self.tgt_restid, self.cookies, self.proxy)
if self.is_firstrun:
if tweetdic_new:
self.tweet_id_old = sorted(tweetdic_new, reverse=True)[0]
writelog(self.logpath,
'[Info] "%s" gettwittertweetdic %s: %s' % (self.name, self.tgt, tweetdic_new))
self.is_firstrun = False
else:
for tweet_id in tweetdic_new:
if tweet_id > self.tweet_id_old:
self.push(tweet_id, tweetdic_new)
if tweetdic_new:
self.tweet_id_old = sorted(tweetdic_new, reverse=True)[0]
writelog(self.logpath, '[Success] "%s" gettwittertweetdic %s' % (self.name, self.tgt_restid))
except Exception as e:
printlog('[Error] "%s" gettwittertweetdic %s: %s' % (self.name, self.tgt_restid, e))
writelog(self.logpath, '[Error] "%s" gettwittertweetdic %s: %s' % (self.name, self.tgt_restid, e))
time.sleep(self.interval)
def push(self, tweet_id, tweetdic):
# 获取用户推特时大小写不敏感,但检测用户和提及的时候大小写敏感
pushcolor_vipdic = getpushcolordic("%s\n%s" % (self.tgt, tweetdic[tweet_id]['tweet_mention']),
self.vip_dic)
pushcolor_worddic = getpushcolordic(tweetdic[tweet_id]['tweet_text'], self.word_dic)
pushcolor_dic = addpushcolordic(pushcolor_vipdic, pushcolor_worddic)
if pushcolor_dic:
pushmedia = ""
if tweetdic[tweet_id]["tweet_media"]:
pushmedia = "媒体:%s\n" % tweetdic[tweet_id]["tweet_media"]
pushurl = ""
if tweetdic[tweet_id]["tweet_urls"]:
pushurl = "链接:%s\n" % tweetdic[tweet_id]["tweet_urls"]
pushtext = "【%s %s 推特%s】\n内容:%s\n%s%s时间:%s\n网址:https://twitter.com/%s/status/%s" % (
self.__class__.__name__, self.tgt_name, tweetdic[tweet_id]["tweet_type"],
tweetdic[tweet_id]["tweet_text"], pushmedia, pushurl,
formattime(tweetdic[tweet_id]["tweet_timestamp"], self.timezone), self.tgt, tweet_id)
pushall(pushtext, pushcolor_dic, self.push_list)
printlog('[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
writelog(self.logpath, '[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
# vip=tgt+mention, word=text
class TwitterFleet(SubMonitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s.txt' % (self.__class__.__name__, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
self.is_firstrun = True
self.tgt_restid = False
# fleet_id为整数
self.fleet_id_old = 0
def run(self):
while not self.stop_now:
# 获取用户restid
if not self.tgt_restid:
try:
tgt_dic = gettwitteruser(self.tgt, self.cookies, self.proxy)
self.tgt_restid = tgt_dic["rest_id"]
writelog(self.logpath, '[Info] "%s" gettwitteruser %s: %s' % (self.name, self.tgt, self.tgt_restid))
writelog(self.logpath, '[Success] "%s" gettwitteruser %s' % (self.name, self.tgt))
except Exception as e:
printlog('[Error] "%s" gettwitteruser %s: %s' % (self.name, self.tgt, e))
writelog(self.logpath, '[Error] "%s" gettwitteruser %s: %s' % (self.name, self.tgt, e))
time.sleep(5)
continue
# 获取fleet列表
if self.tgt_restid:
try:
fleetdic_new = gettwitterfleetdic(self.tgt_restid, self.cookies, self.proxy)
if self.is_firstrun:
if fleetdic_new:
self.fleet_id_old = sorted(fleetdic_new, reverse=True)[0]
writelog(self.logpath,
'[Info] "%s" gettwitterfleetdic %s: %s' % (self.name, self.tgt, fleetdic_new))
self.is_firstrun = False
else:
for fleet_id in fleetdic_new:
if fleet_id > self.fleet_id_old:
self.push(fleet_id, fleetdic_new)
if fleetdic_new:
self.fleet_id_old = sorted(fleetdic_new, reverse=True)[0]
writelog(self.logpath, '[Success] "%s" gettwitterfleetdic %s' % (self.name, self.tgt_restid))
except Exception as e:
printlog('[Error] "%s" gettwitterfleetdic %s: %s' % (self.name, self.tgt_restid, e))
writelog(self.logpath, '[Error] "%s" gettwitterfleetdic %s: %s' % (self.name, self.tgt_restid, e))
time.sleep(self.interval)
def push(self, fleet_id, fleetdic):
# 获取用户推特时大小写不敏感,但检测用户和提及的时候大小写敏感
pushcolor_vipdic = getpushcolordic("%s\n%s" % (self.tgt, fleetdic[fleet_id]['fleet_mention']),
self.vip_dic)
pushcolor_worddic = getpushcolordic(fleetdic[fleet_id]['fleet_text'], self.word_dic)
pushcolor_dic = addpushcolordic(pushcolor_vipdic, pushcolor_worddic)
if pushcolor_dic:
pushtext = "【%s %s fleet发布】\n内容:%s\n时间:%s\n网址:%s" % (
self.__class__.__name__, self.tgt_name, fleetdic[fleet_id]["fleet_text"],
formattime(fleetdic[fleet_id]["fleet_timestamp"], self.timezone), fleetdic[fleet_id]["fleet_urls"])
pushall(pushtext, pushcolor_dic, self.push_list)
printlog('[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
writelog(self.logpath, '[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
# vip=tgt+mention, word=text, only_live="True"/"False", only_liveorvideo="True"/"False", "no_chat"="True"/"False"
class TwitterSearch(SubMonitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s.txt' % (self.__class__.__name__, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
self.is_firstrun = True
self.tweet_id_old = 0
# 是否只推送有链接指向正在进行的youtube直播的推文
self.only_live = getattr(self, "only_live", "False")
# 是否只推送有链接指向youtube直播或视频的推文
self.only_liveorvideo = getattr(self, "only_liveorvideo", "False")
def run(self):
while not self.stop_now:
# 获取推特列表
try:
tweetdic_new = gettwittersearchdic(self.tgt, self.cookies, self.proxy)
if self.is_firstrun:
if tweetdic_new:
self.tweet_id_old = sorted(tweetdic_new, reverse=True)[0]
writelog(self.logpath,
'[Info] "%s" gettwittersearchdic %s: %s' % (self.name, self.tgt, tweetdic_new))
self.is_firstrun = False
else:
for tweet_id in tweetdic_new:
if tweet_id > self.tweet_id_old:
self.push(tweet_id, tweetdic_new)
if tweetdic_new:
self.tweet_id_old = sorted(tweetdic_new, reverse=True)[0]
writelog(self.logpath, '[Success] "%s" gettwittersearchdic %s' % (self.name, self.tgt))
except Exception as e:
printlog('[Error] "%s" gettwittersearchdic %s: %s' % (self.name, self.tgt, e))
writelog(self.logpath, '[Error] "%s" gettwittersearchdic %s: %s' % (self.name, self.tgt, e))
time.sleep(self.interval)
def push(self, tweet_id, tweetdic):
# 检测是否有链接指向正在进行的直播
if self.only_live == "True":
is_live = False
for url in tweetdic[tweet_id]["tweet_urls"]:
if url.count("https://youtu.be/"):
if getyoutubevideostatus(url.replace("https://youtu.be/", ""), self.proxy) == "开始":
is_live = True
break
else:
is_live = True
# 检测是否有链接指向直播或视频
if self.only_liveorvideo == "True":
is_liveorvideo = False
for url in tweetdic[tweet_id]["tweet_urls"]:
if url.count("https://youtu.be/"):
is_liveorvideo = True
break
else:
is_liveorvideo = True
if is_live and is_liveorvideo:
pushcolor_vipdic = getpushcolordic("%s\n%s" % (self.tgt, tweetdic[tweet_id]['tweet_mention']),
self.vip_dic)
pushcolor_worddic = getpushcolordic(tweetdic[tweet_id]['tweet_text'], self.word_dic)
pushcolor_dic = addpushcolordic(pushcolor_vipdic, pushcolor_worddic)
if pushcolor_dic:
pushtext = "【%s %s 推特%s】\n内容:%s\n媒体:%s\n链接:%s\n时间:%s\n网址:https://twitter.com/a/status/%s" % (
self.__class__.__name__, self.tgt_name, tweetdic[tweet_id]["tweet_type"],
tweetdic[tweet_id]["tweet_text"], tweetdic[tweet_id]["tweet_media"],
tweetdic[tweet_id]["tweet_urls"], formattime(tweetdic[tweet_id]["tweet_timestamp"], self.timezone),
tweet_id)
pushall(pushtext, pushcolor_dic, self.push_list)
printlog('[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
writelog(self.logpath, '[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
# vip=tgt, "no_chat"="True"/"False", "status_push" = "开始|结束", regen="False"/"间隔秒数", regen_amount="1"/"恢复数量"
class TwitcastLive(Monitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s.txt' % (self.__class__.__name__, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
# 重新设置submonitorconfig用于启动子线程,并添加频道id信息到子进程使用的cfg中
self.submonitorconfig_setname("twitcastchat_submonitor_cfg")
self.submonitorconfig_addconfig("twitcastchat_config", self.cfg)
self.livedic = {"": {"live_status": "结束", "live_title": ""}}
self.no_chat = getattr(self, "no_chat", "False")
self.status_push = getattr(self, "status_push", "开始|结束")
self.regen = getattr(self, "regen", "False")
self.regen_amount = getattr(self, "regen_amount", 1)
def run(self):
while not self.stop_now:
# 获取直播状态
try:
livedic_new = gettwitcastlive(self.tgt, self.cookies, self.proxy)
for live_id in livedic_new:
if live_id not in self.livedic or livedic_new[live_id]["live_status"] == "结束":
for live_id_old in self.livedic:
if self.livedic[live_id_old]["live_status"] != "结束":
self.livedic[live_id_old]["live_status"] = "结束"
self.push(live_id_old)
if live_id not in self.livedic:
self.livedic[live_id] = livedic_new[live_id]
self.push(live_id)
# 返回非空的live_id则必定为正在直播的状态,不过还是保留防止问题
elif self.livedic[live_id]["live_status"] != livedic_new[live_id]["live_status"]:
self.livedic[live_id] = livedic_new[live_id]
self.push(live_id)
writelog(self.logpath, '[Success] "%s" gettwitcastlive %s' % (self.name, self.tgt))
except Exception as e:
printlog('[Error] "%s" gettwitcastlive %s: %s' % (self.name, self.tgt, e))
writelog(self.logpath, '[Error] "%s" gettwitcastlive %s: %s' % (self.name, self.tgt, e))
time.sleep(self.interval)
def push(self, live_id):
if self.status_push.count(self.livedic[live_id]["live_status"]):
pushcolor_vipdic = getpushcolordic(self.tgt, self.vip_dic)
pushcolor_worddic = getpushcolordic(self.livedic[live_id]["live_title"], self.word_dic)
pushcolor_dic = addpushcolordic(pushcolor_vipdic, pushcolor_worddic)
if pushcolor_dic:
pushtext = "【%s %s 直播%s】\n标题:%s\n时间:%s\n网址:https://twitcasting.tv/%s" % (
self.__class__.__name__, self.tgt_name, self.livedic[live_id]["live_status"],
self.livedic[live_id]["live_title"], formattime(None, self.timezone), self.tgt)
pushall(pushtext, pushcolor_dic, self.push_list)
printlog('[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
writelog(self.logpath, '[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
if self.no_chat != "True":
# 开始记录弹幕
if self.livedic[live_id]["live_status"] == "开始":
monitor_name = "%s - TwitcastChat %s" % (self.name, live_id)
if monitor_name not in getattr(self, self.submonitor_config_name)["submonitor_dic"]:
self.submonitorconfig_addmonitor(monitor_name, "TwitcastChat", live_id, self.tgt_name,
"twitcastchat_config", tgt_channel=self.tgt, interval=2,
regen=self.regen, regen_amount=self.regen_amount)
self.checksubmonitor()
printlog('[Info] "%s" startsubmonitor %s' % (self.name, monitor_name))
writelog(self.logpath, '[Info] "%s" startsubmonitor %s' % (self.name, monitor_name))
# 停止记录弹幕
else:
monitor_name = "%s - TwitcastChat %s" % (self.name, live_id)
if monitor_name in getattr(self, self.submonitor_config_name)["submonitor_dic"]:
self.submonitorconfig_delmonitor(monitor_name)
self.checksubmonitor()
printlog('[Info] "%s" stopsubmonitor %s' % (self.name, monitor_name))
writelog(self.logpath, '[Info] "%s" stopsubmonitor %s' % (self.name, monitor_name))
'''
# vip=chat_screenname, word=text, punish=tgt+push(不包括含有'vip'的类型)
class TwitcastChat(SubMonitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s/%s.txt' % (
self.__class__.__name__, self.tgt_name, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
if not os.path.exists('./log/%s/%s' % (self.__class__.__name__, self.tgt_name)):
os.mkdir('./log/%s/%s' % (self.__class__.__name__, self.tgt_name))
self.chatpath = './log/%s/%s/%s_chat.txt' % (
self.__class__.__name__, self.tgt_name, self.name)
self.chat_id_old = 0
self.pushpunish = {}
self.regen_time = 0
self.tgt_channel = getattr(self, "tgt_channel", "")
self.regen = getattr(self, "regen", "False")
self.regen_amount = getattr(self, "regen_amount", 1)
def run(self):
while not self.stop_now:
# 获取直播评论列表
try:
chatlist = gettwitcastchatlist(self.tgt, self.cookies, self.proxy)
for chat in chatlist:
# chatlist默认从小到大排列
if self.chat_id_old < chat['chat_id']:
self.chat_id_old = chat['chat_id']
self.push(chat)
# 目标每次请求获取5条评论,间隔时间应在0.1~2秒之间
if len(chatlist) > 0:
self.interval = self.interval * 5 / len(chatlist)
else:
self.interval = 2
if self.interval > 2:
self.interval = 2
if self.interval < 0.1:
self.interval = 0.1
except Exception as e:
printlog('[Error] "%s" gettwitcastchatlist %s: %s' % (self.name, self.chat_id_old, e))
writelog(self.logpath, '[Error] "%s" gettwitcastchatlist %s: %s' % (self.name, self.chat_id_old, e))
time.sleep(self.interval)
def push(self, chat):
writelog(self.chatpath, "%s\t%s\t%s\t%s" % (
chat["chat_timestamp"], chat["chat_name"], chat["chat_screenname"], chat["chat_text"]))
pushcolor_vipdic = getpushcolordic(chat["chat_screenname"], self.vip_dic)
pushcolor_worddic = getpushcolordic(chat["chat_text"], self.word_dic)
pushcolor_dic = addpushcolordic(pushcolor_vipdic, pushcolor_worddic)
if pushcolor_dic:
pushcolor_dic = self.punish(pushcolor_dic)
pushtext = "【%s %s 直播评论】\n用户:%s(%s)\n内容:%s\n时间:%s\n网址:https://twitcasting.tv/%s" % (
self.__class__.__name__, self.tgt_name, chat["chat_name"], chat["chat_screenname"], chat["chat_text"],
formattime(chat["chat_timestamp"], self.timezone), self.tgt_channel)
pushall(pushtext, pushcolor_dic, self.push_list)
printlog('[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
writelog(self.logpath, '[Info] "%s" pushall %s\n%s' % (self.name, str(pushcolor_dic), pushtext))
def punish(self, pushcolor_dic):
if self.regen != "False":
time_now = getutctimestamp()
regen_amt = int(int((time_now - self.regen_time) / float(self.regen)) * float(self.regen_amount))
if regen_amt:
self.regen_time = time_now
for color in list(self.pushpunish):
if self.pushpunish[color] > regen_amt:
self.pushpunish[color] -= regen_amt
else:
self.pushpunish.pop(color)
if self.tgt_channel in self.vip_dic:
for color in self.vip_dic[self.tgt_channel]:
if color in pushcolor_dic and not color.count("vip"):
pushcolor_dic[color] -= self.vip_dic[self.tgt_channel][color]
for color in self.pushpunish:
if color in pushcolor_dic and not color.count("vip"):
pushcolor_dic[color] -= self.pushpunish[color]
for color in pushcolor_dic:
if pushcolor_dic[color] > 0 and not color.count("vip"):
if color in self.pushpunish:
self.pushpunish[color] += 1
else:
self.pushpunish[color] = 1
return pushcolor_dic
'''
# vip=chat_screenname, word=text, punish=tgt+push(不包括含有'vip'的类型)
class TwitcastChat(SubMonitor):
def __init__(self, name, tgt, tgt_name, cfg, **config_mod):
super().__init__(name, tgt, tgt_name, cfg, **config_mod)
self.logpath = './log/%s/%s/%s.txt' % (
self.__class__.__name__, self.tgt_name, self.name)
if not os.path.exists('./log/%s' % self.__class__.__name__):
os.mkdir('./log/%s' % self.__class__.__name__)
if not os.path.exists('./log/%s/%s' % (self.__class__.__name__, self.tgt_name)):
os.mkdir('./log/%s/%s' % (self.__class__.__name__, self.tgt_name))
self.chatpath = './log/%s/%s/%s_chat.txt' % (
self.__class__.__name__, self.tgt_name, self.name)
self.proxyhost = ""
self.proxyport = ""
if 'http' in self.proxy:
self.proxyhost = self.proxy['http'].split(':')[-2].replace('/', '')
self.proxyport = self.proxy['http'].split(':')[-1]
self.pushpunish = {}
self.regen_time = 0
self.tgt_channel = getattr(self, "tgt_channel", "")
self.regen = getattr(self, "regen", "False")
self.regen_amount = getattr(self, "regen_amount", 1)
def on_message(self, data):
try:
for chat_item in json.loads(data):
chat_type = chat_item['type']
chat_id = chat_item['id']
chat_screenname = ''