Implement support for Amazon ebooks
This commit is contained in:
4
TODO
4
TODO
@@ -54,8 +54,10 @@ TODO
|
|||||||
Filetypes:
|
Filetypes:
|
||||||
✓ cbz, cbr support
|
✓ cbz, cbr support
|
||||||
✓ Keep font settings enabled but only for background color
|
✓ Keep font settings enabled but only for background color
|
||||||
|
✓ mobi, azw support
|
||||||
|
Limit the extra files produced by KindleUnpack
|
||||||
|
Have them save to memory
|
||||||
epub support
|
epub support
|
||||||
mobi, azw support
|
|
||||||
Other:
|
Other:
|
||||||
✓ Define every widget in code
|
✓ Define every widget in code
|
||||||
Bugs:
|
Bugs:
|
||||||
|
12
__main__.py
12
__main__.py
@@ -288,7 +288,8 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
|
|||||||
file_list,
|
file_list,
|
||||||
'addition',
|
'addition',
|
||||||
self.database_path,
|
self.database_path,
|
||||||
self.settings['auto_tags'])
|
self.settings['auto_tags'],
|
||||||
|
self.temp_dir.path())
|
||||||
|
|
||||||
parsed_books = books.initiate_threads()
|
parsed_books = books.initiate_threads()
|
||||||
database.DatabaseFunctions(self.database_path).add_to_database(parsed_books)
|
database.DatabaseFunctions(self.database_path).add_to_database(parsed_books)
|
||||||
@@ -704,9 +705,12 @@ class MainUI(QtWidgets.QMainWindow, mainwindow.Ui_MainWindow):
|
|||||||
|
|
||||||
file_md5 = filename[1]
|
file_md5 = filename[1]
|
||||||
if not file_md5:
|
if not file_md5:
|
||||||
with open(filename[0], 'rb') as current_book:
|
try:
|
||||||
first_bytes = current_book.read(1024 * 32) # First 32KB of the file
|
with open(filename[0], 'rb') as current_book:
|
||||||
file_md5 = hashlib.md5(first_bytes).hexdigest()
|
first_bytes = current_book.read(1024 * 32) # First 32KB of the file
|
||||||
|
file_md5 = hashlib.md5(first_bytes).hexdigest()
|
||||||
|
except FileNotFoundError:
|
||||||
|
return
|
||||||
|
|
||||||
# Remove any already open files
|
# Remove any already open files
|
||||||
# Set focus to last file in case only one is open
|
# Set focus to last file in case only one is open
|
||||||
|
@@ -18,23 +18,46 @@
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
import zipfile
|
import zipfile
|
||||||
import collections
|
import collections
|
||||||
from urllib.parse import unquote
|
from urllib.parse import unquote
|
||||||
|
|
||||||
import ebooklib.epub
|
import ebooklib.epub
|
||||||
|
import KindleUnpack.kindleunpack as KindleUnpack
|
||||||
|
|
||||||
|
|
||||||
class ParseEPUB:
|
class ParseEBook:
|
||||||
def __init__(self, filename, temp_dir, file_md5):
|
def __init__(self, filename, temp_dir, file_md5):
|
||||||
# TODO
|
# TODO
|
||||||
# Maybe also include book description
|
# Maybe also include book description
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.book = None
|
self.book = None
|
||||||
self.temp_dir = temp_dir
|
self.temp_dir = temp_dir
|
||||||
|
self.temp_dir_copy = temp_dir
|
||||||
self.file_md5 = file_md5
|
self.file_md5 = file_md5
|
||||||
|
|
||||||
|
# This is a crazy lazy thing
|
||||||
|
# But it works for now
|
||||||
|
|
||||||
|
self.use_KindleUnpack = False
|
||||||
|
kindle_extensions = ['.mobi', '.azw', '.azw3', '.azw4', '.prc']
|
||||||
|
if os.path.splitext(self.filename)[1].lower() in kindle_extensions:
|
||||||
|
self.use_KindleUnpack = True
|
||||||
|
self.temp_dir = os.path.join(
|
||||||
|
self.temp_dir, os.path.basename(self.filename))
|
||||||
|
|
||||||
def read_book(self):
|
def read_book(self):
|
||||||
|
if self.use_KindleUnpack:
|
||||||
|
with HidePrinting():
|
||||||
|
KindleUnpack.unpackBook(self.filename, self.temp_dir)
|
||||||
|
|
||||||
|
new_filename_with_ext = os.path.splitext(
|
||||||
|
os.path.basename(self.filename))[0] + '.epub'
|
||||||
|
self.filename = os.path.join(
|
||||||
|
self.temp_dir, 'mobi8', new_filename_with_ext)
|
||||||
|
self.temp_dir = self.temp_dir_copy
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.book = ebooklib.epub.read_epub(self.filename)
|
self.book = ebooklib.epub.read_epub(self.filename)
|
||||||
except (KeyError, AttributeError, FileNotFoundError):
|
except (KeyError, AttributeError, FileNotFoundError):
|
||||||
@@ -162,3 +185,12 @@ class ParseEPUB:
|
|||||||
'images_only': False}
|
'images_only': False}
|
||||||
|
|
||||||
return contents, file_settings
|
return contents, file_settings
|
||||||
|
|
||||||
|
|
||||||
|
class HidePrinting:
|
||||||
|
def __enter__(self):
|
||||||
|
self._original_stdout = sys.stdout
|
||||||
|
sys.stdout = None
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
sys.stdout = self._original_stdout
|
@@ -44,14 +44,16 @@ from PyQt5 import QtCore, QtGui
|
|||||||
|
|
||||||
import database
|
import database
|
||||||
|
|
||||||
from parsers.epub import ParseEPUB
|
from parsers.ebook import ParseEBook
|
||||||
from parsers.cbz import ParseCBZ
|
from parsers.cbz import ParseCBZ
|
||||||
from parsers.cbr import ParseCBR
|
from parsers.cbr import ParseCBR
|
||||||
|
|
||||||
sorter = {
|
sorter = {
|
||||||
'epub': ParseEPUB,
|
'epub': ParseEBook,
|
||||||
|
'mobi': ParseEBook,
|
||||||
|
'azw': ParseEBook,
|
||||||
'cbz': ParseCBZ,
|
'cbz': ParseCBZ,
|
||||||
'cbr': ParseCBR}
|
'cbr': ParseCBR,}
|
||||||
|
|
||||||
available_parsers = [i for i in sorter]
|
available_parsers = [i for i in sorter]
|
||||||
progressbar = None # This is populated by __main__
|
progressbar = None # This is populated by __main__
|
||||||
|
@@ -56,7 +56,8 @@ class BackGroundBookAddition(QtCore.QThread):
|
|||||||
self.file_list,
|
self.file_list,
|
||||||
'addition',
|
'addition',
|
||||||
self.database_path,
|
self.database_path,
|
||||||
self.parent.settings['auto_tags'])
|
self.parent.settings['auto_tags'],
|
||||||
|
self.parent.temp_dir.path())
|
||||||
parsed_books = books.initiate_threads()
|
parsed_books = books.initiate_threads()
|
||||||
self.parent.lib_ref.generate_model('addition', parsed_books, False)
|
self.parent.lib_ref.generate_model('addition', parsed_books, False)
|
||||||
if self.prune_required:
|
if self.prune_required:
|
||||||
|
Reference in New Issue
Block a user