2023-02-24 17:17:00 +01:00
from setup . loginAccount import loginAndGetKey
2023-02-23 08:41:48 +01:00
from setup . fulfill import downloadFile
from decrypt . decodePDF import decryptPDF
from decrypt . decodeEPUB import decryptEPUB
2023-02-23 19:42:15 +01:00
import argparse
from os import mkdir , remove , rename
from os . path import exists
2023-02-24 11:46:01 +01:00
from sys import exit
2023-02-23 19:42:15 +01:00
from setup . params import FILE_DEVICEKEY , FILE_DEVICEXML , FILE_ACTIVATIONXML
from decrypt . params import KEYPATH
from setup . data import createDefaultFiles
2023-02-24 11:46:01 +01:00
def main ( acsmFile , login , outputFilename ) :
2023-02-23 19:42:15 +01:00
# user login
if login :
if not exists ( " account " ) :
mkdir ( " account " )
loginAndGetKey ( )
exit ( 0 )
# setting up the account and keys
if not ( exists ( FILE_ACTIVATIONXML ) and exists ( FILE_DEVICEXML ) and exists ( FILE_DEVICEKEY ) and exists ( KEYPATH ) ) :
if not exists ( " account " ) :
mkdir ( " account " )
createDefaultFiles ( )
2023-02-24 11:46:01 +01:00
print ( )
2023-02-23 19:42:15 +01:00
# cheek for file existance
if not exists ( acsmFile ) :
print ( f " { acsmFile } file does not exist " )
print ( )
exit ( 1 )
# download
encryptedFile = downloadFile ( acsmFile )
print ( encryptedFile )
print ( )
# decrypt
if encryptedFile . endswith ( " .pdf " ) :
decryptedFile = decryptPDF ( encryptedFile )
elif encryptedFile . endswith ( " .epub " ) :
decryptedFile = decryptEPUB ( encryptedFile )
2023-02-23 08:41:48 +01:00
else :
2023-02-23 19:42:15 +01:00
print ( " File format not supported " )
2023-02-23 08:41:48 +01:00
print ( )
exit ( 1 )
2023-02-23 19:42:15 +01:00
remove ( encryptedFile )
2023-02-24 11:46:01 +01:00
if outputFilename is None :
tempName = encryptedFile
else :
tempName = outputFilename
rename ( decryptedFile , tempName )
print ( tempName )
2023-02-23 08:41:48 +01:00
print ( )
2023-02-23 19:42:15 +01:00
if __name__ == " __main__ " :
parser = argparse . ArgumentParser ( description = " Download and Decrypt an encrypted PDF or EPUB file. It uses Dummy account for ADE, you can overide using --login " )
2023-02-24 11:46:01 +01:00
parser . add_argument ( " file " , type = str , nargs = ' ? ' , default = None , help = " Path to the ACSM file " )
2023-02-23 19:42:15 +01:00
parser . add_argument ( " -l " , " --login " , action = " store_true " , help = " Login to your ADE account. (optional) " )
2023-02-24 11:46:01 +01:00
parser . add_argument ( " -o " , " --output " , type = str , default = None , help = " Output file name. (optional) " )
2023-02-23 19:42:15 +01:00
args = parser . parse_args ( )
2023-02-24 11:46:01 +01:00
# check for default value
if args . file == None :
if exists ( " URLLink.acsm " ) :
args . file = " URLLink.acsm "
else :
parser . print_help ( )
exit ( 0 )
main ( args . file , args . login , args . output )