From 8944ece4a88f97bb5322cdb8e45909cea20c9ab2 Mon Sep 17 00:00:00 2001 From: ricterz Date: Sat, 11 Jan 2025 08:48:43 +0800 Subject: [PATCH] use os.path.sep as path separator --- nhentai/cmdline.py | 10 +++++++--- nhentai/constant.py | 2 ++ nhentai/serializer.py | 4 +++- nhentai/utils.py | 7 ++++--- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index f08d0b2..d5f931c 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -11,6 +11,7 @@ from nhentai import __version__ from nhentai.utils import generate_html, generate_main_html, DB from nhentai.logger import logger +from nhentai.constant import PATH_SEPARATOR def banner(): @@ -64,7 +65,8 @@ def cmd_parser(): # operation options parser.add_option('--download', '-D', dest='is_download', action='store_true', help='download doujinshi (for search results)') - parser.add_option('--show', '-S', dest='is_show', action='store_true', help='just show the doujinshi information') + parser.add_option('--show', '-S', dest='is_show', action='store_true', + help='just show the doujinshi information') # doujinshi options parser.add_option('--id', dest='id', action='callback', callback=callback, @@ -86,7 +88,8 @@ def cmd_parser(): choices=['recent', 'popular', 'popular-today', 'popular-week', 'date']) # download options - parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='./', + parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', + default=f'.{PATH_SEPARATOR}', help='output dir') parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5, help='thread count for downloading doujinshi') @@ -96,7 +99,8 @@ def cmd_parser(): help='slow down between downloading every doujinshi') parser.add_option('--proxy', type='string', dest='proxy', action='store', help='store a proxy, for example: -p "http://127.0.0.1:1080"') - parser.add_option('--file', '-f', type='string', dest='file', action='store', help='read gallery IDs from file.') + parser.add_option('--file', '-f', type='string', dest='file', action='store', + help='read gallery IDs from file.') parser.add_option('--format', type='string', dest='name_format', action='store', help='format the saved folder name', default='[%i][%a][%t]') parser.add_option('--dry-run', action='store_true', dest='dryrun', help='Dry run, skip file download') diff --git a/nhentai/constant.py b/nhentai/constant.py index 42b1d0f..8942b1a 100644 --- a/nhentai/constant.py +++ b/nhentai/constant.py @@ -35,6 +35,8 @@ def get_nhentai_home() -> str: CHALLENGE_URL = f'{BASE_URL}/challenge' FAV_URL = f'{BASE_URL}/favorites/' +PATH_SEPARATOR = os.path.sep + IMAGE_URL = f'{urlparse(BASE_URL).scheme}://i.{urlparse(BASE_URL).hostname}/galleries' IMAGE_URL_MIRRORS = [ diff --git a/nhentai/serializer.py b/nhentai/serializer.py index 29c0ef4..fbcc2d9 100644 --- a/nhentai/serializer.py +++ b/nhentai/serializer.py @@ -1,6 +1,8 @@ # coding: utf-8 import json import os + +from nhentai.constant import PATH_SEPARATOR from xml.sax.saxutils import escape from nhentai.constant import LANGUAGE_ISO @@ -82,7 +84,7 @@ def xml_write_simple_tag(f, name, val, indent=1): def merge_json(): lst = [] - output_dir = "./" + output_dir = f".{PATH_SEPARATOR}" os.chdir(output_dir) doujinshi_dirs = next(os.walk('.'))[1] for folder in doujinshi_dirs: diff --git a/nhentai/utils.py b/nhentai/utils.py index edaea5c..c38063f 100644 --- a/nhentai/utils.py +++ b/nhentai/utils.py @@ -14,6 +14,7 @@ from requests.structures import CaseInsensitiveDict from nhentai import constant +from nhentai.constant import PATH_SEPARATOR from nhentai.logger import logger from nhentai.serializer import serialize_json, serialize_comic_xml, set_js_database @@ -100,7 +101,7 @@ def parse_doujinshi_obj( file_type: str = '' ) -> Tuple[str, str]: - filename = f'./doujinshi.{file_type}' + filename = f'.{PATH_SEPARATOR}doujinshi.{file_type}' if doujinshi_obj is not None: doujinshi_dir = os.path.join(output_dir, doujinshi_obj.filename) _filename = f'{doujinshi_obj.filename}.{file_type}' @@ -113,7 +114,7 @@ def parse_doujinshi_obj( filename = os.path.join(output_dir, _filename) else: - doujinshi_dir = './' + doujinshi_dir = f'.{PATH_SEPARATOR}' return doujinshi_dir, filename @@ -178,7 +179,7 @@ def move_to_folder(output_dir='.', doujinshi_obj=None, file_type=None): shutil.move(filename, os.path.join(doujinshi_dir, os.path.basename(filename))) -def generate_main_html(output_dir='./'): +def generate_main_html(output_dir=f'.{PATH_SEPARATOR}'): """ Generate a main html to show all the contains doujinshi. With a link to their `index.html`.