Created
March 25, 2014 12:49
-
-
Save JerryFleming/9761193 to your computer and use it in GitHub Desktop.
Parse chapters for KingReader.apk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# coding: utf-8 | |
import sqlite3 as sqlite | |
import os.path | |
import struct | |
import re | |
from binascii import hexlify | |
PIN = re.compile(r'[ ]*[^ ]+(经卷|品)第[一二三四五六七八九十百千万]+($|\(|之)', re.U) | |
HUI = re.compile(r'^第[一二三四五六七八九十百千万]+回', re.U) | |
def toNum(n, length): | |
return '{0:0{width}x}'.format(n, width=length) | |
def toByte(f): | |
return hexlify(struct.pack('>f', f)).decode() | |
def toUnicode(s): | |
return ''.join('{0:04x}'.format(ord(c)) for c in s) | |
def split(s, length): | |
return [s[i:i+length] for i in range(0, len(s), length)] | |
def getTOC(bid, path): | |
if path.startswith('/mnt/sdcard/external_sd/大正藏'.encode('utf-8')): | |
ptn = PIN | |
else: | |
ptn = HUI | |
owner = toNum(bid, 16) | |
f = open(path, 'r') | |
fsize = os.path.getsize(path) | |
offset = 0 | |
arr = [ | |
[], | |
] | |
while True: | |
lline = f.readline() | |
if len(lline)==0: break | |
line = lline.strip() | |
if ptn.match(line): | |
row = [ | |
toNum(offset, 16), | |
owner, | |
toByte(offset * 1.0 / fsize), | |
toNum(len(line), 8), | |
toUnicode(line) | |
] | |
arr.append(row) | |
print(line) | |
offset += len(lline.encode('utf8')) | |
if arr == [[]]: return '' | |
arr[0].append(toNum(len(arr) - 1, 8)) | |
s = ''.join(''.join(x) for x in arr) | |
ret = 'aced0005' | |
for item in split(s, 2048): # 1k of length | |
length = '7a' + toNum(len(item) // 2, 8) | |
ret += length + item | |
return ret | |
def main(): | |
con = sqlite.connect('/data/data/com.kingreader.framework/databases/kingreaderv3.db') | |
cur = con.cursor() | |
cur.execute('SELECT _id, fullpath FROM urls') | |
pass | |
for row in cur.fetchall(): | |
print(row) | |
path = row[1].encode('utf-8') | |
content = getTOC(row[0], path) | |
cur.execute('''UPDATE urls SET chapters=X'%s' WHERE _id=%s''' % (content, row[0])) | |
con.commit() | |
cur.close() | |
con.close() | |
def test(): | |
con = sqlite.connect('kingreaderv3.db') | |
cur = con.cursor() | |
content = getTOC(1, '../dzz/01/0157悲华经.txt') | |
cur.execute('UPDATE urls SET chapters=? WHERE _id=?', (content, 78)) | |
con.commit() | |
cur.close() | |
con.close() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment