Example #1
0
    def make_unrestricted_file(filename):
        """
        Try to remove Digital Rights Management (DRM) type
        encryption from the EPUB document.

        Legal note:
        The removal of the encryption from the file does not involve any
        violation of any laws whatsoever, as long as the user has obtained
        the eBook using legitimate means, and the user intents to use the
        content in a manor that does not violate the law.
        """
        filepath = Path(filename)
        content_hash = md5()
        with open(filepath, "rb") as ebook_file:
            for chunk in ebook_file:
                content_hash.update(chunk)
        identifier = content_hash.hexdigest()
        processed_book = home_data_path(f"{identifier}.epub")
        if processed_book.exists():
            return str(processed_book)
        _temp = TemporaryDirectory()
        temp_path = Path(_temp.name)
        ZipFile(filename).extractall(temp_path)
        with ZipFile(processed_book, "w") as book:
            for file in recursively_iterdir(temp_path):
                if "encryption.xml" in file.name.lower():
                    continue
                book.write(file, file.relative_to(temp_path))
        _temp.cleanup()
        return str(processed_book)
Example #2
0
 def get_converted_filename(self):
     storage_area = home_data_path("odf_as_html")
     storage_area.mkdir(parents=True, exist_ok=True)
     target_file = storage_area / f"{generate_file_md5(self.odf_filename)}.html"
     if not target_file.exists():
         target_file.write_text(self.as_html, encoding="utf-8")
     return target_file
Example #3
0
 def get_converted_filename(cls, filename):
     storage_area = home_data_path("docx_as_html")
     storage_area.mkdir(parents=True, exist_ok=True)
     target_file = storage_area / f"{generate_file_md5(filename)}.html"
     if not target_file.exists():
         with open(filename, "rb") as docx:
             result = mammoth.convert_to_html(
                 docx, include_embedded_style_map=False)
             html_string = cls.make_proper_html(result.value, filename)
             target_file.write_text(html_string, encoding="utf-8")
     return target_file
Example #4
0
 def read(self):
     self.filename = self.get_file_system_path()
     rendered_md_path = home_data_path("rendered_markdown")
     rendered_md_path.mkdir(parents=True, exist_ok=True)
     filehash = generate_file_md5(self.filename)
     target_file = rendered_md_path / f"{filehash}.html"
     html_content = markdown(self.filename.read_text(), escape=False)
     target_file.write_text(html_content)
     raise ChangeDocument(
         old_uri=self.uri,
         new_uri=DocumentUri.from_filename(target_file),
         reason="Unpacked the mobi file to epub",
     )
Example #5
0
def make_unrestricted_file(filename):
    """Try to remove digital restrictions from the file if found."""
    hashed_filename = md5(filename.lower().encode("utf8")).hexdigest()
    processed_book = home_data_path(hashed_filename)
    if processed_book.exists():
        return str(processed_book)
    _temp = TemporaryDirectory()
    temp_path = Path(_temp.name)
    ZipFile(filename).extractall(temp_path)
    (temp_path / "META-INF\\encryption.xml").unlink()
    with ZipFile(processed_book, "w") as book:
        for file in recursively_iterdir(temp_path):
            book.write(file, file.relative_to(temp_path))
    _temp.cleanup()
    return str(processed_book)
Example #6
0
# coding: utf-8
"""
Database models for `Bookworm`.
"""

import sqlite3
import os
import db_magic as db
from bookworm import config
from bookworm.paths import home_data_path, db_path as get_db_path
from bookworm.logger import logger
from .models import *

log = logger.getChild(__name__)

FILE_HISTORY_DB_PATH = home_data_path("file_history.db")


def init_database():
    db_path = os.path.join(get_db_path(), "db.sqlite")
    if not os.path.isfile(FILE_HISTORY_DB_PATH):
        create_file_history_db()
    return db.Model.setup_database(f"sqlite:///{db_path}", create=True)


def create_file_history_db():
    sql = """
        CREATE TABLE file_history
        (id INTEGER PRIMARY KEY, file_path TEXT, last_page INTEGER, last_pos INTEGER)
    """
    con = sqlite3.connect(FILE_HISTORY_DB_PATH)
Example #7
0
 def get_mobi_storage_area(cls):
     storage_area = home_data_path("unpacked_mobi")
     if not storage_area.exists():
         storage_area.mkdir(parents=True, exist_ok=True)
     return storage_area
Example #8
0
 def _get_cache_directory(self):
     return str(home_data_path(".parsed_epub_cache"))