openmedialibrary/oml/media/cbr.py

35 lines
786 B
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 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']
def filter_images(files):
return [f for f in files if os.path.splitext(f)[-1] in IMAGE_EXTENSION]
2015-03-14 07:35:15 +00:00
def cover(path):
data = None
#open rar file and extract first page here
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:
data = None
2015-03-14 07:35:15 +00:00
return data
def info(path):
data = {}
data['title'] = os.path.splitext(os.path.basename(path))[0]
#data['pages'] = fixme read rar to count pages
return data