knock/knock
2021-07-23 11:33:48 -05:00

58 lines
No EOL
1.8 KiB
Python

#!/usr/bin/env python3
import os, sys, argparse, subprocess
from pathlib import Path
from getpass import getpass
parser = argparse.ArgumentParser(prog='knock', description='Convert an ACSM file to a DRM-free EPUB file')
parser.add_argument('acsm_file', metavar='file', type=str, help='the ACSM file to convert')
args = parser.parse_args()
# these are all Path objects: https://docs.python.org/3/library/pathlib.html
args.acsm_file = Path(args.acsm_file).expanduser()
args.drm_file = args.acsm_file.with_suffix('.drm.epub')
args.epub_file = args.acsm_file.with_suffix('.epub')
args.adobe_dir = Path('~/.config/knock').expanduser()
if not args.acsm_file.exists():
raise Exception(f'{str(args.acsm_file)} does not exist.')
if args.epub_file.exists():
raise Exception(f'{str(args.epub_file)} already exists.')
if not args.adobe_dir.exists():
print('This device is not registered with Adobe.')
email = input("Enter your Adobe account's email address: ")
password = getpass("Enter your Adobe account's password: ")
print('Registering this device with Adobe...')
subprocess.check_output([
'activate',
'-u', email,
'-p', password,
'-O', str(args.adobe_dir)
])
print('Downloading the EPUB file from Adobe...')
subprocess.check_output([
'acsmdownloader',
'-d', str(args.adobe_dir.joinpath('device.xml')),
'-a', str(args.adobe_dir.joinpath('activation.xml')),
'-k', str(args.adobe_dir.joinpath('devicesalt')),
'-o', str(args.drm_file),
'-f', str(args.acsm_file)
])
print('Decrypting the file...')
subprocess.check_output([
'inept-epub',
str(args.adobe_dir.joinpath('activation.xml')),
str(args.drm_file),
str(args.epub_file)
])
args.drm_file.unlink()
print(f'\nDRM-free EPUB file created:\n{str(args.epub_file)}')