2012-11-20 19:28:12 +06:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# This is a python script. You need a Python interpreter to run it.
|
|
|
|
# For example, ActiveState Python, which exists for windows.
|
|
|
|
#
|
|
|
|
# Changelog
|
|
|
|
# 1.00 - Initial version
|
|
|
|
# 1.01 - getPidList interface change
|
2012-09-09 06:45:24 +06:00
|
|
|
|
2012-11-20 19:28:12 +06:00
|
|
|
__version__ = '1.01'
|
2012-11-07 19:14:25 +06:00
|
|
|
|
2012-11-20 19:28:12 +06:00
|
|
|
import sys
|
2012-11-07 19:14:25 +06:00
|
|
|
|
2012-12-25 05:31:34 +06:00
|
|
|
class SafeUnbuffered:
|
2012-11-20 19:28:12 +06:00
|
|
|
def __init__(self, stream):
|
|
|
|
self.stream = stream
|
2012-12-25 05:31:34 +06:00
|
|
|
self.encoding = stream.encoding
|
|
|
|
if self.encoding == None:
|
|
|
|
self.encoding = "utf-8"
|
2012-11-20 19:28:12 +06:00
|
|
|
def write(self, data):
|
2012-12-25 05:31:34 +06:00
|
|
|
if isinstance(data,unicode):
|
|
|
|
data = data.encode(self.encoding,"replace")
|
2012-11-20 19:28:12 +06:00
|
|
|
self.stream.write(data)
|
|
|
|
self.stream.flush()
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
return getattr(self.stream, attr)
|
2012-12-25 05:31:34 +06:00
|
|
|
sys.stdout=SafeUnbuffered(sys.stdout)
|
|
|
|
sys.stderr=SafeUnbuffered(sys.stderr)
|
2012-11-07 19:14:25 +06:00
|
|
|
|
2012-11-20 19:28:12 +06:00
|
|
|
import os
|
|
|
|
import struct
|
|
|
|
import binascii
|
|
|
|
import kgenpids
|
|
|
|
import topazextract
|
|
|
|
import mobidedrm
|
|
|
|
from alfcrypto import Pukall_Cipher
|
|
|
|
|
|
|
|
class DrmException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def getK4PCpids(path_to_ebook):
|
|
|
|
# Return Kindle4PC PIDs. Assumes that the caller checked that we are not on Linux, which will raise an exception
|
|
|
|
|
|
|
|
mobi = True
|
|
|
|
magic3 = file(path_to_ebook,'rb').read(3)
|
|
|
|
if magic3 == 'TPZ':
|
|
|
|
mobi = False
|
|
|
|
|
|
|
|
if mobi:
|
2012-12-24 20:55:19 +06:00
|
|
|
mb = mobidedrm.MobiBook(path_to_ebook)
|
2012-11-20 19:28:12 +06:00
|
|
|
else:
|
|
|
|
mb = topazextract.TopazBook(path_to_ebook)
|
2012-11-07 19:14:25 +06:00
|
|
|
|
2012-11-20 19:28:12 +06:00
|
|
|
md1, md2 = mb.getPIDMetaInfo()
|
2012-11-07 19:14:25 +06:00
|
|
|
|
2012-11-20 19:28:12 +06:00
|
|
|
return kgenpids.getPidList(md1, md2)
|
2012-11-07 19:14:25 +06:00
|
|
|
|
|
|
|
|
2012-11-20 19:28:12 +06:00
|
|
|
def main(argv=sys.argv):
|
|
|
|
print ('getk4pcpids.py v%(__version__)s. '
|
|
|
|
'Copyright 2012 Apprentice Alf' % globals())
|
2012-11-07 19:14:25 +06:00
|
|
|
|
2012-11-20 19:28:12 +06:00
|
|
|
if len(argv)<2 or len(argv)>3:
|
|
|
|
print "Gets the possible book-specific PIDs from K4PC for a particular book"
|
|
|
|
print "Usage:"
|
|
|
|
print " %s <bookfile> [<outfile>]" % sys.argv[0]
|
|
|
|
return 1
|
2012-09-09 06:45:24 +06:00
|
|
|
else:
|
2012-11-20 19:28:12 +06:00
|
|
|
infile = argv[1]
|
|
|
|
try:
|
|
|
|
pidlist = getK4PCpids(infile)
|
|
|
|
except DrmException, e:
|
|
|
|
print "Error: %s" % e
|
|
|
|
return 1
|
|
|
|
pidstring = ','.join(pidlist)
|
|
|
|
print "Possible PIDs are: ", pidstring
|
|
|
|
if len(argv) is 3:
|
|
|
|
outfile = argv[2]
|
|
|
|
file(outfile, 'w').write(pidstring)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
sys.exit(main())
|