2021-09-12 03:25:19 +00:00
|
|
|
from xdg import xdg_config_home
|
2021-12-04 21:58:14 +00:00
|
|
|
import click, sys, shutil, subprocess, magic
|
2021-09-12 22:46:15 +00:00
|
|
|
from utils import run
|
2021-09-12 03:25:19 +00:00
|
|
|
|
|
|
|
def handle_acsm(acsm_path):
|
|
|
|
drm_path = acsm_path.with_suffix('.drm')
|
2021-09-12 22:46:15 +00:00
|
|
|
adobe_dir = xdg_config_home().joinpath('knock', 'acsm')
|
|
|
|
adobe_dir.mkdir(parents=True, exist_ok=True)
|
2021-09-12 03:25:19 +00:00
|
|
|
|
|
|
|
if drm_path.exists():
|
|
|
|
click.echo(f"Error: {drm_path} must be moved out of the way or deleted.", err=True)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if (
|
2021-09-12 22:46:15 +00:00
|
|
|
not adobe_dir.joinpath('device.xml').exists()
|
|
|
|
or not adobe_dir.joinpath('activation.xml').exists()
|
|
|
|
or not adobe_dir.joinpath('devicesalt').exists()
|
2021-09-12 03:25:19 +00:00
|
|
|
):
|
|
|
|
shutil.rmtree(str(adobe_dir))
|
|
|
|
click.echo('This device is not registered with Adobe.')
|
|
|
|
email = click.prompt("Enter your Adobe account's email address")
|
|
|
|
password = click.prompt("Enter your Adobe account's password", hide_input=True)
|
|
|
|
click.echo('Registering this device with Adobe...')
|
|
|
|
|
|
|
|
run(
|
|
|
|
[
|
2021-12-04 21:58:14 +00:00
|
|
|
'adept_activate',
|
2021-09-12 03:25:19 +00:00
|
|
|
'-u', email,
|
|
|
|
'-O', str(adobe_dir)
|
|
|
|
],
|
|
|
|
stdin=password+'\n',
|
|
|
|
cleanser=lambda:shutil.rmtree(str(adobe_dir))
|
|
|
|
)
|
|
|
|
|
2021-09-12 22:46:15 +00:00
|
|
|
click.echo('Downloading the book from Adobe...')
|
2021-09-12 03:25:19 +00:00
|
|
|
|
|
|
|
run([
|
2021-12-04 21:58:14 +00:00
|
|
|
'acsmdownloader',
|
2021-09-12 03:25:19 +00:00
|
|
|
'-d', str(adobe_dir.joinpath('device.xml')),
|
|
|
|
'-a', str(adobe_dir.joinpath('activation.xml')),
|
|
|
|
'-k', str(adobe_dir.joinpath('devicesalt')),
|
|
|
|
'-o', str(drm_path),
|
|
|
|
'-f', str(acsm_path)
|
|
|
|
])
|
|
|
|
|
2021-09-12 22:46:15 +00:00
|
|
|
drm_path_type = magic.from_file(str(drm_path), mime=True)
|
|
|
|
if drm_path_type == 'application/epub+zip':
|
2021-12-04 21:58:14 +00:00
|
|
|
file_type = 'epub'
|
2021-09-12 22:46:15 +00:00
|
|
|
elif drm_path_type == 'application/pdf':
|
2021-12-04 21:58:14 +00:00
|
|
|
file_type = 'pdf'
|
2021-09-12 03:25:19 +00:00
|
|
|
else:
|
2021-12-04 21:58:14 +00:00
|
|
|
click.echo(f'Error: Received file of media type {drm_path_type} from Adobe\' servers.', err=True)
|
2021-09-12 03:25:19 +00:00
|
|
|
click.echo('Only the following ACSM conversions are currently supported:', err=True)
|
|
|
|
click.echo(' * ACSM -> EPUB', err=True)
|
|
|
|
click.echo(' * ACSM -> PDF', err=True)
|
|
|
|
click.echo('Please open a feature request at:', err=True)
|
2021-09-12 22:46:15 +00:00
|
|
|
click.echo(f' https://github.com/BentonEdmondson/knock/issues/new?title=Support%20{drm_path_type}%20Files&labels=enhancement', err=True)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-12-04 21:58:14 +00:00
|
|
|
output_path = acsm_path.with_suffix('.' + file_type)
|
|
|
|
if output_path.exists():
|
|
|
|
drm_path.unlink()
|
|
|
|
click.echo(f"Error: {output_path} must be moved out of the way or deleted.", err=True)
|
2021-09-12 03:25:19 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
click.echo('Decrypting the file...')
|
|
|
|
|
|
|
|
run([
|
2021-12-04 21:58:14 +00:00
|
|
|
'rmdrm-' + file_type,
|
2021-09-12 22:46:15 +00:00
|
|
|
str(adobe_dir.joinpath('activation.xml')),
|
|
|
|
str(drm_path),
|
2021-12-04 21:58:14 +00:00
|
|
|
str(output_path)
|
2021-09-12 03:25:19 +00:00
|
|
|
])
|
|
|
|
|
2021-09-12 22:46:15 +00:00
|
|
|
drm_path.unlink()
|
2021-09-12 03:25:19 +00:00
|
|
|
|
2021-12-04 21:58:14 +00:00
|
|
|
click.secho(f'DRM-free {file_type.upper()} file created:\n{output_path}', fg='green')
|