2011-06-16 11:59:20 +06:00
#!/usr/bin/env python
from __future__ import with_statement
from calibre . customize import FileTypePlugin
from calibre . gui2 import is_ok_to_use_qt
# from calibre.ptempfile import PersistentTemporaryDirectory
from calibre_plugins . k4mobidedrm import kgenpids
from calibre_plugins . k4mobidedrm import topazextract
from calibre_plugins . k4mobidedrm import mobidedrm
import sys
import os
import re
class K4DeDRM ( FileTypePlugin ) :
name = ' K4PC, K4Mac, Kindle Mobi and Topaz DeDRM ' # Name of the plugin
2011-09-05 12:17:36 +06:00
description = ' Removes DRM from Mobipocket, Kindle/Mobi, Kindle/Topaz and Kindle/Print Replica files. Provided by the work of many including DiapDealer, SomeUpdates, IHeartCabbages, CMBDTC, Skindle, DarkReverser, ApprenticeAlf, etc. '
2011-06-16 11:59:20 +06:00
supported_platforms = [ ' osx ' , ' windows ' , ' linux ' ] # Platforms this plugin will run on
author = ' DiapDealer, SomeUpdates ' # The author of this plugin
2011-09-05 12:17:36 +06:00
version = ( 0 , 3 , 7 ) # The version number of this plugin
file_types = set ( [ ' prc ' , ' mobi ' , ' azw ' , ' azw1 ' , ' azw4 ' , ' tpz ' ] ) # The file types that this plugin will be applied to
2011-06-16 11:59:20 +06:00
on_import = True # Run this plugin during the import
priority = 210 # run this plugin before mobidedrm, k4pcdedrm, k4dedrm
minimum_calibre_version = ( 0 , 7 , 55 )
def run ( self , path_to_ebook ) :
2011-09-05 12:17:36 +06:00
plug_ver = ' . ' . join ( str ( self . version ) . strip ( ' () ' ) . replace ( ' ' , ' ' ) . split ( ' , ' ) )
2011-06-16 11:59:20 +06:00
k4 = True
if sys . platform . startswith ( ' linux ' ) :
k4 = False
pids = [ ]
serials = [ ]
kInfoFiles = [ ]
# Get supplied list of PIDs to try from plugin customization.
customvalues = self . site_customization . split ( ' , ' )
for customvalue in customvalues :
customvalue = str ( customvalue )
customvalue = customvalue . strip ( )
if len ( customvalue ) == 10 or len ( customvalue ) == 8 :
pids . append ( customvalue )
else :
if len ( customvalue ) == 16 and customvalue [ 0 ] == ' B ' :
serials . append ( customvalue )
else :
print " %s is not a valid Kindle serial number or PID. " % str ( customvalue )
2011-09-05 12:17:36 +06:00
2011-06-16 11:59:20 +06:00
# Load any kindle info files (*.info) included Calibre's config directory.
try :
# Find Calibre's configuration directory.
confpath = os . path . split ( os . path . split ( self . plugin_path ) [ 0 ] ) [ 0 ]
2011-09-05 12:17:36 +06:00
print ' K4MobiDeDRM v %s : Calibre configuration directory = %s ' % ( plug_ver , confpath )
2011-06-16 11:59:20 +06:00
files = os . listdir ( confpath )
filefilter = re . compile ( " \ .info$| \ .kinf$ " , re . IGNORECASE )
files = filter ( filefilter . search , files )
if files :
for filename in files :
fpath = os . path . join ( confpath , filename )
kInfoFiles . append ( fpath )
2011-09-05 12:17:36 +06:00
print ' K4MobiDeDRM v %s : Kindle info/kinf file %s found in config folder. ' % ( plug_ver , filename )
2011-06-16 11:59:20 +06:00
except IOError :
2011-09-05 12:17:36 +06:00
print ' K4MobiDeDRM v %s : Error reading kindle info/kinf files from config directory. ' % plug_ver
2011-06-16 11:59:20 +06:00
pass
mobi = True
magic3 = file ( path_to_ebook , ' rb ' ) . read ( 3 )
if magic3 == ' TPZ ' :
mobi = False
bookname = os . path . splitext ( os . path . basename ( path_to_ebook ) ) [ 0 ]
if mobi :
mb = mobidedrm . MobiBook ( path_to_ebook )
else :
mb = topazextract . TopazBook ( path_to_ebook )
title = mb . getBookTitle ( )
md1 , md2 = mb . getPIDMetaInfo ( )
pidlst = kgenpids . getPidList ( md1 , md2 , k4 , pids , serials , kInfoFiles )
try :
mb . processBook ( pidlst )
2011-09-05 12:17:36 +06:00
except mobidedrm . DrmException , e :
2011-06-16 11:59:20 +06:00
#if you reached here then no luck raise and exception
if is_ok_to_use_qt ( ) :
from PyQt4 . Qt import QMessageBox
2011-09-05 12:17:36 +06:00
d = QMessageBox ( QMessageBox . Warning , " K4MobiDeDRM v %s Plugin " % plug_ver , " Error: " + str ( e ) + " ... %s \n " % path_to_ebook )
2011-06-16 11:59:20 +06:00
d . show ( )
d . raise_ ( )
d . exec_ ( )
2011-09-05 12:17:36 +06:00
raise Exception ( " K4MobiDeDRM plugin v %s Error: %s " % ( plug_ver , str ( e ) ) )
except topazextract . TpzDRMError , e :
2011-06-16 11:59:20 +06:00
#if you reached here then no luck raise and exception
if is_ok_to_use_qt ( ) :
from PyQt4 . Qt import QMessageBox
2011-09-05 12:17:36 +06:00
d = QMessageBox ( QMessageBox . Warning , " K4MobiDeDRM v %s Plugin " % plug_ver , " Error: " + str ( e ) + " ... %s \n " % path_to_ebook )
2011-06-16 11:59:20 +06:00
d . show ( )
d . raise_ ( )
d . exec_ ( )
2011-09-05 12:17:36 +06:00
raise Exception ( " K4MobiDeDRM plugin v %s Error: %s " % ( plug_ver , str ( e ) ) )
2011-06-16 11:59:20 +06:00
print " Success! "
if mobi :
2011-09-05 12:17:36 +06:00
if mb . getPrintReplica ( ) :
of = self . temporary_file ( bookname + ' .azw4 ' )
print ' K4MobiDeDRM v %s : Print Replica format detected. ' % plug_ver
else :
of = self . temporary_file ( bookname + ' .mobi ' )
2011-06-16 11:59:20 +06:00
mb . getMobiFile ( of . name )
2011-09-05 12:17:36 +06:00
else :
2011-06-16 11:59:20 +06:00
of = self . temporary_file ( bookname + ' .htmlz ' )
mb . getHTMLZip ( of . name )
mb . cleanup ( )
return of . name
def customization_help ( self , gui = False ) :
return ' Enter 10 character PIDs and/or Kindle serial numbers, use a comma (no spaces) to separate each PID or SerialNumber from the next. '