knock/knock

57 lines
1.8 KiB
Text
Raw Normal View History

#!/usr/bin/env python3
2021-07-21 23:14:42 +00:00
import os, sys, argparse, subprocess
from pathlib import Path
2021-07-21 23:47:53 +00:00
from getpass import getpass
2021-07-21 23:14:42 +00:00
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()
args.activation_file = args.adobe_dir.joinpath('activation.xml')
2021-07-21 23:14:42 +00:00
if not args.acsm_file.exists():
raise Exception(f'{str(args.acsm_file)} does not exist.')
if not args.adobe_dir.exists():
2021-07-21 23:47:53 +00:00
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 ACSM 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.activation_file),
str(args.drm_file),
str(args.epub_file)
])
args.drm_file.unlink()
2021-07-21 23:14:42 +00:00
print(f'\nThe DRM-free EPUB file now exists at {str(args.epub_file)}!')
print(f'Once you make sure the EPUB file is readable, you can delete {str(args.acsm_file)}.')