forked from gumblex/refine-buka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buka.py
executable file
·1300 lines (1201 loc) · 41.3 KB
/
buka.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 python3
# -*- coding: utf-8 -*-
# Python 3.x
__author__ = "Gumble <[email protected]>"
__version__ = "2.5"
'''
Extract images downloaded by Buka.
Supports .buka, .bup.view, .jpg.view formats.
To use: buka.py input [output]
For help: buka.py -h
API: import buka
Main features:
* BukaFile Reads the buka file.
* ComicInfo Get comic information from chaporder.dat.
* DirMan Manages directories for converting and renaming.
* buildfromdb Build a dict of BukaFile objects from buka_store.sql
'''
import sys
if sys.version_info[0] < 3:
print('requires Python 3. try:\n python3 ' + sys.argv[0])
sys.exit(1)
import os
import platform
import shutil
import argparse
import time
import json
import struct
import sqlite3
import urllib.request, urllib.parse
import logging, logging.config
import traceback
import threadpool
from io import StringIO, BytesIO
from collections import OrderedDict, deque
from subprocess import Popen, PIPE
from multiprocessing import cpu_count
try:
# requires Pillow with WebP support
from PIL import Image
import PIL.WebPImagePlugin
SUPPORTPIL = True
# release 2.8.0 fixed webp decode memory leak
PILFIXED = tuple(map(int, (Image.PILLOW_VERSION.split(".")[:2]))) > (2, 7)
except ImportError:
SUPPORTPIL = False
PILFIXED = False
NT_SLEEP_SEC = 7
logstr = StringIO()
class BadBukaFile(Exception):
pass
class ArgumentParserWait(argparse.ArgumentParser):
'''For Windows: makes the cmd window delay.'''
def exit(self, status=0, message=None):
if message:
self._print_message(message, sys.stderr)
if os.name == 'nt':
sys.stderr.write("使用方法不正确。请将文件夹拖至软件图标使用。")
sys.stderr.flush()
time.sleep(NT_SLEEP_SEC)
sys.exit(status)
class tTree():
'''
The tTree format for directories.
tTree[('foo', 'bar', 'baz')] = 42
which auto creates:
tTree[('foo', 'bar')] = None
tTree[('foo', )] = None
'''
def __init__(self):
self.d = {}
def __len__(self):
return len(self.d)
def __getitem__(self, key):
return self.d[tuple(key)]
def __setitem__(self, key, value):
key = tuple(key)
if key not in self.d:
for i in range(1, len(key)):
if key[:i] not in self.d:
self.d[key[:i]] = None
self.d[key] = value
def __delitem__(self, key):
del self.d[tuple(key)]
def __iter__(self):
return iter(self.d)
def __contains__(self, item):
return tuple(item) in self.d
def keys(self):
return self.d.keys()
def items(self):
return self.d.items()
def values(self):
return self.d.values()
def get(self, key, default=None):
key = tuple(key)
if key in self.d:
return self.d[key]
else:
return default
def __repr__(self):
return repr(self.d)
class BukaFile:
'''Reads the buka file.'''
def __init__(self, filename):
self.filename = filename
f = self.fp = open(filename, 'rb')
buff = f.read(128)
if buff[0:4] != b'buka':
raise BadBukaFile('not a buka file')
# I guess it's the version number.
# [4:8] is more likely a (minor) version
# [9:12] may be a major version or "file type"
self.version = struct.unpack('<II', buff[4:12])
self.comicid = struct.unpack('<I', buff[12:16])[0]
self.chapid = struct.unpack('<I', buff[16:20])[0]
pos = buff.find(b'\x00', 20)
self.comicname = buff[20:pos].decode(encoding='utf-8', errors='ignore')
pos += 1
endhead = pos + struct.unpack('<I', buff[pos:pos + 4])[0] - 1
pos += 4
f.seek(pos)
buff = f.read(endhead-pos+1)
self.files = OrderedDict() # {}
pos = 0
while pos + 8 < len(buff):
pointer, size = struct.unpack('<II', buff[pos:pos + 8])
pos += 8
end = buff.find(b'\x00', pos)
name = buff[pos:end].decode(encoding='utf-8', errors='ignore')
pos = end + 1
self.files[name] = (pointer, size)
if 'chaporder.dat' in self.files:
self.fp.seek(self.files['chaporder.dat'][0])
self._chaporderdat = self.fp.read(self.files['chaporder.dat'][1])
self.chapinfo = ComicInfo(json.loads(self._chaporderdat.decode('utf-8')), self.comicid)
else:
self._chaporderdat, self.chapinfo = None, None
def __len__(self):
return len(self.files)
def __getitem__(self, key):
if key in self.files:
if key == 'chaporder.dat' and self._chaporderdat:
return self._chaporderdat
index = self.files[key]
self.fp.seek(index[0])
return self.fp.read(index[1])
else:
raise KeyError(key)
def __iter__(self):
return iter(self.files)
def __contains__(self, item):
return item in self.files
def keys(self):
return self.files.keys()
def getfile(self, key, offset=0):
'''offset is for bup files.'''
if key == 'chaporder.dat' and self._chaporderdat:
return self._chaporderdat
index = self.files[key]
self.fp.seek(index[0] + offset)
return self.fp.read(index[1] - offset)
def extract(self, key, path):
with open(path, 'wb') as w:
index = self.files[key]
self.fp.seek(index[0])
w.write(self.fp.read(index[1]))
def extractall(self, path):
if not os.path.exists(path):
os.makedirs(path)
for key in self.files:
self.extract(key, os.path.join(path, key))
def __repr__(self):
return "<BukaFile comicid=%r comicname=%r chapid=%r>" % \
(self.comicid, self.comicname, self.chapid)
def __str__(self):
return "漫画: %s, %s" % (self.comicname, self.chapid)
def close(self):
self.fp.close()
def __del__(self):
self.fp.close()
class ComicInfo:
'''
Get comic information from chaporder.dat.
This class represents the items in chaporder.dat,
and provides convenient access to chapters.
'''
def __init__(self, chaporder, comicid=None):
self.chaporder = chaporder
self.comicname = chaporder['name']
self.chap = {}
for d in chaporder['links']:
self.chap[int(d['cid'])] = d
if comicid:
self.comicid = comicid
else:
self.comicid = chaporder['logo'].split('/')[-1].split('-')[0]
if self.comicid.isdigit():
self.comicid = int(self.comicid)
else:
logging.debug("can't get comicid from url: %s", chaporder['logo'])
self.comicid = None
@staticmethod
def fromfile(filename):
return ComicInfo(json.load(open(filename)))
def renamef(self, cid):
if cid in self.chap:
if self.chap[cid]['title']:
return self.chap[cid]['title']
else:
if self.chap[cid]['type'] == '0':
return '第' + str(self.chap[cid]['idx']).zfill(3) + '话'
elif self.chap[cid]['type'] == '1':
return '第' + str(self.chap[cid]['idx']).zfill(2) + '卷'
elif self.chap[cid]['type'] == '2':
return '番外' + str(self.chap[cid]['idx']).zfill(2)
else:
return str(self.chap[cid]['idx']).zfill(3)
else:
return str(cid)
def __getitem__(self, key):
return self.chaporder[key]
def __contains__(self, item):
return item in self.chaporder
def __repr__(self):
return "<ComicInfo comicid=%r comicname=%r>" % (self.comicid, self.comicname)
def __str__(self):
return "漫画: %s" % (self.comicname)
class DirMan:
'''
Manages directories for converting and renaming.
This class mainly maintains three items:
* self.nodes - represents the directory tree and what it contains.
* self.comicdict - maintains the dictionary of known comic entries.
* self.dwebpman - puts decode requests
'''
def __init__(self, dirpath, dwebpman=None, origpath=None, comicdict={}):
self.dirpath = dirpath.rstrip('\\/')
self.origpath = (origpath or dirpath).rstrip('\\/')
self.nodes = tTree()
self.dwebpman = dwebpman
self.comicdict = comicdict
def __repr__(self):
return "<DirMan dirpath=%r origpath=%r>" % (self.dirpath, self.origpath)
def cutname(self, filename):
'''
Cuts the filename to be relative to the base directory name
to avoid renaming outer directories.
'''
return os.path.relpath(filename, os.path.dirname(self.dirpath))
def basename(self, filename):
'''
Cuts the filename to be relative to the base directory name
to avoid renaming outer directories.
'''
if filename == self.dirpath:
return os.path.basename(self.origpath)
else:
return os.path.basename(filename)
def updatecomicdict(self, comicinfo):
if comicinfo.comicid in self.comicdict:
self.comicdict[comicinfo.comicid].chaporder.update(comicinfo.chaporder)
self.comicdict[comicinfo.comicid].chap.update(comicinfo.chap)
else:
self.comicdict[comicinfo.comicid] = comicinfo
def detect(self):
'''
Only detects directory contents.
'''
for root, subFolders, files in os.walk(self.dirpath):
dtype = None
if 'chaporder.dat' in files:
filename = os.path.join(root, 'chaporder.dat')
chaporder = ComicInfo(json.load(open(filename, 'r', encoding='utf-8')))
tempid = self.basename(root)
if tempid.isdigit():
tempid = int(tempid)
if tempid == chaporder.comicid:
dtype = dtype or ('comic', chaporder.comicname)
elif tempid in chaporder.chap:
dtype = dtype or ('chap', chaporder.comicname, chaporder.renamef(tempid))
elif chaporder.comicid is None:
dtype = dtype or ('comic', chaporder.comicname)
chaporder.comicid = tempid
self.updatecomicdict(chaporder)
for name in files:
filename = os.path.join(root, name)
if detectfile(filename) == 'buka' and not subFolders and (name == 'pack.dat' or len(files)<4):
# only a buka (and a chaporder) (and an index2)
buka = BukaFile(filename)
if buka.chapinfo:
chaporder = buka.chapinfo
self.updatecomicdict(chaporder)
tempid = self.basename(root)
if tempid.isdigit():
tempid = int(tempid)
dtype = dtype or ('chap', buka.comicname, chaporder.renamef(tempid))
elif buka.comicid in self.comicdict:
dtype = dtype or ('chap', buka.comicname, self.comicdict[buka.comicid].renamef(buka.chapid))
elif detectfile(filename) == 'buka':
buka = BukaFile(filename)
sp = splitpath(self.cutname(os.path.join(root, os.path.splitext(name)[0])))
if buka.chapinfo:
chaporder = buka.chapinfo
self.updatecomicdict(chaporder)
self.nodes[sp] = ('chap', buka.comicname, chaporder.renamef(buka.chapid))
elif buka.comicid in self.comicdict:
self.nodes[sp] = ('chap', buka.comicname, self.comicdict[buka.comicid].renamef(buka.chapid))
tempid = self.basename(root)
if tempid.isdigit():
tempid = int(tempid)
if tempid == buka.comicid:
dtype = dtype or ('comic', buka.comicname)
elif detectfile(filename) == 'bup':
pass
elif detectfile(filename) == 'tmp':
pass
elif name == 'buka_store.sql':
try:
cdict = buildfromdb(filename)
for key in cdict:
self.updatecomicdict(cdict[key])
except Exception:
pass
if root == self.dirpath:
rootdir = self.origpath
else:
rootdir = root
sp = splitpath(self.cutname(root))
if not dtype:
tempid = self.basename(rootdir)
if tempid.isdigit():
tempid = int(tempid)
if tempid in self.comicdict:
dtype = ('comic', self.comicdict[tempid].comicname)
else:
tempid2 = self.basename(os.path.dirname(root))
if tempid2.isdigit():
tempid2 = int(tempid2)
if tempid2 in self.comicdict:
if tempid in self.comicdict[tempid2].chap:
dtype = ('chap', self.comicdict[tempid2].comicname, self.comicdict[tempid2].renamef(tempid))
self.nodes[sp] = dtype
return self.nodes
def detectndecode(self):
'''
Detects what the directory contains, attach it to its contents,
and decode bup/webp images.
'''
# ifndef = lambda x,y: x if x else y
# ==> x or y
if self.dwebpman is None:
raise NotImplementedError('dwebpman must be specified first.')
removefiles = []
for root, subFolders, files in os.walk(self.dirpath):
dtype = None
#frombup = set()
if 'chaporder.dat' in files:
filename = os.path.join(root, 'chaporder.dat')
chaporder = ComicInfo(json.load(open(filename, 'r', encoding='utf-8')))
logging.info(str(chaporder))
tempid = self.basename(root)
if tempid.isdigit():
tempid = int(tempid)
if tempid == chaporder.comicid:
dtype = dtype or ('comic', chaporder.comicname)
elif tempid in chaporder.chap:
dtype = dtype or ('chap', chaporder.comicname, chaporder.renamef(tempid))
elif chaporder.comicid is None:
dtype = dtype or ('comic', chaporder.comicname)
chaporder.comicid = tempid
self.updatecomicdict(chaporder)
for name in files:
filename = os.path.join(root, name)
if detectfile(filename) == 'buka' and not subFolders and (name == 'pack.dat' or len(files)<4):
# only a buka (and a chaporder) (and an index2)
logging.info('正在提取 ' + self.cutname(filename))
buka = BukaFile(filename)
logging.info(str(buka))
if buka.chapinfo:
chaporder = buka.chapinfo
self.updatecomicdict(chaporder)
tempid = self.basename(root)
if tempid.isdigit():
tempid = int(tempid)
dtype = dtype or ('chap', buka.comicname, chaporder.renamef(tempid))
elif buka.comicid in self.comicdict:
dtype = dtype or ('chap', buka.comicname, self.comicdict[buka.comicid].renamef(buka.chapid))
extractndecode(buka, root, self.dwebpman)
buka.close()
removefiles.append(filename)
elif detectfile(filename) == 'buka':
logging.info('正在提取 ' + self.cutname(filename))
buka = BukaFile(filename)
logging.info(str(buka))
sp = splitpath(self.cutname(os.path.join(root, os.path.splitext(name)[0])))
if buka.chapinfo:
chaporder = buka.chapinfo
self.updatecomicdict(chaporder)
self.nodes[sp] = ('chap', buka.comicname, chaporder.renamef(buka.chapid))
elif buka.comicid in self.comicdict:
self.nodes[sp] = ('chap', buka.comicname, self.comicdict[buka.comicid].renamef(buka.chapid))
extractndecode(buka, os.path.join(root, os.path.splitext(name)[0]), self.dwebpman)
tempid = self.basename(root)
if tempid.isdigit():
tempid = int(tempid)
if tempid == buka.comicid:
dtype = dtype or ('comic', buka.comicname)
buka.close()
removefiles.append(filename)
elif detectfile(filename) == 'bup':
basename = os.path.splitext(filename)[0]
with open(filename, 'rb') as f:
f.seek(64)
bupfile = f.read()
# Don't use JPG files to cheat me!!!!!
#trueformat = detectfile(basename + '.webp', True)
trueformat = detectfile(bupfile, True)
if trueformat == 'webp':
logging.info('加入队列 ' + self.cutname(filename))
#frombup.add(basename + '.webp')
self.dwebpman.add(basename, bupfile, self.cutname(filename))
#decodewebp(basename)
else:
with open('%s.%s' % (basename, trueformat), 'wb') as w:
w.write(bupfile)
logging.info('完成转换 ' + self.cutname(filename))
removefiles.append(filename)
elif detectfile(filename) == 'tmp':
logging.info('已忽略 ' + self.cutname(filename))
removefiles.append(filename)
# No way! don't let webp's confuse the program.
#elif detectfile(filename) == 'webp':
#if os.path.isfile(os.path.splitext(filename)[0]+'.bup') or filename in frombup:
#continue
#logging.info('加入队列 ' + self.cutname(filename))
#self.dwebpman.add(os.path.splitext(filename)[0], self.cutname(filename))
##decodewebp(os.path.splitext(filename)[0])
elif name == 'buka_store.sql':
try:
cdict = buildfromdb(filename)
for key in cdict:
self.updatecomicdict(cdict[key])
except Exception:
logging.error('不是有效的数据库: ' + self.cutname(filename))
#else:
#dtype = 'unk'
#for name in subFolders:
#pass
if root == self.dirpath:
rootdir = self.origpath
else:
rootdir = root
sp = splitpath(self.cutname(root))
if not dtype:
tempid = self.basename(rootdir)
if tempid.isdigit():
tempid = int(tempid)
if tempid in self.comicdict:
dtype = ('comic', self.comicdict[tempid].comicname)
else:
tempid2 = self.basename(os.path.dirname(root))
if tempid2.isdigit():
tempid2 = int(tempid2)
if tempid2 in self.comicdict:
if tempid in self.comicdict[tempid2].chap:
dtype = ('chap', self.comicdict[tempid2].comicname, self.comicdict[tempid2].renamef(tempid))
self.nodes[sp] = dtype
# just for the low speed of Windows
for filename in removefiles:
tryremove(filename)
def renamedirs(self):
'''Does the renaming.'''
ls = sorted(self.nodes.keys(), key=len, reverse=True)
newparentpath = self.dirpath
for i in ls:
this = self.nodes.get(i)
parent = self.nodes.get(i[:-1])
if this:
newpath = origpath = os.path.join(os.path.dirname(self.dirpath), *i)
basepath = os.path.join(os.path.dirname(self.dirpath), *i[:-1])
if this[0] == 'comic':
newpath = os.path.join(basepath, this[1])
movedir(origpath, newpath)
elif parent:
if this[1] == parent[1]:
newpath = os.path.join(basepath, this[2])
movedir(origpath, newpath)
else:
newpath = os.path.join(basepath, '%s-%s' % (this[1], this[2]))
movedir(origpath, newpath)
else:
newpath = os.path.join(basepath, '%s-%s' % (this[1], this[2]))
movedir(origpath, newpath)
if len(i) == 1:
newparentpath = newpath
return newparentpath
def movedir(src, dst):
'''Avoid conflicts when moving into an exist directory.'''
if src == dst:
pass
elif os.path.isdir(src) and os.path.isdir(dst):
for item in os.listdir(src):
movedir(os.path.join(src, item), os.path.join(dst, item))
os.rmdir(src)
else:
delayedtry(shutil.move, src, dst)
def delayedtry(fn, *args, **kwargs):
for att in range(10):
try:
fn(*args, **kwargs)
break
except Exception as ex:
logging.debug("Try failed, trying... %s" % (att+1))
if att == 9:
logging.error("文件操作失败超过重试次数。")
raise ex
time.sleep(0.2 * att)
def tryremove(filename):
'''
Tries to remove a file until it's not locked.
It's just for the LOW speed of Windows.
The exceptions are caused by the file lock is not released by System(4)
'''
for att in range(10):
try:
os.remove(filename)
break
except PermissionError as ex:
logging.debug("Delete failed, trying... %s" % (att+1))
if att == 9:
logging.error("删除文件失败超过重试次数。")
raise ex
time.sleep(0.2 * att)
def splitpath(path):
'''
Splits a path to a list.
>>> p = splitpath('a/b/c/d/')
# p = ['a', 'b', 'c', 'd']
>>> p = splitpath('/a/b/c/d')
# p = ['/', 'a', 'b', 'c', 'd']
'''
folders = []
path = path.rstrip('\\/')
while 1:
path,folder = os.path.split(path)
if folder != "":
folders.append(folder)
else:
if path != "":
folders.append(path)
break
folders.reverse()
return folders
def extractndecode(bukafile, path, dwebpman):
'''Extracts buka files and puts decode requests.'''
if not os.path.exists(path):
os.makedirs(path)
for key in bukafile.files:
if os.path.splitext(key)[1] == '.bup':
imgfile = bukafile.getfile(key, 64)
trueformat = detectfile(imgfile, True)
basename = os.path.join(path, os.path.splitext(key)[0])
if trueformat == 'webp':
dwebpman.add(basename, imgfile, os.path.join(os.path.basename(path), key))
else:
with open('%s.%s' % (basename, trueformat), 'wb') as w:
w.write(imgfile)
logging.info('完成转换 ' + os.path.join(os.path.basename(path), key))
elif key == 'logo':
imgfile = bukafile[key]
trueformat = detectfile(imgfile, True)
with open('%s.%s' % (os.path.join(path, key), trueformat), 'wb') as f:
f.write(imgfile)
else:
with open(os.path.join(path, key), 'wb') as f:
f.write(bukafile[key])
def cleandir(dirpath):
'''
Remove non-image files.
'''
_imgfiletype = frozenset(('jpg', 'png', 'webp', 'gif'))
for root, subFolders, files in os.walk(dirpath):
for name in files:
filename = os.path.join(root, name)
if detectfile(filename) not in _imgfiletype:
tryremove(filename)
def buildfromdb(dbname):
'''
Build a dict of BukaFile objects from buka_store.sql file in iOS devices.
use json.dump(<dictname>[id].chaporder) to generate chaporder.dat from db.
'''
db = sqlite3.connect(dbname)
c = db.cursor()
initd = {'author': '', #mangainfo/author
'discount': '0', 'favor': 0,
'finish': '0', #ismangaend/isend
'intro': '',
'lastup': '', #mangainfo/recentupdatename
'lastupcid': '', #Trim and lookup chapterinfo/fulltitle
'lastuptime': '', #mangainfo/recentupdatetime
'lastuptimeex': '', #mangainfo/recentupdatetime + ' 00:00:00'
'links': [], #From chapterinfo
'logo': '', #mangainfo/logopath
'logos': '', #mangainfo/logopath.split('-')[0]+'-s.jpg'
'name': '', #mangainfo/title
'popular': 9999999, 'populars': '10000000+', 'rate': '20',
'readmode': 50331648, 'readmode2': '0',
'recomctrlparam': '101696', 'recomctrltype': '1',
'recomdelay': '2000', 'recomenter': '', 'recomwords': '',
'res': [],
#'res': [{'cid': '0', #downloadview/cid
#'csize': '4942', 'restype': '1'}]
'resupno': '0', 'ret': 0, 'upno': '0'}
d = {}
c.execute('select * from mangainfo')
while 1:
lst = c.fetchone()
if not lst:
break
d[lst[0]] = initd.copy()
d[lst[0]]['name'] = lst[1]
d[lst[0]]['logo'] = lst[2]
d[lst[0]]['logos'] = lst[2].split('-')[0] + '-s.jpg'
d[lst[0]]['lastup'] = lst[3]
d[lst[0]]['lastuptime'] = lst[4]
d[lst[0]]['lastuptimeex'] = lst[4] + ' 00:00:00'
d[lst[0]]['author'] = lst[5]
c.execute('select * from ismangaend')
while 1:
lst = c.fetchone()
if not lst:
break
d[lst[0]]['finish'] = str(lst[1])
c.execute('select * from chapterinfo')
while 1:
lst = c.fetchone()
if not lst:
break
if not d[lst[0]]['links']:
d[lst[0]]['links'] = []
if not d[lst[0]]['res']:
d[lst[0]]['res'] = []
if lst[3]:
comictitle = ''
if lst[3][-1] == '卷':
comictype = '1'
elif lst[3][-1] == '话':
comictype = '0'
else:
comictype = '2'
comictitle = lst[3]
else:
comictype = '2'
comictitle = lst[2]
d[lst[0]]['links'].append({'cid': str(lst[1]), #chapterinfo/cid
'idx': str(lst[4]), #chapterinfo/idx
'ressupport': '7', 'size': '0',
'title': comictitle, 'type': comictype})
d[lst[0]]['res'].append({'cid': str(lst[1]), 'csize': '1', 'restype': '1'})
if not d[lst[0]]['lastupcid']:
if d[lst[0]]['lastup'].strip() in lst[3]:
d[lst[0]]['lastupcid'] = str(lst[1])
elif d[lst[0]]['lastup'].strip() in lst[2]:
d[lst[0]]['lastupcid'] = str(lst[1])
db.close()
return dict((k, ComicInfo(v, k)) for k,v in d.items())
def detectfile(fp, force=False):
'''
Tests file format.
If fp is str, treat it as a path;
If fp is bytes, treat it as a file;
Else, treat it as a file-like object;
Parts from standard library imghdr.
'''
if isinstance(fp, str):
if not os.path.exists(fp):
return None
if os.path.isdir(fp):
return 'dir'
if not os.path.isfile(fp):
return False
if not force:
if os.path.basename(fp) == 'index2.dat':
return 'index2'
elif os.path.basename(fp) == 'chaporder.dat':
return 'chaporder'
ext = os.path.splitext(fp)[1]
if ext == '.buka':
return 'buka'
elif ext == '.bup':
return 'bup'
elif ext == '.view':
ext2 = os.path.splitext(os.path.splitext(fp)[0])[1]
if ext2 == '.jpg':
return 'jpg'
elif ext2 == '.bup':
return 'bup'
elif ext2 == '.png':
return 'png'
elif ext == '.tmp':
return 'tmp'
with open(fp, 'rb') as f:
h = f.read(32)
elif isinstance(fp, bytes):
h = fp[:32]
else:
h = fp.peek(32)
if h[6:10] in (b'JFIF', b'Exif'):
return 'jpg'
elif h[:4] == b"bup\x00":
return 'bup'
elif h[:4] == b"RIFF" and h[8:16] == b"WEBPVP8 ":
return 'webp'
elif h[:4] == b"buka":
return 'buka'
elif h[:4] == b"AKUB" or h[12:16] == b"AKUB":
return 'index2'
elif h.startswith(b'SQLite format 3'):
return 'sqlite3'
# IMHO Buka won't be that crazy to use other formats.
elif h.startswith(b'\211PNG\r\n\032\n'):
return 'png'
elif h[:6] in (b'GIF87a', b'GIF89a'):
return 'gif'
else:
return False
def fileinfo(path):
ftype = detectfile(path)
if ftype is None:
return path + ':\n Not exist'
elif ftype is False:
return path + ':\n Unknown'
elif ftype == 'index2':
return path + ':\n Buka index2.dat image index file'
elif ftype == 'chaporder':
rv = [path + ':\n Buka chaporder.dat comic description file']
ci = ComicInfo.fromfile(path)
rv.append('Comic ID: %s' % ci.comicid)
rv.append('Comic Name: %s' % ci.comicname)
rv.append('Author: %s' % ci.chaporder.get('author'))
rv.append('Introduction: %s' % ci.chaporder.get('intro'))
return '\n'.join(rv)
elif ftype == 'bup':
rv = path + ':\n Buka bup image wrapper file, with '
with open(path, 'rb') as f:
f.seek(64)
buptype = detectfile(f)
if buptype == 'jpg':
return rv + 'JPEG image file'
elif buptype == 'webp':
return rv + 'WebP image file'
elif buptype == 'png':
return rv + 'PNG image file'
elif buptype == 'gif':
return rv + 'GIF image file'
else:
return rv + 'unknown file'
elif ftype == 'jpg':
return path + ':\n JPEG image file'
elif ftype == 'webp':
return path + ':\n WebP image file'
elif ftype == 'png':
return path + ':\n PNG image file'
elif ftype == 'gif':
return path + ':\n GIF image file'
elif ftype == 'tmp':
return path + ':\n Buka download temporary file'
elif ftype == 'sqlite3':
return path + ':\n SQLite 3.x database'
elif ftype == 'buka':
rv = [path + ':\n Buka archive file']
bf = BukaFile(path)
rv.append('Version: %s, %s' % bf.version)
rv.append('Comic ID: %s' % bf.comicid)
rv.append('Comic Name: %s' % bf.comicname)
rv.append('Chapter ID: %s' % bf.chapid)
if bf.chapinfo:
rv[0] += ', with chaporder.dat'
rv.append('Chapter Name: %s' % bf.chapinfo.renamef(bf.chapid))
rv.append('Author: %s' % bf.chapinfo.chaporder.get('author'))
rv.append('Introduction: %s' % bf.chapinfo.chaporder.get('intro'))
return '\n'.join(rv)
elif ftype == 'dir':
rv = [path + ':\n Directory']
dm = DirMan(path).detect()
dml = sorted(dm.items())
def describe(item):
if item is None:
return 'Unknown'
elif item[0] == 'comic':
return item[1]
else:
return item[2] #'%s %s' % item[1:]
for key, item in dml:
if len(key) > 1:
if item is not None and item[0] == 'chap':
if describe(dm[key[:-1]]) != item[1]:
rv.append('%s%s/: %s - %s' % (' ' * sum(map(len, key[:-1])), key[-1], item[1], item[2]))
continue
rv.append('%s%s/: %s' % (' ' * sum(map(len, key[:-1])), key[-1], describe(item)))
return '\n'.join(rv)
else:
return path + ':\n Unknown'
def folderinfo(path):
for root, dirs, files in os.walk(startpath):
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * 4 * (level)
print('{}{}/ : {}'.format(indent, os.path.basename(root)))
subindent = ' ' * 4 * (level + 1)
for f in files:
print('{}{} : {}'.format(subindent, f))
def copytree(src, dst, symlinks=False, ignore=None):
if not os.path.exists(dst):
os.makedirs(dst)
for item in os.listdir(src):
s = os.path.join(src, item)
d = os.path.join(dst, item)
if os.path.isdir(s):
copytree(s, d, symlinks, ignore)
elif detectfile(s) in ('index2','chaporder','buka','bup','jpg','png','sqlite3'): # whitelist ,'webp'
if os.path.splitext(s)[1] == '.view':
d = os.path.splitext(d)[0]
if not os.path.isfile(d) or os.stat(src).st_mtime - os.stat(dst).st_mtime > 1:
shutil.copy2(s, d)
if not os.listdir(dst):
os.rmdir(dst)
class DwebpMan:
'''
Use a pool of dwebp's to decode webps.
'''
def __init__(self, dwebppath=None, process=1, pilconvert=False, quality=92):
'''
If dwebppath is False, don't convert.
'''
self.pilconvert = pilconvert
self.quality = quality
programdir = os.path.dirname(os.path.abspath(sys.argv[0]))
self.fail = False
if '64' in platform.machine():
bit = '64'
else:
bit = '32'
logging.debug('platform.machine() = %s', platform.machine())
if dwebppath is False:
self.supportwebp = False
self.dwebp = None
self.pool = None
return
elif dwebppath:
self.dwebp = dwebppath
elif os.name == 'nt' or sys.platform in ('win32', 'cygwin'):
self.dwebp = os.path.join(programdir, 'dwebp_%s.exe' % bit)
elif sys.platform == 'darwin':
self.dwebp = os.path.join(programdir, 'dwebp_mac')
else:
self.dwebp = os.path.join(programdir, 'dwebp_' + bit)
DEVNUL = open(os.devnull, 'w')
try:
p = Popen(self.dwebp, stdout=DEVNUL, stderr=DEVNUL).wait()
self.supportwebp = True
except Exception as ex:
if os.name == 'posix':
try:
p = Popen('dwebp', stdout=DEVNUL, stderr=DEVNUL).wait()
self.supportwebp = True
self.dwebp = 'dwebp'
logging.info("used dwebp installed in the system.")
except Exception as ex:
logging.error("dwebp 不可用,仅支持普通文件格式。")
logging.debug("dwebp test: " + repr(ex))
self.supportwebp = False
else:
logging.error("dwebp 不可用,仅支持普通文件格式。")
logging.debug("dwebp test: " + repr(ex))
self.supportwebp = False
DEVNUL.close()
logging.debug("dwebp = " + self.dwebp)
if self.supportwebp:
self.pool = threadpool.NoOrderedRequestManager(process, self.decodewebp, self.checklog, self.handle_thread_exception, q_size=10)
else:
self.pool = None
def __repr__(self):
return "<DwebpMan supportwebp=%r dwebp=%r>" % (self.supportwebp, self.dwebp)
def add(self, basepath, webpfile, displayname):
'''Ignores if not supported.'''
if self.pool:
self.pool.putRequest(basepath, webpfile, displayname)
else:
with open(basepath + '.webp', 'wb') as f:
f.write(webpfile)
def wait(self):
self.pool.wait()
def checklog(self, request, result):
if 'Saved' not in result[1]:
logging.error("dwebp 错误[%d]: %s", result[0], result[1])
self.fail = True
else:
logging.info("完成转换 %s", request.args[2])
logging.debug("dwebp OK[%d]: %s", result[0], result[1])
def handle_thread_exception(self, request, exc_info):
"""Logging exception handler callback function."""
self.fail = True
# avoid dumping whole webp binary
logging.getLogger().error("<WorkRequest id=%s args[0]=%r kwargs=%r exception=%s>" % (request.requestID, request.args[0], request.kwds, request.exception))
traceback.print_exception(*exc_info, file=logstr)
def decodewebp(self, basepath, webpfile, displayname):
if self.pilconvert:
proc = Popen([self.dwebp, "-bmp", "-o", "-", "--", "-"], stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
stdout, stderr = proc.communicate(webpfile)
if stdout:
self.convertpng(basepath, stdout)
else:
# This will handled using stderr info.
pass
else:
proc = Popen([self.dwebp, "-o", basepath + ".png", "--", "-"], stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=os.getcwd())
stdout, stderr = proc.communicate(webpfile)
#tryremove(basepath + ".webp")
if stderr:
stderr = stderr.decode(errors='ignore')
return (proc.returncode, stderr)
def convertpng(self, basepath, imgdata):