diff --git a/README.md b/README.md index 6375c60..48d4f06 100644 --- a/README.md +++ b/README.md @@ -132,7 +132,7 @@ This script born as a cross-platform alternative to `KindleComicParser` by **Dc5 The app relies and includes the following scripts/binaries: - - `DualMetaFix` script by Charles **K. Hendricks**. Released with GPL-3 License. + - `DualMetaFix` script by **K. Hendricks**. Released with GPL-3 License. - `rarfile.py` script © 2005-2011 **Marko Kreen** . Released with ISC License. - `image.py` class from **Alex Yatskov**'s [Mangle](https://github.com/FooSoft/mangle/) with subsequent [proDOOMman](https://github.com/proDOOMman/Mangle)'s and [Birua](https://github.com/Birua/Mangle)'s patches. - Icon is by **Nikolay Verin** ([http://ncrow.deviantart.com/](http://ncrow.deviantart.com/)) and released under [CC BY-NC-SA 3.0](http://creativecommons.org/licenses/by-nc-sa/3.0/) License. diff --git a/kcc/KCC_gui.py b/kcc/KCC_gui.py index 7cc66a4..c439f08 100644 --- a/kcc/KCC_gui.py +++ b/kcc/KCC_gui.py @@ -37,6 +37,7 @@ from PyQt5 import QtGui, QtCore, QtWidgets from xml.dom.minidom import parse from html.parser import HTMLParser from psutil import virtual_memory, Popen, Process +from uuid import uuid4 from .shared import md5Checksum from . import comic2ebook from . import dualmetafix @@ -315,8 +316,8 @@ class DualMetaFixThread(QtCore.QRunnable): mobiPath = item.replace('.epub', '.mobi') move(mobiPath, mobiPath + '_toclean') try: - mobiedit = dualmetafix.DualMobiMetaFix(mobiPath + '_toclean') - open(mobiPath, 'wb').write(mobiedit.getresult()) + # noinspection PyArgumentList + dualmetafix.DualMobiMetaFix(mobiPath + '_toclean', mobiPath, bytes(str(uuid4()), 'UTF-8')) self.signals.result.emit([True]) except Exception as err: self.signals.result.emit([False, format(err)]) diff --git a/kcc/dualmetafix.py b/kcc/dualmetafix.py index e99fb45..1c80b58 100644 --- a/kcc/dualmetafix.py +++ b/kcc/dualmetafix.py @@ -17,7 +17,8 @@ # along with this program. If not, see . import struct -from uuid import uuid4 +import mmap +import shutil class DualMetaFixException(Exception): @@ -70,9 +71,7 @@ def replacesection(datain, secno, secdata): seclen = secend - secstart if len(secdata) != seclen: raise DualMetaFixException('section length change in replacesection') - datalst = [datain[0:secstart], secdata, datain[secend:]] - dataout = b''.join(datalst) - return dataout + datain[secstart:secstart+seclen] = secdata def get_exth_params(rec0): @@ -135,12 +134,11 @@ def del_exth(rec0, exth_num): class DualMobiMetaFix: - - def __init__(self, infile): - self.datain = open(infile, 'rb').read() + def __init__(self, infile, outfile, asin): + shutil.copyfile(infile, outfile) + f = open(outfile, "r+b") + self.datain = mmap.mmap(f.fileno(), 0) self.datain_rec0 = readsection(self.datain, 0) - # noinspection PyArgumentList - self.asin = bytes(str(uuid4()), 'UTF-8') # in the first mobi header # add 501 to "EBOK", add 113 as asin, add 504 as asin @@ -149,9 +147,9 @@ class DualMobiMetaFix: rec0 = del_exth(rec0, 113) rec0 = del_exth(rec0, 504) rec0 = add_exth(rec0, 501, b'EBOK') - rec0 = add_exth(rec0, 113, self.asin) - rec0 = add_exth(rec0, 504, self.asin) - self.datain = replacesection(self.datain, 0, rec0) + rec0 = add_exth(rec0, 113, asin) + rec0 = add_exth(rec0, 504, asin) + replacesection(self.datain, 0, rec0) ver = getint(self.datain_rec0, mobi_version) self.combo = (ver != 8) @@ -178,9 +176,9 @@ class DualMobiMetaFix: rec0 = del_exth(rec0, 113) rec0 = del_exth(rec0, 504) rec0 = add_exth(rec0, 501, b'EBOK') - rec0 = add_exth(rec0, 113, self.asin) - rec0 = add_exth(rec0, 504, self.asin) - self.datain = replacesection(self.datain, datain_kf8, rec0) + rec0 = add_exth(rec0, 113, asin) + rec0 = add_exth(rec0, 504, asin) + replacesection(self.datain, datain_kf8, rec0) - def getresult(self): - return self.datain \ No newline at end of file + self.datain.flush() + self.datain.close() \ No newline at end of file