예제 #1
0
파일: cleanup.py 프로젝트: cdadar/org-bib
    def decrypt(self, book):
        """Decrypts the book and returns the name of the output file.
        """
        if self.outdir and not os.path.exists(self.outdir):
            os.makedirs(self.outdir)
        drmdir = os.path.join(self.outdir, 'drm')
        os.mkdir(drmdir)
        cleanbook = None
        mobi = None
        try:
            temp = sys.stdout
            sys.stdout = codecs.open(os.devnull, 'w', encoding='utf-8')
            if self.alfdir:
                sys.path.append(self.alfdir)
            from k4mobidedrm import decryptBook
            decryptBook(os.path.expanduser(book), drmdir,
                        [], [self.serial], [])
            sys.stdout = temp
            cleanbook = os.listdir(drmdir)[0]
            mobi = os.path.join(self.outdir,
                                dashify.dash_name(cleanbook.replace('_nodrm', '')))

            os.rename(os.path.join(drmdir, cleanbook), mobi)
        finally:
            os.rmdir(drmdir)

        return mobi
예제 #2
0
def decryptk4mobi(infile, outdir, rscpath):
    rv = 1
    pidnums = []
    pidspath = os.path.join(rscpath, 'pidlist.txt')
    if os.path.exists(pidspath):
        pidstr = file(pidspath, 'r').read()
        pidstr = pidstr.rstrip(os.linesep)
        pidstr = pidstr.strip()
        if pidstr != '':
            pidnums = pidstr.split(',')
    serialnums = []
    serialnumspath = os.path.join(rscpath, 'seriallist.txt')
    if os.path.exists(serialnumspath):
        serialstr = file(serialnumspath, 'r').read()
        serialstr = serialstr.rstrip(os.linesep)
        serialstr = serialstr.strip()
        if serialstr != '':
            serialnums = serialstr.split(',')
    kDatabaseFiles = []
    files = os.listdir(rscpath)
    filefilter = re.compile("\.k4i$", re.IGNORECASE)
    files = filter(filefilter.search, files)
    if files:
        for filename in files:
            dpath = os.path.join(rscpath, filename)
            kDatabaseFiles.append(dpath)
    try:
        rv = k4mobidedrm.decryptBook(infile, outdir, kDatabaseFiles,
                                     serialnums, pidnums)
    except Exception, e:
        errlog += traceback.format_exc()
        errlog += str(e)
        rv = 1
예제 #3
0
def decryptk4mobi(infile, outdir, rscpath):
    rv = 1
    pidnums = []
    pidspath = os.path.join(rscpath,'pidlist.txt')
    if os.path.exists(pidspath):
        pidstr = file(pidspath,'r').read()
        pidstr = pidstr.rstrip(os.linesep)
        pidstr = pidstr.strip()
        if pidstr != '':
            pidnums = pidstr.split(',')
    serialnums = []
    serialnumspath = os.path.join(rscpath,'seriallist.txt')
    if os.path.exists(serialnumspath):
        serialstr = file(serialnumspath,'r').read()
        serialstr = serialstr.rstrip(os.linesep)
        serialstr = serialstr.strip()
        if serialstr != '':
            serialnums = serialstr.split(',')
    kDatabaseFiles = []
    files = os.listdir(rscpath)
    filefilter = re.compile("\.k4i$", re.IGNORECASE)
    files = filter(filefilter.search, files)
    if files:
        for filename in files:
            dpath = os.path.join(rscpath,filename)
            kDatabaseFiles.append(dpath)
    try:
        rv = k4mobidedrm.decryptBook(infile, outdir, kDatabaseFiles, serialnums, pidnums)
    except Exception, e:
        errlog += traceback.format_exc()
        errlog += str(e)
        rv = 1
예제 #4
0
파일: dedrm.py 프로젝트: mfa/free-kindle
def freeBooks(kindle, storage):
  print "Free BOOKS!"
  serial = raw_input("Kindle Serial Number: ") #B02415022487114Q
  kindle_path = kindle.mount_paths[0]
  storage_path = storage.mount_paths[0]
  path_to_freedom = storage_path+"/freed_books"
  books = os.listdir (kindle_path+"/documents")
  try:
    os.mkdir(path_to_freedom)
  except OSError:
    pass
  for book in books:
    if book.endswith(".azw3") or book.endswith(".mobi"):
      print book
      decryptBook(kindle_path+"/documents/"+book, path_to_freedom, [], [serial], [])
      # fetch DrmException
  storage.unmount()
예제 #5
0
import mobidedrm
import k4mobidedrm
from argv_utils import add_cp65001_codec, set_utf8_default_encoding, unicode_argv
add_cp65001_codec()
set_utf8_default_encoding()

infile = sys.argv[1]
if not os.path.isfile(infile):
    print 'Input file not exist!'
    exit()
outdir = os.path.dirname(infile)

kindlekeyfile = os.path.dirname(os.path.realpath(
    sys.argv[0])) + '\kindlekey.k4i'

if not os.path.exists(kindlekeyfile):
    import kindlekey
    try:
        kindlekey.getkey(kindlekeyfile)
    except:
        print 'Get Key Failed'
        exit()

pidnums = []
serialnums = []

kDatabaseFiles = [kindlekeyfile]

rv = k4mobidedrm.decryptBook(infile, outdir, kDatabaseFiles, [], serialnums,
                             pidnums)
예제 #6
0
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("-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
    try:
        config_file = Path(__file__).parent / 'config.json'
        if config_file.exists():
            with config_file.open('r', encoding='utf-8') as f:
                config = json.load(f)
            print('Loaded config from config.json')
    except:
        pass

    if not which(config['calibre']):
        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!')
        return 1

    print(f'Folder: {p}')

    azw_files = [
        f for f in p.iterdir() if (f.suffix.lower() in ['.azw', '.azw3'])
    ]
    azw_files_DeDrmed = [
        f for f in p.iterdir() if f.name.lower().endswith('_nodrm.azw3')
    ]
    deDRM = False
    if len(azw_files) == 0:
        print('No .azw file found!')
        return 1
    elif len(azw_files_DeDrmed) == 1:
        deDRMed_azw_file = azw_files_DeDrmed[0]
    elif len(azw_files) == 1:
        print(f'DeDRMing {azw_files[0].name}..')
        if not KEY.exists():
            getkey(str(Path(__file__).parent))
        if not KEY.exists():
            print('Failed to get key!')
            return 1

        decryptBook(azw_files[0], str(p), [str(KEY)], [], [], [])

        deDRMed_azw_file = next(f for f in p.iterdir()
                                if f.name.endswith('_nodrm.azw3'))
        deDRM = True  # this flags that the deDRMed file is created by ourselves (wasn't there before). So we remove it later.

    print(f'Extracting from {deDRMed_azw_file.name}..')
    temp_folder = p / 'temp'
    run([config['calibre'], '-x', deDRMed_azw_file, temp_folder])
    if not args.keep:
        # Clean up `images` folder
        for f in (temp_folder / 'images').iterdir():
            if f.suffix.lower() in ['.unknown']:
                print(f'Removing {f}..')
                f.unlink()
        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
        print(f'Removing {imgs[-1]}..')
        imgs[-1].unlink()
        if deDRM:
            deDRMed_azw_file.unlink()  # Remove deDRMed file.

    res_files = [f for f in p.iterdir() if (f.suffix.lower() in ['.res'])]
    if len(res_files) == 1:

        print(f'Processing resource file {res_files[0]}..')
        DumpAZW6_py3.main(['DumpAZW6_py3.py', str(res_files[0])])

        hd_images = [
            f for f in (p / 'azw6_images').iterdir()
            if f.suffix.lower() in ['.jpeg', '.jpg']
        ]
        for img in hd_images:
            lowq_img = temp_folder / 'images' / img.name.replace('HDimage', '')
            if lowq_img.exists():
                print(f'Replacing {lowq_img.name} with {img.name}..')
                if not args.keep:
                    lowq_img.unlink()
                img.rename(lowq_img.with_suffix('.hd.jpeg'))
    else:
        print('No or more than one .res file found. Not processed.')

    # Find the title of the ebook from the metadata file.
    metadata = (temp_folder / 'metadata.opf').read_text(encoding='utf8')
    title = re.search(r'<dc:title>(.+?)</dc:title>', metadata,
                      flags=re.DOTALL)[1].strip()

    for c in '<>:"\/|?*\r\n\t':  # Windows-safe filename
        title = title.replace(c, '_')

    if args.output:
        save_dir = Path(args.output)
        save_dir.mkdir(parents=True, exist_ok=True)
    else:
        save_dir = p

    print(f'Moving files to {save_dir}..')
    new_folder = save_dir / title
    move((temp_folder / 'images'), new_folder)

    # Create an empty file to keep track of the the filename.
    # 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 not args.keep:
        rmtree(temp_folder)
        if (p / 'azw6_images').exists():
            rmtree(p / 'azw6_images')

    print('All done!')