Support remote files + binary files

This commit is contained in:
hyugogirubato
2023-02-08 19:51:08 +01:00
parent 40cb3759f2
commit f5debc6f56
10 changed files with 119 additions and 1 deletions
+4
View File
@@ -12,3 +12,7 @@ class MissingPageFile(PyCBZHelperException):
class InvalidFilePermission(PyCBZHelperException):
"""Unable to delete existing file."""
class FileNotFound(PyCBZHelperException):
"""The specified source file was not found."""
+12 -1
View File
@@ -3,12 +3,13 @@ import pathlib
import shutil
import zipfile
import requests
from json2xml import json2xml
from langcodes import Language
from PIL import Image
from pycbzhelper.comicinfo import KEYS_STRING, KEYS_INT, KEYS_SPECIAL, KEYS_AGE, KEYS_FORMAT, KEYS_PAGE_TYPE
from pycbzhelper.exceptions import InvalidKeyValue, MissingPageFile, InvalidFilePermission
from pycbzhelper.exceptions import InvalidKeyValue, MissingPageFile, InvalidFilePermission, FileNotFound
from pycbzhelper.utils import get_key_value, delete_none, slugify
@@ -60,6 +61,16 @@ class Helper:
kwargs['PageCount'] = len(kwargs.get('Pages'))
for i in range(kwargs.get('PageCount')):
page = kwargs.get('Pages')[i]
if isinstance(page['File'], bytes) or (isinstance(page['File'], str) and page['File'].startswith('https://')):
file = os.path.join("tmp", f"page-{i:03d}.jpg")
with open(file, mode="wb") as f:
f.write(page['File'] if isinstance(page['File'], bytes) else requests.get(url=page['File']).content)
f.close()
page['File'] = file
if not os.path.exists(page['File']):
raise FileNotFound(f"ERROR: Source file is invalid: {page['File']}")
properties = Image.open(page['File'])
self._files.append(page['File'])
if not page.get('Type'):