Skip to content

Commit

Permalink
Update to make it more genereic
Browse files Browse the repository at this point in the history
  • Loading branch information
fireattack committed Jun 15, 2022
1 parent 21fb592 commit 949af48
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,40 @@

1. Clone the repo.
1. Install calibre and edit `config.json`'s calibre key to your `calibre-debug.exe` path
1. (Optional) install WinRAR and edit `config.json`'s rar key to your `Rar.exe` path
1. (Optional) create some post-prosssing command and put into `config.json`'s `postprocessing` key. Remember to add quotes and escape special characters if needed! You can check the sample config.json to get an idea.
1. Clone https://github.com/apprenticeharper/DeDRM_tools repo and place "DeDRM_plugin" folder and its content directly into this repo's root folder.
1. Install pycryptodome (`pip install pycryptodome`)

## Usage
## Usage

```
usage: extract_kindle.py [-h] [-k] [-z] [-o OUTPUT] dir
usage: extract_kindle.py [-h] [-k] [-p] [-o OUTPUT] dir
positional arguments:
dir choose the folder contains DRMed azw file(s)
optional arguments:
-h, --help show this help message and exit
-k, --keep keep temp and useless files
-z, --compress also compress the files
-p, --postprocessing do some postprocessing with the file.
-o OUTPUT, --output OUTPUT
output folder (default: same as dir)
```

Examples:

```
extract_kindle.py "G:\_temp\My Kindle Content\B00KYFFDV2_EBOK" -o "D:\output" -z
extract_kindle.py "G:\_temp\My Kindle Content\B00KYFFDV2_EBOK" -o "D:\output" -p
extract_kindle.py "G:\_temp\My Kindle Content\B00KYFFDV2_EBOK"
```

You can also use it as a module by something like
You can also use it as a module by something like
```py
extract_kindle.main(R"G:\_temp\My Kindle Content\B00KYFFDV2_EBOK", "-o", "D:\\output", "-z")
```
extract_kindle.main(R"G:\_temp\My Kindle Content\B00KYFFDV2_EBOK", "-o", "D:\\output", "-p")
```

## Post-processing format

* $o: output root path (i.e. -o parameter)
* $f: book name string (path-safe, extracted from Kindle info)
* $p: full path of the folder contains the image files (basically, $o/$f)
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"calibre": "calibre-debug.exe",
"rar": "C:\\Program Files\\WinRAR\\Rar.exe"
"postprocessing": "\"C:\\Program Files\\WinRAR\\Rar.exe\" a -r -ep1 -rr5p -m0 -ma4 \"$o\\$f.rar\" \"$p\\*\""
}
23 changes: 12 additions & 11 deletions extract_kindle.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ def main(*input_args):
parser = argparse.ArgumentParser()
parser.add_argument("dir", help="choose the folder contains DRMed azw file(s)")
parser.add_argument("-k", "--keep", action='store_true', help="keep temp and useless files")
parser.add_argument("-z", "--compress", action='store_true', help="also compress the files")
parser.add_argument("-p", "--postprocessing", action='store_true', help="do some postprocessing with the file.")
parser.add_argument("-o", "--output", help="output folder (default: same as dir)")
args = parser.parse_args(input_args)

# load config
config = dict(calibre='calibre-debug.exe', rar=R'C:\Program Files (x86)\WinRAR\Rar.exe') # default
config = dict(calibre='calibre-debug.exe') # default
try:
config_file = Path(__file__).parent / 'config.json'
if config_file.exists():
Expand All @@ -42,11 +42,6 @@ def main(*input_args):
print('No calibre (calibre-debug.exe) found!')
return 1

has_rar = True
if not which(config['rar']):
print('No WinRAR (Rar.exe) found. --compress function disabled.')
has_rar = False

p = Path(args.dir)
if not p.exists():
print('Please provide a path to work on!')
Expand Down Expand Up @@ -87,7 +82,7 @@ def main(*input_args):
imgs = [f for f in (temp_folder / 'images').iterdir() if f.suffix.lower() in ['.jpeg', '.jpg']]
# The last image which is always a cover thumbnail. Here just some quick check to make sure. Then remove.
assert imgs[-1].stat().st_size <= 50*1024
assert int(imgs[-1].stem) == int(imgs[-2].stem) + 2
# assert int(imgs[-1].stem) == int(imgs[-2].stem) + 2 # well, sometimes it has some weird GIFs inbetween.. so this isn't reliable.
print(f'Removing {imgs[-1]}..')
imgs[-1].unlink()
if deDRM:
Expand Down Expand Up @@ -131,9 +126,15 @@ def main(*input_args):
# Easier to recognize which ebook is which after moving/removing the extracted files.
(p / (title + '.txt')).open('a').close()

if args.compress and has_rar:
zip_name = new_folder.with_name(new_folder.name + '.rar')
run([config['rar'], 'a', '-r', '-ep1', '-rr5p', '-m0', zip_name, str(new_folder) + "\\*"])
if args.postprocessing:
cmd = config['postprocessing']
cmd = cmd.replace('$o', str(save_dir))
cmd = cmd.replace('$p', str(new_folder))
cmd = cmd.replace('$f', new_folder.name)
run(cmd)
# if
# zip_name = new_folder.with_name(new_folder.name + '.rar')
# run([config['rar'], 'a', '-r', '-ep1', '-rr5p', '-m0', '-ma4', zip_name, str(new_folder) + "\\*"])

if not args.keep:
rmtree(temp_folder)
Expand Down

0 comments on commit 949af48

Please sign in to comment.