View Single Post
Old 01-10-2024, 05:56 AM   #1222
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,212
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by nqk View Post
I have a feeling that it's almost there. Because it has now come to the print command.

Code:
NameError: name 'prints' is not defined
I'm afraid not. That part of the code only runs if the command fails for some reason. Anyway, here we go:

Code:
import subprocess
import os
from calibre.constants import iswindows, isosx
import sys

def run(gui, settings, chain):
    fmt = 'TXTZ'
    db = gui.current_db
    output_dir = 'D:\Temp'
    path_to_binary = 'C:\Program Files\7-Zip\7z.exe'
    clean_env = dict(os.environ)
    kw = {'env': clean_env, 'shell':True, 'capture_output': True}
    if iswindows:
        del clean_env['PATH']
    elif isosx:
        if path_to_binary.lower().endswith(".app"):
            path_to_binary = 'open -a ' + path_to_binary
    else: #Linux
        clean_env['LD_LIBRARY_PATH'] = ''
    for book_id in chain.scope().get_book_ids():
        path_to_format = db.format_abspath(book_id, fmt, index_is_id=True)
        title = db.title(book_id, index_is_id=True)
        if not path_to_format:
            print(f'Book {title} does not have format: {fmt}')
            continue
        cmd = f'"{path_to_binary}" e {path_to_format} -o"{output_dir}" file-to-extract -y'
        result = subprocess.run(cmd, **kw)
        if result.returncode != 0:
            print(f'Command error: Return code is {result.returncode} when running command for file: {path_to_format}\ncmd: {cmd}')
            sys.stdout.buffer.write(b'stderr: ' + result.stderr)
            print()
capink is online now   Reply With Quote