forked from lifegpc/bili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiliDanmu.py
1660 lines (1646 loc) · 71.6 KB
/
biliDanmu.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
# (C) 2019-2020 lifegpc
# This file is part of bili.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import biliDanmuDown
from os.path import exists
from os import remove
import biliDanmuXmlParser
import biliDanmuCreate
import biliDanmuXmlFilter
import biliTime
import time
import json
import biliLogin
import biliDanmuAuto
import file
from JSONParser import getset, loadset
from file import mkdir
import sys
from command import gopt
from lang import getdict, getlan
from inspect import currentframe
from traceback import format_exc
from requests import Session
from acfunDanmu import getDanmuList
from Logger import Logger
from autoopenlist import autoopenfilelist
from nicoPara import genNicoDanmuPara
from nicoDanmu import getNicoDanmuList
from urllib.parse import urlsplit
from bstr import unescapeHTML
lan = None
se = loadset()
if se == -1 or se == -2:
se = {}
ip = {}
if len(sys.argv) > 1:
ip = gopt(sys.argv[1:])
lan = getdict('biliDanmu', getlan(se, ip))
def downloadh(filen, r, pos, da, logg=None):
d = biliDanmuDown.downloadh(pos, r, biliTime.tostr(biliTime.getDate(da)), logg)
if d == -1:
if logg is not None:
logg.write(f"d = {d}", currentframe(), "Download History Barrage Error")
print(lan['ERROR1']) # 网络错误!
return -3
if exists(filen):
remove(filen)
try:
f = open(filen, mode='w', encoding='utf8')
f.write(d)
f.close()
except:
if logg is not None:
logg.write(format_exc(), currentframe(), "Write History Barrage Error")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -1
try:
d = biliDanmuXmlParser.loadXML(filen)
remove(filen)
return d
except:
if logg is not None:
logg.write(format_exc(), currentframe(), "Read History Barrage Error")
return {'status': -2, 'd': d}
def DanmuGetn(c, data, r, t, xml, xmlc, ip: dict, se: dict):
"处理现在的弹幕"
fnl = ip['fnl'] if 'fnl' in ip else se["fnl"] if "fnl" in se else 80
log = False
logg = None
if 'logg' in ip:
log = True
logg = ip['logg']
oll = None
if 'oll' in ip:
oll = ip['oll']
ns = True
if 's' in ip:
ns = False
o = "Download/"
read = getset(se, 'o')
if read is not None:
o = read
if 'o' in ip:
o = ip['o']
fin = True
if getset(se, 'in') is False:
fin = False
if 'in' in ip:
fin = ip['in']
dmp = False
if getset(se, 'dmp') is True:
dmp = True
if 'dmp' in ip:
dmp = ip['dmp']
if t != 'av' or data['videos'] == 1:
dmp = False
if dmp:
if not fin:
o = f"{o}{file.filtern(data['title'], fnl)}/"
else:
o = "%s%s/" % (o, file.filtern(f"{data['title']}(AV{data['aid']},{data['bvid']})", fnl))
if log:
logg.write(f"ns = {ns}\no = '{o}'\nfin = {fin}\ndmp = {dmp}", currentframe(), "Normal Video Barrage Para")
try:
if not exists(o):
mkdir(o)
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video Barrage Mkdir Error")
print(lan['ERROR3'].replace('<dirname>', o)) # 创建文件夹<dirname>失败。
return -3
try:
if not exists('Temp'):
mkdir('Temp')
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video Barrage Mkdir Error2")
print(lan['ERROR3'].replace('<dirname>', "Temp")) # 创建Temp文件夹失败
return -3
if t == 'av':
d = biliDanmuDown.downloadn(data['page'][c - 1]['cid'], r, logg)
if d == -1:
if log:
logg.write(f"d = {d}", currentframe(), "Normal Video Download Barrage Failed")
print(lan['ERROR1']) # 网络错误
return -1
if data['videos'] == 1:
if fin:
filen = o + file.filtern(data['title'] + "(AV" + str(data['aid']) + ',' + data['bvid'] + ',P' + str(c) + ',' + str(data['page'][c - 1]['cid']) + ").xml", fnl)
else:
filen = o + file.filtern(f"{data['title']}.xml", fnl)
else:
if fin and not dmp:
filen = o + file.filtern(data['title'] + '-' + f"{c}." + data['page'][c - 1]['part'] + "(AV" + str(data['aid']) + ',' + data['bvid'] + ',P' + str(c) + ',' + str(data['page'][c - 1]['cid']) + ").xml", fnl)
elif not dmp:
filen = o + file.filtern(f"{data['title']}-{c}.{data['page'][c-1]['part']}.xml", fnl)
elif fin:
filen = o + file.filtern(f"{c}.{data['page'][c - 1]['part']}(P{c},{data['page'][c - 1]['cid']}).xml", fnl)
else:
filen = o + file.filtern(f"{c}.{data['page'][c - 1]['part']}.xml", fnl)
if log:
logg.write(f"filen = {filen}", currentframe(), "Normal Video Barrage Var1")
if exists(filen):
fg = False
bs = True
if not ns:
bs = False
fg = True
if 'y' in se:
fg = se['y']
bs = False
if 'y' in ip:
fg = ip['y']
bs = False
while bs:
inp = input(f"{lan['INPUT1'].replace('<filename>',filen)}(y/n)?") # 文件"<filename>"已存在,是否覆盖?
if inp[0].lower() == 'y':
bs = False
fg = True
elif inp[0].lower() == 'n':
bs = False
if fg:
try:
remove(filen)
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video Barrage Remove File Failed")
print(lan['ERROR4']) # 删除原有文件失败,跳过下载
return -1
else:
return -1
if xml == 2:
try:
f = open(filen, mode='w', encoding='utf8')
f.write(d)
f.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video Barrage Error1")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -2
if oll:
oll.add(filen)
return 0
else:
filen2 = f"Temp/n_{data['page'][c-1]['cid']}.xml"
if exists(filen2):
remove(filen2)
try:
f = open(filen2, mode='w', encoding='utf8')
f.write(d)
f.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video Barrage Error2")
print(lan['ERROR2'].replace('<filename>', filen2)) # 写入到文件"<filename>"时失败!
return -2
d = biliDanmuXmlParser.loadXML(filen2)
remove(filen2)
try:
f = open(filen, mode='w', encoding='utf8')
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video Barrage Error3")
print(lan['ERROR5'].replace('<filename>', filen)) # 打开文件"<filename>"失败
return -2
try:
f.write('<?xml version="1.0" encoding="UTF-8"?>')
f.write('<i><chatserver>%s</chatserver><chatid>%s</chatid><mission>%s</mission><maxlimit>%s</maxlimit><state>%s</state><real_name>%s</real_name><source>%s</source>' % (d['chatserver'], d['chatid'], d['mission'], d['maxlimit'], d['state'], d['real_name'], d['source']))
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video Barrage Error4")
print(lan['ERROR6'].replace('<filename>', filen)) # 保存文件失败
return -2
if ns:
print(f"{lan['OUTPUT1']}{len(d['list'])}") # 总计:
print(lan['OUTPUT2']) # 正在过滤……
filternum = 0
for i in d['list']:
read = biliDanmuXmlFilter.Filter(i, xmlc)
if read:
filternum += 1
else:
try:
f.write(biliDanmuCreate.objtoxml(i))
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video Barrage Error5")
print(lan['ERROR6'].replace('<filename>', filen)) # 保存文件失败
return -2
if ns:
print(lan['OUTPUT3'].replace('<number>', str(filternum))) # 共计过滤%s条
print(lan['OUTPUT4'].replace('<number>', str(len(d['list']) - filternum))) # 实际输出<number>条
try:
f.write('</i>')
f.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video Barrage Error6")
print(lan['ERROR6'].replace('<filename>', filen)) # 保存文件失败
return -2
if oll:
oll.add(filen)
return 0
elif t == 'ss':
d = biliDanmuDown.downloadn(c['cid'], r, logg)
if d == -1:
if log:
logg.write(f"d = {d}", currentframe(), "Bangumi Video Download Barrage Failed")
print(lan['ERROR1']) # 网络错误
return -1
pat = o + file.filtern('%s(SS%s)' % (data['mediaInfo']['title'], data['mediaInfo']['ssId']), fnl)
if log:
logg.write(f"pat = {pat}", currentframe(), "Bangumi Video Barrage Var1")
try:
if not exists(pat):
mkdir(pat)
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video Barrage Mkdir Failed")
print(lan['ERROR3'].replace('<dirname>', pat)) # 创建文件夹<dirname>失败。
return -3
if c['s'] == 'e':
if fin:
filen = '%s/%s' % (pat, file.filtern('%s.%s(%s,AV%s,%s,ID%s,%s).xml' % (c['i'] + 1, c['longTitle'], c['titleFormat'], c['aid'], c['bvid'], c['id'], c['cid']), fnl))
else:
filen = '%s/%s' % (pat, file.filtern(f"{c['i']+1}.{c['longTitle']}.xml", fnl))
else:
if fin:
filen = '%s/%s' % (pat, file.filtern('%s%s.%s(%s,AV%s,%s,ID%s,%s).xml' % (c['title'], c['i'] + 1, c['longTitle'], c['titleFormat'], c['aid'], c['bvid'], c['id'], c['cid']), fnl))
else:
filen = '%s/%s' % (pat, file.filtern(f"{c['title']}{c['i']+1}.{c['longTitle']}.xml", fnl))
if log:
logg.write(f"filen = {filen}", currentframe(), "Bangumi Video Barrage Var2")
if exists(filen):
fg = False
bs = True
if not ns:
fg = True
bs = False
if 'y' in se:
fg = se['y']
bs = False
if 'y' in ip:
fg = ip['y']
bs = False
while bs:
inp = input(f"{lan['INPUT1'].replace('<filename>',filen)}(y/n)?") # 文件"<filename>"已存在,是否覆盖?
if inp[0].lower() == 'y':
bs = False
fg = True
elif inp[0].lower() == 'n':
bs = False
if fg:
try:
remove(filen)
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video Barrage Error")
print(lan['ERROR4']) # 删除原有文件失败,跳过下载
return -1
else:
return -1
if xml == 2:
try:
f = open(filen, mode='w', encoding='utf8')
f.write(d)
f.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video Barrage Error2")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -2
if oll:
oll.add(filen)
return 0
else:
filen2 = f"Temp/n_{c['cid']}.xml"
if exists(filen2):
remove(filen2)
try:
f = open(filen2, mode='w', encoding='utf8')
f.write(d)
f.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video Barrage Error3")
print(lan['ERROR2'].replace('<filename>', filen2)) # 写入到文件"<filename>"时失败!
return -2
d = biliDanmuXmlParser.loadXML(filen2)
remove(filen2)
try:
f = open(filen, mode='w', encoding='utf8')
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video Barrage Error4")
print(lan['ERROR5'].replace('<filename>', filen)) # 打开文件"<filename>"失败
return -2
try:
f.write('<?xml version="1.0" encoding="UTF-8"?>')
f.write('<i><chatserver>%s</chatserver><chatid>%s</chatid><mission>%s</mission><maxlimit>%s</maxlimit><state>%s</state><real_name>%s</real_name><source>%s</source>' % (d['chatserver'], d['chatid'], d['mission'], d['maxlimit'], d['state'], d['real_name'], d['source']))
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video Barrage Error5")
print(lan['ERROR6'].replace('<filename>', filen)) # 保存文件失败
return -2
if ns:
print(f"{lan['OUTPUT1']}{len(d['list'])}") # 总计:
print(lan['OUTPUT2']) # 正在过滤……
filternum = 0
for i in d['list']:
read = biliDanmuXmlFilter.Filter(i, xmlc)
if read:
filternum = filternum + 1
else:
try:
f.write(biliDanmuCreate.objtoxml(i))
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video Barrage Error6")
print(lan['ERROR6'].replace('<filename>', filen)) # 保存文件失败
return -2
if ns:
print(lan['OUTPUT3'].replace('<number>', str(filternum))) # 共计过滤%s条
print(lan['OUTPUT4'].replace('<number>', str(len(d['list']) - filternum))) # 实际输出<number>条
try:
f.write('</i>')
f.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video Barrage Error7")
print(lan['ERROR6'].replace('<filename>', filen)) # 保存文件失败
return -2
if oll:
oll.add(filen)
return 0
def DanmuGeta(c, data, r, t, xml, xmlc, ip: dict, se: dict, che: bool = False):
"全弹幕处理"
fnl = ip['fnl'] if 'fnl' in ip else se["fnl"] if "fnl" in se else 80
log = False
logg = None
if 'logg' in ip:
log = True
logg = ip['logg']
oll = None
if 'oll' in ip:
oll = ip['oll']
ns = True
if 's' in ip:
ns = False
o = "Download/"
read = getset(se, 'o')
if read is not None:
o = read
if 'o' in ip:
o = ip['o']
fin = True
if getset(se, 'in') is False:
fin = False
if 'in' in ip:
fin = ip['in']
dmp = False
if getset(se, 'dmp') is True:
dmp = True
if 'dmp' in ip:
dmp = ip['dmp']
if t != 'av' or data['videos'] == 1:
dmp = False
if dmp:
if not fin:
o = f"{o}{file.filtern(data['title'], fnl)}/"
else:
o = "%s%s/" % (o, file.filtern(f"{data['title']}(AV{data['aid']},{data['bvid']})", fnl))
dwa = False
if getset(se, 'dwa') is True:
dwa = True
if 'dwa' in ip:
dwa = ip['dwa']
if log:
logg.write(f"ns = {ns}\no = '{o}'\nfin = {fin}\ndmp = {dmp}\ndwa = {dwa}", currentframe(), "All Barrage Para")
try:
if not exists(o):
mkdir(o)
except:
if log:
logg.write(format_exc(), currentframe(), "All Barrage Mkdir Error")
print(lan['ERROR3'].replace('<dirname>', o)) # 创建文件夹<dirname>失败。
return -1
try:
if not exists('Temp'):
mkdir('Temp')
except:
if log:
logg.write(format_exc(), currentframe(), "All Barrage Mkdir Error2")
print(lan['ERROR3'].replace('<dirname>', "Temp")) # 创建Temp文件夹失败
return -1
if t == 'av':
bs = True
at2 = False
fi = True
jt = False
if getset(se, 'jt') is True:
jt = True
while bs:
if fi and 'jt' in ip:
fi = False
at = ip['jt']
elif jt:
at = 'a'
elif ns:
at = input(lan['INPUT2'].replace('<value>', '1-365')) # 请输入两次抓取之间的天数(有效值为<value>,a会启用自动模式(推荐)):
else:
print(lan['ERROR7']) # 请使用"--jt <number>|a|b"来设置两次抓取之间的天数
return -1
if at.isnumeric() and int(at) <= 365 and int(at) >= 1:
at = int(at)
bs = False
elif len(at) > 0 and at[0].lower() == 'a':
at2 = True
at = 1
bs = False
if data['videos'] == 1:
if fin:
filen = o + file.filtern(data['title'] + "(AV" + str(data['aid']) + ',' + data['bvid'] + ',P' + str(c) + ',' + str(data['page'][c - 1]['cid']) + ").xml", fnl)
else:
filen = o + file.filtern(f"{data['title']}.xml", fnl)
else:
if fin and not dmp:
filen = o + file.filtern(data['title'] + '-' + f"{c}." + data['page'][c - 1]['part'] + "(AV" + str(data['aid']) + ',' + data['bvid'] + ',P' + str(c) + ',' + str(data['page'][c - 1]['cid']) + ").xml", fnl)
elif not dmp:
filen = o + file.filtern(f"{data['title']}-{c}.{data['page'][c-1]['part']}.xml", fnl)
elif fin:
filen = o + file.filtern(f"{c}.{data['page'][c - 1]['part']}(P{c},{data['page'][c - 1]['cid']}).xml", fnl)
else:
filen = o + file.filtern(f"{c}.{data['page'][c - 1]['part']}.xml", fnl)
if log:
logg.write(f"filen = {filen}", currentframe(), "Normal Video All Barrage Var")
if exists(filen):
fg = False
bs = True
if not ns:
fg = True
bs = False
if 'y' in se:
fg = se['y']
bs = False
if 'y' in ip:
fg = ip['y']
bs = False
while bs:
inp = input(f"{lan['INPUT1'].replace('<filename>',filen)}(y/n)?") # 文件"<filename>"已存在,是否覆盖?
if inp[0].lower() == 'y':
bs = False
fg = True
elif inp[0].lower() == 'n':
bs = False
if fg:
try:
remove(filen)
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error")
print(lan['ERROR4']) # 删除原有文件失败,跳过下载
return -2
else:
return -2
da = int(data['pubdate'])
zl = 0
zg = 0
zm = 0
now = 1
now2 = now
if log:
logg.write(f"da = {da}\nnow = {now}\nnow2 = {now2}", currentframe(), "Normal Video All Barrage Var2")
if ns:
print(lan['OUTPUT5']) # 正在抓取最新弹幕……
d2 = biliDanmuDown.downloadn(data['page'][c - 1]['cid'], r, logg)
if d2 == -1:
if log:
logg.write(f"d2 = {d2}", currentframe(), "Normal Video All Barrage Var3")
print(lan['ERROR1']) # 网络错误!
return -1
filen2 = f"Temp/a_{data['page'][c-1]['cid']}.xml"
if exists(filen2):
remove(filen2)
try:
f = open(filen2, mode='w', encoding='utf8')
f.write(d2)
f.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error2")
print(lan['ERROR2'].replace('<filename>', filen2)) # 写入到文件"<filename>"时失败!
return -3
d3 = biliDanmuXmlParser.loadXML(filen2)
remove(filen2)
ma = int(d3['maxlimit'])
if log:
logg.write(f"ma = {ma}", currentframe(), "Normal Video All Barrage Var4")
allok = False
if not dwa and len(d3['list']) < ma - 10:
bs = True
if not ns:
bs = False
while bs:
sts = input(f"{lan['INPUT3'].replace('<number>',str(len(d3['list']))).replace('<limit>',str(ma))}(y/n)")
if len(sts) > 0:
if sts[0].lower() == 'y':
bs = False
elif sts[0].lower() == 'n':
allok = True
bs = False
if log:
logg.write(f"allok = {allok}", currentframe(), "Normal Video All Barrage Var5")
if not allok:
d2 = d3
if ns:
print(lan['OUTPUT6'].replace('<number>', str(len(d2['list'])))) # 抓取到<number>条弹幕,最新弹幕将在最后处理
try:
f2 = open(filen, mode='w', encoding='utf8')
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error3")
print(lan['ERROR5'].replace('<filename>', filen)) # 打开文件"<filename>"失败
return -3
if not allok:
try:
f2.write('<?xml version="1.0" encoding="UTF-8"?>')
f2.write('<i><chatserver>%s</chatserver><chatid>%s</chatid><mission>%s</mission><maxlimit>%s</maxlimit><state>%s</state><real_name>%s</real_name><source>%s</source>' % (d2['chatserver'], d2['chatid'], d2['mission'], d2['maxlimit'], d2['state'], d2['real_name'], d2['source']))
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error4")
print(lan['ERROR6'].replace('<filename>', filen)) # 保存文件失败
return -3
mri = 0
mri2 = 0
t1 = 0
t2 = 0
tem = {}
fir = True
while not allok and biliTime.equal(biliTime.getDate(da), biliTime.getNowDate()) < 0 and ((not at2) or (at2 and biliTime.equal(biliTime.getDate(da + now * 24 * 3600), biliTime.getNowDate()) < 0)):
t1 = time.time()
if log:
logg.write(f"mri = {mri}\nmri2 = {mri2}\nt1 = {t1}\nt2 = {t2}\ntem.keys() = {tem.keys()}\nfir = {fir}\nda = {da}\nnow = {now}\nnow2 = {now2}", currentframe(), "Normal Video All Barrage Var6")
if (not at2) or fir:
if ns:
print(lan['OUTPUT7'].replace('<date>', biliTime.tostr(biliTime.getDate(da)))) # 正在抓取<date>的弹幕……
bs = True
ts = 300
rec = 0
while bs:
read = downloadh(filen2, r, data['page'][c - 1]['cid'], da)
if read == -1:
if log:
logg.write(f"read = {read}", currentframe(), "Normal Video All Barrage Var7")
return -1
elif read == -3:
rec = rec + 1
if log:
logg.write(f"read = {read}\nrec = {rec}", currentframe(), "Normal Video All Barrage Var8")
if rec % 5 != 0:
time.sleep(5)
print(lan['OUTPUT8'].replace('<number>', str(rec))) # 正在进行第<number>次重连
else:
bss = True
while bss:
inn = input(f"{lan['INPUT4'].replace('<number>',str(rec))}(y/n)") # 是否重连?(已经失败<number>次)
if len(inn) > 0 and inn[0].lower() == 'y':
time.sleep(5)
print(lan['OUTPUT8'].replace('<number>', str(rec))) # 正在进行第<number>次重连
elif len(inn) > 0 and inn[0].lower() == 'n':
return -1
elif 'status' in read and read['status'] == -2:
if log:
logg.write(f"read = {read}\nts = {ts}", currentframe(), "Normal Video All Barrage Var9")
obj = json.loads(read['d'])
if obj['code'] == -101:
if obj['message'] == '账户未登录':
ud = {}
read = biliLogin.login(r, ud, ip, logg)
if log:
logg.write(f"read = {read}", currentframe(), "Normal Video All Barrage Var10")
if read > 1:
return -1
else:
print(obj)
print(lan['OUTPUT9'].replace('<number>', str(ts))) # 休眠<number>s
time.sleep(ts)
ts = ts + 300
else:
print(obj)
print(lan['OUTPUT9'].replace('<number>', str(ts))) # 休眠<number>s
time.sleep(ts)
ts = ts + 300
else:
bs = False
d = read
l = 0 # noqa: E741
g = 0
if ns:
print(lan['OUTPUT10']) # 正在处理弹幕……
for i in d['list']:
if mri2 < int(i['ri']):
mri2 = int(i['ri'])
if mri < int(i['ri']):
l += 1 # noqa: E741
if xml == 2:
try:
f2.write(biliDanmuCreate.objtoxml(i))
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error5")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -3
elif xml == 1:
read = biliDanmuXmlFilter.Filter(i, xmlc)
if read:
g = g + 1
else:
try:
f2.write(biliDanmuCreate.objtoxml(i))
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error6")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -3
else:
rr = False
rr2 = False
if biliTime.tostr(biliTime.getDate(da + now * 24 * 3600)) in tem:
if ns:
print(lan['OUTPUT11'].replace('<date>', biliTime.tostr(biliTime.getDate(da + now * 24 * 3600)))) # 从内存中获取了<date>的弹幕。
read = biliDanmuAuto.reload(tem.pop(biliTime.tostr(biliTime.getDate(da + now * 24 * 3600))), mri, ns)
rr = True
if read['z'] == read['l'] and read['z'] > ma - 10 and now > 1:
rr2 = True
if log:
logg.write(f"rr = {rr}\nrr2 = {rr2}", currentframe(), "Normal Video All Barrage Var11")
if (not rr) or (rr and rr2):
if (not rr):
read = biliDanmuAuto.getMembers(filen2, r, da + now * 24 * 3600, data['page'][c - 1]['cid'], mri, ip)
if read == -1:
if log:
logg.write(f"read = {read}", currentframe(), "Normal Video All Barrage Var12")
return -3
while read['z'] == read['l'] and read['z'] > ma - 10 and now > 1:
# if ns:
# print('尝试抓取了%s的弹幕,获取到%s条有效弹幕,未防止遗漏,间隔时间减半' % (biliTime.tostr(biliTime.getDate(da+now*24*3600)),read['l']))
tem[biliTime.tostr(biliTime.getDate(da + now * 24 * 3600))] = read
now = now / 2
if now < 1:
now = 1
read = biliDanmuAuto.getMembers(filen2, r, da + now * 24 * 3600, data['page'][c - 1]['cid'], mri, ip)
if read == -1:
if log:
logg.write(f"read = {read}", currentframe(), "Normal Video All Barrage Var13")
return -3
now2 = now
if read['l'] < ma * 0.5:
now2 = now * 2
if now2 > 365:
now2 = 365
l = read['l'] # noqa: E741
g = 0
mri2 = read['m']
if ns:
print(lan['OUTPUT12']) # 正在处理……
for i in read['d']['list']:
if xml == 2:
try:
f2.write(biliDanmuCreate.objtoxml(i))
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error7")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -3
elif xml == 1:
read = biliDanmuXmlFilter.Filter(i, xmlc)
if read:
g = g + 1
else:
try:
f2.write(biliDanmuCreate.objtoxml(i))
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error8")
print(lan['ERROR2'].replace('<filename>', filen2)) # 写入到文件"<filename>"时失败!
return -3
bs2 = True
while bs2 and biliTime.equal(biliTime.getDate(da + (now2 + now) * 24 * 3600), biliTime.getNowDate()) >= 0:
if allok:
read = biliDanmuAuto.getnownumber(d3, mri2)
else:
read = biliDanmuAuto.getnownumber(d2, mri2)
if read['l'] == read['m']:
now2 = now2 / 2
if now2 < 1:
now2 = 1
else:
bs2 = False
m = l - g
zl = zl + l
zm = zm + m
zg = zg + g
if ns:
print(lan['OUTPUT13'].replace('<number>', f"{l}({zl})")) # 获取了<number>条弹幕。
if xml == 1 and ns:
print(lan['OUTPUT14'].replace('<number>', f"{g}({zg})")) # 过滤了<number>条弹幕。
print(lan['OUTPUT15'].replace('<number>', f"{m}({zm})")) # 实际输出了<number>条弹幕。
if t2 == 0 or t1 - t2 < 2:
time.sleep(2)
t2 = t1
if not at2:
da = da + at * 3600 * 24
elif fir:
fir = False
else:
da = da + now * 3600 * 24
now = now2
mri = mri2
if not allok:
if ns:
print(lan['OUTPUT16']) # 开始处理最新的弹幕……
l = 0 # noqa: E741
g = 0
for i in d2['list']:
if int(mri) < int(i['ri']):
l = l + 1 # noqa: E741
if xml == 2:
try:
f2.write(biliDanmuCreate.objtoxml(i))
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error9")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -3
elif xml == 1:
read = biliDanmuXmlFilter.Filter(i, xmlc)
if read:
g = g + 1
else:
try:
f2.write(biliDanmuCreate.objtoxml(i))
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error10")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -3
try:
f2.write('</i>')
f2.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error11")
print(lan['ERROR2'].replace('<filename>', filen2)) # 写入到文件"<filename>"时失败!
return -3
m = l - g
zl = zl + l
zg = zg + g
zm = zm + m
if ns:
print(lan['OUTPUT17'].replace('<number>', str(l))) # 在最新弹幕中获取新弹幕<number>条。
if xml == 1 and ns:
print(lan['OUTPUT14'].replace('<number>', str(g))) # 过滤了<number>条弹幕。
print(lan['OUTPUT15'].replace('<number>', str(m))) # 实际输出了<number>条弹幕。
if ns:
print(lan['OUTPUT18'].replace('<number>', str(zl))) # 总共获取了<number>条弹幕
if xml == 1 and ns:
print(lan['OUTPUT3'].replace('<number>', str(zg))) # 共计过滤<number>条弹幕。
print(lan['OUTPUT4'].replace('<number>', str(zm))) # 实际输出<number>条弹幕。
else:
if xml == 2:
try:
f2.write(d2)
f2.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error12")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -3
if xml == 1:
z = len(d3['list'])
g = 0
for i in d3['list']:
read = biliDanmuXmlFilter.Filter(i, xmlc)
if read:
g = g + 1
else:
try:
f2.write(biliDanmuCreate.objtoxml(i))
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error13")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -3
try:
f2.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Normal Video All Barrage Error14")
print(lan['ERROR2'].replace('<filename>', filen)) # 写入到文件"<filename>"时失败!
return -3
m = z - g
if ns:
print(lan['OUTPUT13'].replace('<number>', str(z))) # 获取了<number>条弹幕。
print(lan['OUTPUT14'].replace('<number>', str(g))) # 过滤了<number>条弹幕。
print(lan['OUTPUT15'].replace('<number>', str(m))) # 实际输出了<number>条弹幕。
if oll:
oll.add(filen)
return 0
elif t == 'ss':
bs = True
at2 = False
if not che:
pubt = data['mediaInfo']['time'][0:10]
else:
pubt = biliTime.tostr2(c['time'])[0:10]
fi = True
jt = False
if getset(se, 'jt') is True:
jt = True
if 'jts' in ip:
pubt = ip['jts']
while bs:
if fi and 'jt' in ip:
fi = False
at = ip['jt']
elif jt:
at = 'a'
elif ns:
at = input(lan['INPUT5'].replace('<value>', '1-365').replace('<date>', str(pubt))) # 请输入两次抓取之间的天数(有效值为<value>,输入a会启用自动模式(推荐),输入b可手动输入开始抓取的日期,日期目前为<date>):
else:
print(lan['ERROR7']) # 请使用"--jt <number>|a|b"来设置两次抓取之间的天数
return -1
if at.isnumeric() and int(at) <= 365 and int(at) >= 1:
at = int(at)
bs = False
elif len(at) > 0 and at[0].lower() == 'a':
at2 = True
at = 1
bs = False
elif len(at) > 0 and at[0].lower() == 'b':
if not ns:
print(lan['ERROR8']) # 请使用"--jts <date>"来修改抓取开始时间。
return -1
at3 = input(lan['INPUT6']) # 请输入日期(格式为年-月-日,例如1989-02-25):
if len(at3) > 0:
if biliTime.checktime(at3):
pubt = time.strftime('%Y-%m-%d', time.strptime(at3, '%Y-%m-%d'))
else:
print(lan['ERROR9']) # 输入格式有误或者该日期不存在。
pubt = biliTime.mkt(time.strptime(pubt, '%Y-%m-%d'))
da = int(pubt)
pat = o + file.filtern('%s(SS%s)' % (data['mediaInfo']['title'], data['mediaInfo']['ssId']), fnl)
if log:
logg.write(f"da = {da}\npat = {pat}", currentframe(), "Bangumi Video All Barrage Var")
try:
if not exists(pat):
mkdir(pat)
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video All Barrage Mkdir Failed")
print(lan['ERROR3'].replace('<dirname>', pat)) # 创建文件夹<dirname>失败。
return -1
if c['s'] == 'e':
if fin:
filen = '%s/%s' % (pat, file.filtern('%s.%s(%s,AV%s,%s,ID%s,%s).xml' % (c['i'] + 1, c['longTitle'], c['titleFormat'], c['aid'], c['bvid'], c['id'], c['cid']), fnl))
else:
filen = '%s/%s' % (pat, file.filtern(f"{c['i']+1}.{c['longTitle']}.xml", fnl))
else:
if fin:
filen = '%s/%s' % (pat, file.filtern('%s%s.%s(%s,AV%s,%s,ID%s,%s).xml' % (c['title'], c['i'] + 1, c['longTitle'], c['titleFormat'], c['aid'], c['bvid'], c['id'], c['cid']), fnl))
else:
filen = '%s/%s' % (pat, file.filtern(f"{c['title']}{c['i']+1}.{c['longTitle']}.xml", fnl))
if log:
logg.write(f"filen = {filen}", currentframe(), "Bangumi Video All Barrage Var2")
if exists(filen):
fg = False
bs = True
if not ns:
fg = True
bs = False
if 'y' in se:
fg = se['y']
bs = False
if 'y' in ip:
fg = ip['y']
bs = False
while bs:
inp = input(f"{lan['INPUT1'].replace('<filename>',filen)}(y/n)?") # 文件"<filename>"已存在,是否覆盖?
if inp[0].lower() == 'y':
bs = False
fg = True
elif inp[0].lower() == 'n':
bs = False
if fg:
try:
remove(filen)
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video All Barrage Remove File Failed")
print(lan['ERROR4']) # 删除原有文件失败,跳过下载
return -2
else:
return -2
zl = 0
zg = 0
zm = 0
now = 1
now2 = now
if ns:
print(lan['OUTPUT5']) # 正在抓取最新弹幕……
d2 = biliDanmuDown.downloadn(c['cid'], r, logg)
if d2 == -1:
if log:
logg.write(f"d2 = {d2}", currentframe(), "Bangumi Video All Barrage Var3")
print(lan['ERROR1']) # 网络错误!
return -1
filen2 = f"Temp/a_{c['cid']}.xml"
if exists(filen2):
remove(filen2)
try:
f = open(filen2, mode='w', encoding='utf8')
f.write(d2)
f.close()
except:
if log:
logg.write(format_exc(), currentframe(), "Bangumi Video All Barrage Error")
print(lan['ERROR2'].replace('<filename>', filen2)) # 写入到文件"<filename>"时失败!
return -3
d3 = biliDanmuXmlParser.loadXML(filen2)
remove(filen2)
ma = int(d3['maxlimit'])
if log:
logg.write(f"ma = {ma}", currentframe(), "Bangumi Video All Barrage Var4")
allok = False
if not dwa and len(d3['list']) < ma - 10:
bs = True
if not ns:
bs = False