-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Richard Peng
committed
Nov 26, 2010
1 parent
6b1b51c
commit 77fe0a2
Showing
3 changed files
with
161 additions
and
50 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python | ||
#author:Richard Peng | ||
#project:Kindelabra | ||
#website:http://www.richardpeng.com/projects/kindelabra/ | ||
#repository:https://github.com/richardpeng/Kindelabra | ||
#license:Creative Commons GNU GPL v2 | ||
# (http://creativecommons.org/licenses/GPL/2.0/) | ||
|
||
import struct | ||
|
||
class Sectionizer: | ||
def __init__(self, filename, perm): | ||
self.f = file(filename, perm) | ||
header = self.f.read(78) | ||
self.ident = header[0x3C:0x3C+8] | ||
if self.ident != 'BOOKMOBI' and self.ident != 'TEXtREAd': | ||
raise ValueError('invalid file format') | ||
num_sections, = struct.unpack_from('>H', header, 76) | ||
sections = self.f.read(num_sections*8) | ||
self.sections = struct.unpack_from('>%dL' % (num_sections*2), sections, 0)[::2] + (0xfffffff, ) | ||
|
||
def loadSection(self, section): | ||
before, after = self.sections[section:section+2] | ||
self.f.seek(before) | ||
return self.f.read(after - before) | ||
|
||
class Mobi: | ||
def __init__(self, filename): | ||
sections = Sectionizer(filename, 'rb') | ||
header = sections.loadSection(0) | ||
len_mobi = struct.unpack_from('>L', header, 20)[0] + 16 | ||
mobi_raw = header[:len_mobi] | ||
titleoffset, titlelen = struct.unpack_from('>LL', mobi_raw, 84) | ||
self.title = header[titleoffset:titleoffset+titlelen] | ||
len_exth, = struct.unpack_from('>L', header, len_mobi+4) | ||
exth_records = header[len_mobi:len_mobi+len_exth][12:] | ||
self.exth = dict() | ||
while len(exth_records) > 8: | ||
rectype, reclen = struct.unpack_from('>LL', exth_records) | ||
recdata = exth_records[8:reclen] | ||
self.exth[rectype] = recdata | ||
exth_records = exth_records[reclen:] | ||
|
||
def main(): | ||
m = Mobi('book.azw') | ||
if 113 in m.exth: | ||
print m.exth[113] | ||
|
||
if __name__ == "__main__": | ||
main() |
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