Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
skymkmk committed Jul 28, 2023
2 parents e6d68ab + 51999ed commit 3be51f9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
45 changes: 44 additions & 1 deletion download.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
import hashlib
import os
import sys
import zipfile
from typing import Union, List, Tuple

Expand Down Expand Up @@ -71,13 +72,55 @@ async def _download_img(sem: asyncio.Semaphore, path: str, gid: int, page_num: i
return False


def _detect_cbz(root: str, gid: int) -> bool:
matched_dir = [os.path.join(root, i) for i in os.listdir(root) if str(gid) in i]
if len(matched_dir) == 0:
return False
elif len(matched_dir) > 1:
logger.error(f"Detected multi path for {gid} in {root}. Plz deal with it.")
exit(exitcodes.MULTI_GID_IN_DL_DIR_DETECTED)
else:
matched_dir = matched_dir[0]
cbzs = [os.path.join(matched_dir, i) for i in os.listdir(matched_dir) if '.cbz' in i and str(gid) in i]
if len(cbzs) == 0:
return False
elif len(cbzs) > 1:
logger.error(f"Detected multi cbz in {matched_dir}. Plz deal with it.")
exit(exitcodes.MULTI_CBZ_IN_DL_DIR_DETECTED)
else:
if os.stat(cbzs[0]).st_size > 102400:
return True
else:
return False


async def download() -> None:
gal_info = sql.select_doujinshi_for_download()
for i in gal_info:
category_name = sql.select_category_name(i[4])
root = os.path.join(config.save_path, f'{i[4]}-{category_name}')
# Recover download.
if config.save_as_cbz:
detected = _detect_cbz(root, i[0])
if detected:
args = sys.argv[1:]
if '--just-detect-cbz' in args:
skip_cbz = True
else:
while True:
option = input(f"{i[3]} cbz file detected, do you wanna skip it? [N/y]: ")
if option in ['Y', 'y']:
skip_cbz = True
break
elif option in ['N', 'n']:
skip_cbz = False
break
if skip_cbz:
sql.update_gallery_success(i[0])
logger.info(f"Skip {i[3]}")
continue
# Replace the illegal characters
path = f"{i[0]}-{i[3]}"
root = os.path.join(config.save_path, f'{i[4]}-{category_name}')
path = truncate_path(root, path)
if not os.path.exists(path):
os.mkdir(path)
Expand Down
2 changes: 2 additions & 0 deletions exitcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
CANT_PARSE_IMG_PAGE = 6
CANT_FETCH_EHAPI = 7
BUMPED_509 = 8
MULTI_GID_IN_DL_DIR_DETECTED = 9
MULTI_CBZ_IN_DL_DIR_DETECTED = 10
4 changes: 2 additions & 2 deletions pageparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def parse_fav_galleries_list(page: bytes) -> Union[Tuple[List[Tuple[int, str, st
node_list = html.xpath(FAV_GALLERY_XPATH)
if len(node_list) == 0:
notice = html.xpath(FAV_GALLERY_EMPTY_XPATH)
if len(notice) != 1:
if len(notice) == 0:
logger.error(page.decode(encoding="UTF-8", errors="ignore"))
exit(exitcodes.CANT_PARSE_FAV_LIST)
logger.info(notice[0])
Expand Down Expand Up @@ -83,7 +83,7 @@ async def parse_gallery_img_list(url: str, img_list: Union[List[Tuple[str]], Non
the_list = html.xpath(IMG_LIST_XPATH)
if len(the_list) == 0:
notice = html.xpath(GALLERY_NOTICE_XPATH)
if len(notice) != 1:
if len(notice) == 0:
logger.error(page.decode(encoding="UTF-8", errors="ignore"))
exit(exitcodes.CANT_PARSE_GALLERY)
logger.error(notice)
Expand Down

0 comments on commit 3be51f9

Please sign in to comment.