openmedialibrary/oml/media/cbr.py

96 lines
2.2 KiB
Python
Raw Normal View History

2015-03-14 07:35:15 +00:00
# -*- coding: utf-8 -*-
2019-02-01 12:57:58 +00:00
import logging
2015-03-14 07:35:15 +00:00
import os
2019-02-01 13:49:16 +00:00
import zipfile
2019-02-01 12:57:58 +00:00
2019-01-29 10:20:43 +00:00
import ox
2015-03-14 07:35:15 +00:00
2019-02-01 12:57:58 +00:00
logger = logging.getLogger(__name__)
IMAGE_EXTENSIONS = ['.jpg', '.png', '.gif']
2019-02-01 13:49:16 +00:00
2019-02-01 12:57:58 +00:00
def filter_images(files):
2019-02-01 13:49:16 +00:00
return [f for f in files if os.path.splitext(f)[-1].lower() in IMAGE_EXTENSIONS]
def detect_format(path):
with open(path, 'rb') as fd:
head = fd.read(10)
if head[:2] == b'PK':
return 'cbz'
if head[:3] == b'Rar':
return 'cbr'
logger.debug('unknown cbr/cbz file %s - %s', head, path)
return 'unknown'
2019-02-01 12:57:58 +00:00
2015-03-14 07:35:15 +00:00
def cover(path):
2019-02-01 13:49:16 +00:00
format = detect_format(path)
if format == 'cbz':
cover = cover_cbz(path)
elif format == 'cbr':
cover = cover_cbr(path)
else:
cover = None
return cover
def cover_cbr(path):
2015-03-14 07:35:15 +00:00
data = None
2019-01-29 10:20:43 +00:00
try:
from unrar import rarfile
rar = rarfile.RarFile(path)
2019-01-29 13:16:12 +00:00
files = rar.namelist()
2019-02-01 12:57:58 +00:00
files = filter_images(files)
2019-01-29 13:16:12 +00:00
if files:
cover = ox.sorted_strings(files)[0]
data = rar.read(cover)
2019-01-29 10:20:43 +00:00
except:
2019-02-01 13:49:16 +00:00
logger.debug('invalid cbr file %s', path)
2019-01-29 10:20:43 +00:00
data = None
2015-03-14 07:35:15 +00:00
return data
2019-02-01 13:49:16 +00:00
def cover_cbz(path):
data = None
logger.debug('cover %s', path)
data = None
try:
z = zipfile.ZipFile(path)
except zipfile.BadZipFile:
logger.debug('invalid cbz file %s', path)
return data
files = [f.filename for f in z.filelist]
files = filter_images(files)
if files:
cover = ox.sorted_strings(files)[0]
try:
data = z.read(cover)
except:
data = None
return data
2019-02-01 13:54:34 +00:00
def get_pages(path):
files = []
format = detect_format(path)
if format == 'cbz':
try:
z = zipfile.ZipFile(path)
except zipfile.BadZipFile:
logger.debug('invalid cbz file %s', path)
return data
files = [f.filename for f in z.filelist]
elif format == 'cbr':
try:
from unrar import rarfile
rar = rarfile.RarFile(path)
files = rar.namelist()
except:
pass
files = filter_images(files)
return len(files)
2019-02-01 13:49:16 +00:00
2015-03-14 07:35:15 +00:00
def info(path):
data = {}
data['title'] = os.path.splitext(os.path.basename(path))[0]
2019-02-01 13:54:34 +00:00
data['pages'] = get_pages(path)
2015-03-14 07:35:15 +00:00
return data