From 7485a8e823a2e82f4e545a17173aa971281a6c5d Mon Sep 17 00:00:00 2001 From: benton Date: Sun, 18 Jul 2021 19:29:12 -0500 Subject: [PATCH] the script is working correctly, still need to add logins --- .gitignore | 1 + flake.lock | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 26 +++++++++++++++ knock | 45 +++++++++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 .gitignore create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 knock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e2f5dd2 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +result \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2d09941 --- /dev/null +++ b/flake.lock @@ -0,0 +1,97 @@ +{ + "nodes": { + "inept-epub": { + "inputs": { + "nixpkgs": "nixpkgs" + }, + "locked": { + "lastModified": 1626630204, + "narHash": "sha256-R56r2p7z1bNJS0ng+KeuV4zbWT4T46zc6YDqySB7jQc=", + "owner": "BentonEdmondson", + "repo": "inept-epub", + "rev": "45306cadf7fd7a04453a60b8c1e39bc06b0035ec", + "type": "github" + }, + "original": { + "owner": "BentonEdmondson", + "repo": "inept-epub", + "type": "github" + } + }, + "libgourou-utils": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1626630231, + "narHash": "sha256-nuhlaSwdTVZl3mHA3Pw8pdbr8bxBmRuNAEwNh/IU/NE=", + "owner": "BentonEdmondson", + "repo": "libgourou-utils", + "rev": "07637d6aab51fc1e188576fefcf9a7b6d77f6fc6", + "type": "github" + }, + "original": { + "owner": "BentonEdmondson", + "repo": "libgourou-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1626464457, + "narHash": "sha256-u2PCh/+8vQSLwf0mPpKHKQ8hAPB3l4uNZR3r0TdK2Lg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "c6c4a3d45ab200f17805d2d86a1ff1cc7ca2b186", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1626464457, + "narHash": "sha256-u2PCh/+8vQSLwf0mPpKHKQ8hAPB3l4uNZR3r0TdK2Lg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "c6c4a3d45ab200f17805d2d86a1ff1cc7ca2b186", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1626464457, + "narHash": "sha256-u2PCh/+8vQSLwf0mPpKHKQ8hAPB3l4uNZR3r0TdK2Lg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "c6c4a3d45ab200f17805d2d86a1ff1cc7ca2b186", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "inept-epub": "inept-epub", + "libgourou-utils": "libgourou-utils", + "nixpkgs": "nixpkgs_3" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..81838c2 --- /dev/null +++ b/flake.nix @@ -0,0 +1,26 @@ +{ + inputs.nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable; + inputs.libgourou-utils.url = github:BentonEdmondson/libgourou-utils; + inputs.inept-epub.url = github:BentonEdmondson/inept-epub; + + outputs = { self, ... }@flakes: let + nixpkgs = flakes.nixpkgs.legacyPackages.x86_64-linux; + libgourou-utils = flakes.libgourou-utils.defaultPackage.x86_64-linux; + inept-epub = flakes.inept-epub.defaultPackage.x86_64-linux; + in { + defaultPackage.x86_64-linux = nixpkgs.stdenv.mkDerivation { + pname = "knock"; + version = "0.0.0"; + src = self; + nativeBuildInputs = [ nixpkgs.makeWrapper ]; + buildInputs = [ nixpkgs.python3 libgourou-utils inept-epub ]; + installPhase = '' + mkdir -p $out/bin + chmod +x knock + cp knock $out/bin + wrapProgram $out/bin/knock --prefix PATH : ${nixpkgs.lib.makeBinPath [libgourou-utils inept-epub]} + ''; + }; + + }; +} \ No newline at end of file diff --git a/knock b/knock new file mode 100644 index 0000000..e027248 --- /dev/null +++ b/knock @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +import os +import argparse +from pathlib import Path +import subprocess + +parser = argparse.ArgumentParser(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') + +if not args.adobe_dir.exists(): + raise Exception('This device is not registered') + +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() + +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)}.') \ No newline at end of file