1
0
mirror of https://github.com/ciromattia/kcc synced 2025-12-21 21:51:52 +00:00

Implemented new DualMetaFix version

This commit is contained in:
Paweł Jastrzębski
2014-05-28 06:57:37 +02:00
parent de74318c69
commit f53cdf9cd7
3 changed files with 19 additions and 20 deletions

View File

@@ -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: 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 &copy; 2005-2011 **Marko Kreen** <markokr@gmail.com>. Released with ISC License. - `rarfile.py` script &copy; 2005-2011 **Marko Kreen** <markokr@gmail.com>. 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. - `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. - 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.

View File

@@ -37,6 +37,7 @@ from PyQt5 import QtGui, QtCore, QtWidgets
from xml.dom.minidom import parse from xml.dom.minidom import parse
from html.parser import HTMLParser from html.parser import HTMLParser
from psutil import virtual_memory, Popen, Process from psutil import virtual_memory, Popen, Process
from uuid import uuid4
from .shared import md5Checksum from .shared import md5Checksum
from . import comic2ebook from . import comic2ebook
from . import dualmetafix from . import dualmetafix
@@ -315,8 +316,8 @@ class DualMetaFixThread(QtCore.QRunnable):
mobiPath = item.replace('.epub', '.mobi') mobiPath = item.replace('.epub', '.mobi')
move(mobiPath, mobiPath + '_toclean') move(mobiPath, mobiPath + '_toclean')
try: try:
mobiedit = dualmetafix.DualMobiMetaFix(mobiPath + '_toclean') # noinspection PyArgumentList
open(mobiPath, 'wb').write(mobiedit.getresult()) dualmetafix.DualMobiMetaFix(mobiPath + '_toclean', mobiPath, bytes(str(uuid4()), 'UTF-8'))
self.signals.result.emit([True]) self.signals.result.emit([True])
except Exception as err: except Exception as err:
self.signals.result.emit([False, format(err)]) self.signals.result.emit([False, format(err)])

View File

@@ -17,7 +17,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import struct import struct
from uuid import uuid4 import mmap
import shutil
class DualMetaFixException(Exception): class DualMetaFixException(Exception):
@@ -70,9 +71,7 @@ def replacesection(datain, secno, secdata):
seclen = secend - secstart seclen = secend - secstart
if len(secdata) != seclen: if len(secdata) != seclen:
raise DualMetaFixException('section length change in replacesection') raise DualMetaFixException('section length change in replacesection')
datalst = [datain[0:secstart], secdata, datain[secend:]] datain[secstart:secstart+seclen] = secdata
dataout = b''.join(datalst)
return dataout
def get_exth_params(rec0): def get_exth_params(rec0):
@@ -135,12 +134,11 @@ def del_exth(rec0, exth_num):
class DualMobiMetaFix: class DualMobiMetaFix:
def __init__(self, infile, outfile, asin):
def __init__(self, infile): shutil.copyfile(infile, outfile)
self.datain = open(infile, 'rb').read() f = open(outfile, "r+b")
self.datain = mmap.mmap(f.fileno(), 0)
self.datain_rec0 = readsection(self.datain, 0) self.datain_rec0 = readsection(self.datain, 0)
# noinspection PyArgumentList
self.asin = bytes(str(uuid4()), 'UTF-8')
# in the first mobi header # in the first mobi header
# add 501 to "EBOK", add 113 as asin, add 504 as asin # 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, 113)
rec0 = del_exth(rec0, 504) rec0 = del_exth(rec0, 504)
rec0 = add_exth(rec0, 501, b'EBOK') rec0 = add_exth(rec0, 501, b'EBOK')
rec0 = add_exth(rec0, 113, self.asin) rec0 = add_exth(rec0, 113, asin)
rec0 = add_exth(rec0, 504, self.asin) rec0 = add_exth(rec0, 504, asin)
self.datain = replacesection(self.datain, 0, rec0) replacesection(self.datain, 0, rec0)
ver = getint(self.datain_rec0, mobi_version) ver = getint(self.datain_rec0, mobi_version)
self.combo = (ver != 8) self.combo = (ver != 8)
@@ -178,9 +176,9 @@ class DualMobiMetaFix:
rec0 = del_exth(rec0, 113) rec0 = del_exth(rec0, 113)
rec0 = del_exth(rec0, 504) rec0 = del_exth(rec0, 504)
rec0 = add_exth(rec0, 501, b'EBOK') rec0 = add_exth(rec0, 501, b'EBOK')
rec0 = add_exth(rec0, 113, self.asin) rec0 = add_exth(rec0, 113, asin)
rec0 = add_exth(rec0, 504, self.asin) rec0 = add_exth(rec0, 504, asin)
self.datain = replacesection(self.datain, datain_kf8, rec0) replacesection(self.datain, datain_kf8, rec0)
def getresult(self): self.datain.flush()
return self.datain self.datain.close()