mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-04-20 02:41:19 +02:00
refactor: de-dupe doujinshi_obj parsers
This commit is contained in:
parent
12364e980c
commit
4bfe104714
@ -93,7 +93,15 @@ def main():
|
|||||||
|
|
||||||
if options.generate_metadata:
|
if options.generate_metadata:
|
||||||
table = doujinshi.table
|
table = doujinshi.table
|
||||||
generate_metadata_file(options.output_dir, table, doujinshi)
|
|
||||||
|
# Skip downloading metadata if archived file is already generated.
|
||||||
|
check_file_type = ''
|
||||||
|
if options.is_cbz: check_file_type = '.cbz'
|
||||||
|
elif options.is_pdf: check_file_type = '.pdf'
|
||||||
|
|
||||||
|
result = generate_metadata_file(options.output_dir, table, doujinshi, check_file_type)
|
||||||
|
# Already downloaded; continue on with the other doujins.
|
||||||
|
if not result: continue
|
||||||
|
|
||||||
if options.is_save_download_history:
|
if options.is_save_download_history:
|
||||||
with DB() as db:
|
with DB() as db:
|
||||||
|
@ -5,9 +5,11 @@ import re
|
|||||||
import os
|
import os
|
||||||
import zipfile
|
import zipfile
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
from typing import Optional, Tuple
|
||||||
|
|
||||||
from nhentai import constant
|
from nhentai import constant
|
||||||
from nhentai.logger import logger
|
from nhentai.logger import logger
|
||||||
@ -65,13 +67,41 @@ def readfile(path):
|
|||||||
return file.read()
|
return file.read()
|
||||||
|
|
||||||
|
|
||||||
def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
|
def parse_doujinshi_obj(
|
||||||
image_html = ''
|
output_dir: str,
|
||||||
|
doujinshi_obj = None,
|
||||||
|
file_extension: str = '',
|
||||||
|
write_comic_info = False
|
||||||
|
) -> Tuple[str, str, bool]:
|
||||||
|
doujinshi_dir = '.'
|
||||||
|
filename = './doujinshi' + file_extension
|
||||||
|
already_downloaded = False
|
||||||
|
|
||||||
if doujinshi_obj is not None:
|
if doujinshi_obj is not None:
|
||||||
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
||||||
else:
|
if os.path.exists(doujinshi_dir + file_extension):
|
||||||
doujinshi_dir = '.'
|
logger.warning(f'File already exists, skipping "{doujinshi_dir}"')
|
||||||
|
already_downloaded = True
|
||||||
|
elif file_extension is not None:
|
||||||
|
_filename = f'{doujinshi_obj.filename}{file_extension}'
|
||||||
|
|
||||||
|
if file_extension == '.cbz' and write_comic_info:
|
||||||
|
serialize_comic_xml(doujinshi_obj, doujinshi_dir)
|
||||||
|
|
||||||
|
if file_extension == '.pdf':
|
||||||
|
_filename = _filename.replace('/', '-')
|
||||||
|
|
||||||
|
# Isn't os.path.join(doujinshi_dir, '..') equivalent to output_dir?
|
||||||
|
filename = os.path.join(os.path.join(doujinshi_dir, '..'), _filename)
|
||||||
|
|
||||||
|
return doujinshi_dir, filename, already_downloaded
|
||||||
|
|
||||||
|
|
||||||
|
def generate_html(output_dir='.', doujinshi_obj=None, template='default'):
|
||||||
|
doujinshi_dir, filename, already_downloaded = parse_doujinshi_obj(output_dir, doujinshi_obj)
|
||||||
|
if already_downloaded: return
|
||||||
|
|
||||||
|
image_html = ''
|
||||||
|
|
||||||
if not os.path.exists(doujinshi_dir):
|
if not os.path.exists(doujinshi_dir):
|
||||||
logger.warning(f'Path "{doujinshi_dir}" does not exist, creating.')
|
logger.warning(f'Path "{doujinshi_dir}" does not exist, creating.')
|
||||||
@ -166,23 +196,14 @@ def generate_main_html(output_dir='./'):
|
|||||||
|
|
||||||
|
|
||||||
def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_comic_info=True, move_to_folder=False):
|
def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_comic_info=True, move_to_folder=False):
|
||||||
if doujinshi_obj is not None:
|
doujinshi_dir, filename, already_downloaded = parse_doujinshi_obj(output_dir, doujinshi_obj, 'cbz', write_comic_info)
|
||||||
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
if already_downloaded: return
|
||||||
if os.path.exists(doujinshi_dir+".cbz"):
|
|
||||||
logger.warning(f'Comic Book CBZ file exists, skip "{doujinshi_dir}"')
|
|
||||||
return
|
|
||||||
if write_comic_info:
|
|
||||||
serialize_comic_xml(doujinshi_obj, doujinshi_dir)
|
|
||||||
cbz_filename = os.path.join(os.path.join(doujinshi_dir, '..'), f'{doujinshi_obj.filename}.cbz')
|
|
||||||
else:
|
|
||||||
cbz_filename = './doujinshi.cbz'
|
|
||||||
doujinshi_dir = '.'
|
|
||||||
|
|
||||||
file_list = os.listdir(doujinshi_dir)
|
file_list = os.listdir(doujinshi_dir)
|
||||||
file_list.sort()
|
file_list.sort()
|
||||||
|
|
||||||
logger.info(f'Writing CBZ file to path: {cbz_filename}')
|
logger.info(f'Writing CBZ file to path: {filename}')
|
||||||
with zipfile.ZipFile(cbz_filename, 'w') as cbz_pf:
|
with zipfile.ZipFile(filename, 'w') as cbz_pf:
|
||||||
for image in file_list:
|
for image in file_list:
|
||||||
image_path = os.path.join(doujinshi_dir, image)
|
image_path = os.path.join(doujinshi_dir, image)
|
||||||
cbz_pf.write(image_path, image)
|
cbz_pf.write(image_path, image)
|
||||||
@ -199,7 +220,7 @@ def generate_cbz(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, write_
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error deleting file: {e}")
|
print(f"Error deleting file: {e}")
|
||||||
|
|
||||||
shutil.move(cbz_filename, doujinshi_dir)
|
shutil.move(filename, doujinshi_dir)
|
||||||
|
|
||||||
logger.log(16, f'Comic Book CBZ file has been written to "{doujinshi_dir}"')
|
logger.log(16, f'Comic Book CBZ file has been written to "{doujinshi_dir}"')
|
||||||
|
|
||||||
@ -209,22 +230,14 @@ def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, move_t
|
|||||||
import img2pdf
|
import img2pdf
|
||||||
|
|
||||||
"""Write images to a PDF file using img2pdf."""
|
"""Write images to a PDF file using img2pdf."""
|
||||||
if doujinshi_obj is not None:
|
doujinshi_dir, filename, already_downloaded = parse_doujinshi_obj(output_dir, doujinshi_obj, 'pdf')
|
||||||
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
if already_downloaded: return
|
||||||
filename = doujinshi_obj.filename.replace('/', '-')
|
|
||||||
pdf_filename = os.path.join(
|
|
||||||
os.path.join(doujinshi_dir, '..'),
|
|
||||||
f'{filename}.pdf'
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
pdf_filename = './doujinshi.pdf'
|
|
||||||
doujinshi_dir = '.'
|
|
||||||
|
|
||||||
file_list = os.listdir(doujinshi_dir)
|
file_list = os.listdir(doujinshi_dir)
|
||||||
file_list.sort()
|
file_list.sort()
|
||||||
|
|
||||||
logger.info(f'Writing PDF file to path: {pdf_filename}')
|
logger.info(f'Writing PDF file to path: {filename}')
|
||||||
with open(pdf_filename, 'wb') as pdf_f:
|
with open(filename, 'wb') as pdf_f:
|
||||||
full_path_list = (
|
full_path_list = (
|
||||||
[os.path.join(doujinshi_dir, image) for image in file_list]
|
[os.path.join(doujinshi_dir, image) for image in file_list]
|
||||||
)
|
)
|
||||||
@ -242,7 +255,7 @@ def generate_pdf(output_dir='.', doujinshi_obj=None, rm_origin_dir=False, move_t
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error deleting file: {e}")
|
print(f"Error deleting file: {e}")
|
||||||
|
|
||||||
shutil.move(pdf_filename, doujinshi_dir)
|
shutil.move(filename, doujinshi_dir)
|
||||||
|
|
||||||
logger.log(16, f'PDF file has been written to "{doujinshi_dir}"')
|
logger.log(16, f'PDF file has been written to "{doujinshi_dir}"')
|
||||||
|
|
||||||
@ -303,13 +316,11 @@ def paging(page_string):
|
|||||||
return page_list
|
return page_list
|
||||||
|
|
||||||
|
|
||||||
def generate_metadata_file(output_dir, table, doujinshi_obj=None):
|
def generate_metadata_file(output_dir, table, doujinshi_obj=None, check_file_type=''):
|
||||||
logger.info('Writing Metadata Info')
|
logger.info('Writing Metadata Info')
|
||||||
|
|
||||||
if doujinshi_obj is not None:
|
doujinshi_dir, filename, already_downloaded = parse_doujinshi_obj(output_dir, doujinshi_obj, file_extension=check_file_type)
|
||||||
doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename)
|
if already_downloaded: return False
|
||||||
else:
|
|
||||||
doujinshi_dir = '.'
|
|
||||||
|
|
||||||
logger.info(doujinshi_dir)
|
logger.info(doujinshi_dir)
|
||||||
|
|
||||||
@ -330,6 +341,8 @@ def generate_metadata_file(output_dir, table, doujinshi_obj=None):
|
|||||||
|
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
class DB(object):
|
class DB(object):
|
||||||
conn = None
|
conn = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user