better build process, better output, libgourou updated, better file dir structure
This commit is contained in:
parent
cf007616b7
commit
094fac4d9f
6 changed files with 169 additions and 114 deletions
72
lib/handle_acsm.py
Normal file
72
lib/handle_acsm.py
Normal file
|
@ -0,0 +1,72 @@
|
|||
from xdg import xdg_config_home
|
||||
import click, sys, shutil, subprocess
|
||||
from run import run
|
||||
|
||||
def handle_acsm(acsm_path):
|
||||
drm_path = acsm_path.with_suffix('.drm')
|
||||
adobe_dir = xdg_config_home() / 'knock' / 'acsm'
|
||||
|
||||
if drm_path.exists():
|
||||
click.echo(f"Error: {drm_path} must be moved out of the way or deleted.", err=True)
|
||||
sys.exit(1)
|
||||
|
||||
adobe_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
if (
|
||||
not (adobe_dir / 'device.xml').exists()
|
||||
or not (adobe_dir / 'activation.xml').exists()
|
||||
or not (adobe_dir / 'devicesalt').exists()
|
||||
):
|
||||
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(
|
||||
[
|
||||
'adept-register',
|
||||
'-u', email,
|
||||
'-O', str(adobe_dir)
|
||||
],
|
||||
stdin=password+'\n',
|
||||
cleanser=lambda:shutil.rmtree(str(adobe_dir))
|
||||
)
|
||||
|
||||
click.echo('Downloading the EPUB file from Adobe...')
|
||||
|
||||
run([
|
||||
'adept-download',
|
||||
'-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)
|
||||
])
|
||||
|
||||
drm_file_type = magic.from_file(str(args.drm_file), mime=True)
|
||||
if drm_file_type == 'application/epub+zip':
|
||||
decryption_command = 'inept-epub'
|
||||
elif drm_file_type == 'application/pdf':
|
||||
decryption_command = 'inept-pdf'
|
||||
else:
|
||||
click.echo(f'Error: Received file of media type {drm_file_type}.', err=True)
|
||||
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)
|
||||
click.echo(f' https://github.com/BentonEdmondson/knock/issues/new?title=Support%20{drm_file_type}%20Files&labels=enhancement', err=True)
|
||||
sys.exit(1)
|
||||
|
||||
click.echo('Decrypting the file...')
|
||||
|
||||
run([
|
||||
decryption_command,
|
||||
str(args.adobe_dir.joinpath('activation.xml')),
|
||||
str(args.drm_file),
|
||||
str(args.epub_file)
|
||||
])
|
||||
|
||||
args.drm_file.unlink()
|
||||
|
||||
click.secho(f'DRM-free EPUB file created:\n{str(args.epub_file)}', color='green')
|
33
lib/run.py
Normal file
33
lib/run.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
import click, subprocess, sys
|
||||
|
||||
# run a command and display output in a styled terminal
|
||||
# cleanser is called if the command returns a >0 exit code
|
||||
def run(command: [str], stdin: str = '', cleanser = lambda: None) -> int:
|
||||
|
||||
# newline and set styles
|
||||
click.secho('', fg='white', bg='black', bold=True, reset=False)
|
||||
|
||||
# show command
|
||||
click.echo('knock> ' + ' '.join(command))
|
||||
|
||||
# remove bold
|
||||
click.secho('', fg='white', bg='black', bold=False, reset=False)
|
||||
result = subprocess.run(
|
||||
command,
|
||||
stderr=subprocess.STDOUT,
|
||||
input=stdin.encode(),
|
||||
check=False # don't throw Python error if returncode isn't 0
|
||||
)
|
||||
|
||||
# show returncode in bold, then reset styles
|
||||
click.secho(f'\nknock[{result.returncode}]>', bold=True)
|
||||
|
||||
# newline
|
||||
click.echo('')
|
||||
|
||||
if result.returncode > 0:
|
||||
cleanser()
|
||||
click.echo(f'Error: Command returned error code {result.returncode}.', err=True)
|
||||
sys.exit(1)
|
||||
|
||||
return result.returncode
|
Loading…
Add table
Add a link
Reference in a new issue