rpiwebapp-public/scripts/func.py
Matthew Welch 2f855db067 Comics can be read
Comics can be opened and read. Thumbnails for each comics are now stored in the database. Some files have been renamed for clarification.
2019-07-11 17:35:30 -07:00

108 lines
2.9 KiB
Python

from comicapi import comicarchive
from blinker import Namespace
from io import BytesIO
from PIL import Image
import os, sys
from scripts import database
rpi_signals = Namespace()
comic_loaded = rpi_signals.signal("comic-loaded")
# Directories
COMICS_DIRECTORY = "/usb/storage/media/Comics/"
MOVIES_DIRECTORY = "/usb/storage/media/Videos/Movies/"
TV_SHOWS_DIRECTORY = "/usb/storage/media/Videos/TV/"
VIDEOS_DIRECTORY = "/usb/storage/media/Videos/Videos/"
GAMES_DIRECTORY = "/usb/storage/media/games/"
MUSIC_DIRECTORY = "/usb/storage/media/Music/"
#############
def get_comics():
total_comics = 0
comics_in_db = 0
comics_added = 0
meta = []
thumbnails = []
i = 0
for root, dirs, files in os.walk(COMICS_DIRECTORY):
for f in files:
if f.endswith(".cbr"):
total_comics += 1
path = os.path.join(root, f)
if not database.comic_path_in_db(path):
try:
test_path = path.encode("utf8")
except Exception as e:
print("encoding failed on:", path)
print(e)
continue
print(path)
archive = open_comic(path)
thumbnails.append(get_comic_thumbnails(archive))
meta.append((path, archive.readCIX()))
comics_added += 1
comics_in_db += 1
i += 1
if i >= 20:
comic_loaded.send("anonymous", meta=meta.copy(), thumbnails=thumbnails.copy())
meta.clear()
thumbnails.clear()
i = 0
else:
comics_in_db += 1
print("total number of comics:", total_comics)
print("comics in database:", comics_in_db)
print("number of comics added:", comics_added)
comic_loaded.send("anonymous", meta=meta, thumbnails=thumbnails)
def get_comic_thumbnails(comic):
thumbnails = []
size = 128, 128
for page in range(comic.getNumberOfPages()):
image_bytes = BytesIO(comic.getPage(page))
image = Image.open(image_bytes)
image.thumbnail(size)
thumbnails.append(image.tobytes())
return thumbnails
def open_comic(path):
archive = comicarchive.ComicArchive(path, default_image_path="/usb/www/matthew/rpiWebApp/static/icon.png")
return archive
def bytestring_path(path):
"""Given a path, which is either a bytes or a unicode, returns a str
path (ensuring that we never deal with Unicode pathnames).
"""
# Pass through bytestrings.
if isinstance(path, bytes):
return path
# Try to encode with default encodings, but fall back to UTF8.
try:
return path.encode(_fsencoding())
except (UnicodeError, LookupError):
return path.encode('utf8')
def _fsencoding():
"""Get the system's filesystem encoding. On Windows, this is always
UTF-8 (not MBCS).
"""
encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
if encoding == 'mbcs':
# On Windows, a broken encoding known to Python as "MBCS" is
# used for the filesystem. However, we only use the Unicode API
# for Windows paths, so the encoding is actually immaterial so
# we can avoid dealing with this nastiness. We arbitrarily
# choose UTF-8.
encoding = 'utf8'
return encoding