Skip to content

Commit

Permalink
Read CBR and read CBZ
Browse files Browse the repository at this point in the history
  • Loading branch information
Epifanio Suárez Martínez committed Jan 28, 2021
1 parent 22d218b commit f5af31a
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ At the moment it is not open to the contribution of other developers, since the
- [X] Read EPUB
- [X] Read PDF
- [ ] Read MOBI
- [ ] Read CBZ
- [ ] Read CBR
- [X] Read CBZ
- [X] Read CBR
- [ ] Homogenize reading with the same format, regardless of the book.
- [ ] Read only available pages
- [ ] Grouping by tags or equivalent
- [ ] Recursive traversal of the library
- [X] Create a beautiful interface
- [ ] Improve speed
- [ ] Save reading position
Expand Down
32 changes: 32 additions & 0 deletions library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from library.epub import Epub
from library.pdf import Pdf
from library.cbz import Cbz
from library.cbr import Cbr

from .models.book import Book
from slugify import slugify
Expand Down Expand Up @@ -99,6 +101,32 @@ def add_book(self, name):
);
except Exception as error:
logging.warning(f"Error when adding the new book -> {error}");
elif extension == ".cbz":
new_pdf = Cbz(name);
try:
new_book = Book(
title=new_pdf.title,
path_book=new_pdf.path_book,
path_cover=new_pdf.path_cover,
filename=new_pdf.filename,
extension=new_pdf.extension,
number_of_pages=new_pdf.number_of_pages
);
except Exception as error:
logging.warning(f"Error when adding the new book -> {error}");
elif extension == ".cbr":
new_pdf = Cbr(name);
try:
new_book = Book(
title=new_pdf.title,
path_book=new_pdf.path_book,
path_cover=new_pdf.path_cover,
filename=new_pdf.filename,
extension=new_pdf.extension,
number_of_pages=new_pdf.number_of_pages
);
except Exception as error:
logging.warning(f"Error when adding the new book -> {error}");

@orm.db_session
def get_books(self):
Expand All @@ -118,6 +146,10 @@ def get_page_book(self, id_book, page):
return Epub(book).get_page(page);
elif extension == ".pdf":
return Pdf(book).get_page(page);
elif extension == ".cbz":
return Cbz(book).get_page(page);
elif extension == ".cbr":
return Cbr(book).get_page(page);

def _remove_all_files(self, directory):
for filename in os.listdir(directory):
Expand Down
43 changes: 43 additions & 0 deletions library/cbr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import logging
import os
import zipfile

from configuration import configuration
from slugify import slugify

import library.util as util

logging.basicConfig(level=logging.INFO, filename="app.log", filemode="w");

class Cbr:
def __init__(self, path):
self.path_book = path;
self.filename = util.get_name(os.path.basename(path));
self.extension = util.get_extension(os.path.basename(path));

self.title = self.filename;
self.number_of_pages = 10;

self.get_and_save_cover();

def get_and_save_cover(self):
with zipfile.ZipFile(self.path_book, "r", compression=zipfile.ZIP_DEFLATED, allowZip64=True) as zip_file:
images = [image for image in zip_file.namelist() if "." in image];

extension = util.get_extension(os.path.basename(images[0]));
self.path_cover = f"/{configuration['PATH_COVERS']}{slugify(self.filename)}{extension}";

with open(self.path_cover[1::], "wb") as image:
image.write(zip_file.read(images[0]));

def get_page(self, page):
with zipfile.ZipFile(self.path_book, "r", compression=zipfile.ZIP_DEFLATED, allowZip64=True) as zip_file:
images = [image for image in zip_file.namelist() if "." in image];

extension = util.get_extension(os.path.basename(images[page]));
name = f"{configuration['PATH_TEMP']}{slugify(self.filename)}-{page}{extension}";

with open(name[1::], "wb") as image:
image.write(zip_file.read(images[page]));

return f"<img width='700px' src='{name}'>";
43 changes: 43 additions & 0 deletions library/cbz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import logging
import os
import zipfile

from configuration import configuration
from slugify import slugify

import library.util as util

logging.basicConfig(level=logging.INFO, filename="app.log", filemode="w");

class Cbz:
def __init__(self, path):
self.path_book = path;
self.filename = util.get_name(os.path.basename(path));
self.extension = util.get_extension(os.path.basename(path));

self.title = self.filename;
self.number_of_pages = 10;

self.get_and_save_cover();

def get_and_save_cover(self):
with zipfile.ZipFile(self.path_book, "r", compression=zipfile.ZIP_DEFLATED, allowZip64=True) as zip_file:
images = [image for image in zip_file.namelist() if "." in image];

extension = util.get_extension(os.path.basename(images[0]));
self.path_cover = f"/{configuration['PATH_COVERS']}{slugify(self.filename)}{extension}";

with open(self.path_cover[1::], "wb") as image:
image.write(zip_file.read(images[0]));

def get_page(self, page):
with zipfile.ZipFile(self.path_book, "r", compression=zipfile.ZIP_DEFLATED, allowZip64=True) as zip_file:
images = [image for image in zip_file.namelist() if "." in image];

extension = util.get_extension(os.path.basename(images[page]));
name = f"{configuration['PATH_TEMP']}{slugify(self.filename)}-{page}{extension}";

with open(name[1::], "wb") as image:
image.write(zip_file.read(images[page]));

return f"<img width='700px' src='{name}'>";

0 comments on commit f5af31a

Please sign in to comment.