degourou/DeGourou.py

141 lines
4.2 KiB
Python
Raw Normal View History

2023-02-24 11:17:00 -05:00
from setup.loginAccount import loginAndGetKey
2023-02-23 02:41:48 -05:00
from setup.fulfill import downloadFile
from decrypt.decodePDF import decryptPDF
from decrypt.decodeEPUB import decryptEPUB
2023-02-23 13:42:15 -05:00
import argparse
from os import mkdir, remove, rename
from os.path import exists
from setup.params import FILE_DEVICEKEY, FILE_DEVICEXML, FILE_ACTIVATIONXML
from decrypt.params import KEYPATH
from setup.data import createDefaultFiles
2023-02-26 09:00:51 -05:00
from setup.ia import SESSION_FILE, manage_login, get_book, return_book
2023-02-23 13:42:15 -05:00
2023-02-26 09:00:51 -05:00
def loginADE(email, password):
if email is None or password is None:
print("Email or Password cannot be empty")
print()
return
if not exists('account'): mkdir('account')
loginAndGetKey(email, password)
print()
def loginIA(email,password):
if email is None or password is None:
print("Email or Password cannot be empty")
print()
return
manage_login(email,password)
print()
def main(acsmFile, outputFilename):
if not exists('account'): mkdir('account')
2023-02-23 13:42:15 -05:00
# setting up the account and keys
if not (exists(FILE_ACTIVATIONXML) and exists(FILE_DEVICEXML) and exists(FILE_DEVICEKEY) and exists(KEYPATH)):
createDefaultFiles()
2023-02-24 05:46:01 -05:00
print()
2023-02-23 13:42:15 -05:00
# cheek for file existance
if not exists(acsmFile):
print(f"{acsmFile} file does not exist")
print()
2023-02-26 09:00:51 -05:00
return
2023-02-23 13:42:15 -05:00
# download
encryptedFile = downloadFile(acsmFile)
print(encryptedFile)
print()
# decrypt
2023-04-09 06:58:59 -04:00
if encryptedFile is None:
print("Failed to Download, try decrypting from ACSM file")
return
2023-02-23 13:42:15 -05:00
if encryptedFile.endswith(".pdf"):
decryptedFile = decryptPDF(encryptedFile)
elif encryptedFile.endswith(".epub"):
decryptedFile = decryptEPUB(encryptedFile)
2023-02-23 02:41:48 -05:00
else:
2023-02-23 13:42:15 -05:00
print("File format not supported")
2023-02-23 02:41:48 -05:00
print()
2023-02-26 09:00:51 -05:00
return
2023-02-23 02:41:48 -05:00
2023-02-23 13:42:15 -05:00
remove(encryptedFile)
2023-02-24 05:46:01 -05:00
if outputFilename is None:
tempName = encryptedFile
else:
tempName = outputFilename
rename(decryptedFile, tempName)
print(tempName)
2023-02-23 02:41:48 -05:00
print()
2023-02-23 13:42:15 -05:00
2023-02-26 09:00:51 -05:00
def handle_IA(url,format):
if not exists(SESSION_FILE):
print("Login to InternetArchive first or give ACSm file as input")
return
acsmFile = get_book(url,format)
if acsmFile is None:
print("Could not get Book, try using ACSm file as input")
return
main(acsmFile,None)
remove(acsmFile)
if(return_book(url) is None):
print("Please return it yourself")
2023-02-23 13:42:15 -05:00
if __name__ == "__main__":
2023-02-26 09:00:51 -05:00
parser = argparse.ArgumentParser(description="Download and Decrypt an encrypted PDF or EPUB file.")
parser.add_argument("-f", type=str, nargs='?', default=None, help="path to the ACSM file")
parser.add_argument("-u", type=str, nargs='?', default=None, help="book url from InternetArchive")
parser.add_argument("-t", type=str, nargs='?', default='pdf', help="book file type/format/extension for book url (defaults to PDF)")
parser.add_argument("-o", type=str, nargs='?', default=None, help="output file name")
parser.add_argument("-la", action="store_true", help="login to your ADE account.")
parser.add_argument("-li", action="store_true", help="login to your InternetArchive.")
parser.add_argument("-e", type=str, nargs='?', default=None, help="email/username")
parser.add_argument("-p", type=str, nargs='?', default=None, help="password")
parser.add_argument("-lo", action="store_true", help="logout from all")
2023-02-23 13:42:15 -05:00
args = parser.parse_args()
2023-02-24 05:46:01 -05:00
2023-02-26 09:00:51 -05:00
# Logout
if args.lo:
from shutil import rmtree
rmtree("account")
mkdir('account')
print()
print("Logout Sucessfull")
print()
# ADE login
elif args.la:
print()
print("chose login for ADE")
loginADE(args.e, args.p)
# IA login
elif args.li:
print()
print("chose login for InternetArchive")
loginIA(args.e, args.p)
# Book url
elif args.u:
print()
if not args.t in ['pdf','epub']:
print("only PDF and EPUB are supported")
else:
handle_IA(args.u, args.t)
print()
2023-02-24 05:46:01 -05:00
# check for default value
2023-02-26 09:00:51 -05:00
elif args.f == None:
2023-02-24 05:46:01 -05:00
if exists("URLLink.acsm"):
2023-02-26 09:00:51 -05:00
args.f = "URLLink.acsm"
2023-02-26 12:22:36 -05:00
main(args.f, args.o)
2023-02-26 09:00:51 -05:00
else: parser.print_help()
2023-02-24 05:46:01 -05:00
2023-02-26 09:00:51 -05:00
else:
2023-02-26 12:22:36 -05:00
main(args.f, args.o)