Skip to content
This repository has been archived by the owner on Sep 6, 2024. It is now read-only.

Commit

Permalink
improved gallery handling speed by further reducing hard drive calls
Browse files Browse the repository at this point in the history
  • Loading branch information
9FS committed Nov 15, 2023
1 parent 1a2f050 commit 282b6f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ license = "MIT"
name = "x" # can't leave empty because of bug with `poetry install` from poetry.lock file
readme = "readme.md"
repository = "https://github.com/9-FS/2022-10-23-nHentai-to-PDF-Server"
version = "2.3.9"
version = "2.3.10"

[tool.poetry.dependencies]
kfsconfig = "^1.0.0"
Expand Down
15 changes: 10 additions & 5 deletions src/Hentai.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Hentai:

galleries: typing.ClassVar[list[dict]]=[] # list of already downloaded galleries
GALLERIES_FILEPATH: str="./config/galleries.json" # path to file containing already downloaded galleries
galleries_modified: bool=False # has galleries been modified since last save?


def __init__(self, nhentai_ID: int, cookies: dict[str, str], headers: dict[str, str]):
Expand Down Expand Up @@ -132,14 +133,12 @@ def _get_gallery(cls, nhentai_ID: int, cookies: dict[str, str], headers: dict[st
if str(gallery["id"]) in [str(gallery["id"]) for gallery in cls.galleries]: # if gallery already downloaded but adding to class variable requested: something went wrong
logging.critical(f"Gallery {nhentai_ID} has been requested to be added to galleries even though it would result in a duplicate entry.")
raise RuntimeError(f"Error in {Hentai._get_gallery.__name__}{inspect.signature(Hentai._get_gallery)}: Gallery {nhentai_ID} has been requested to be added to galleries even though it would result in a duplicate entry.")
cls.galleries.append(gallery)
cls.galleries.append(gallery) # append new gallery
cls.galleries=sorted(cls.galleries, key=lambda gallery: int(gallery["id"])) # sort galleries by ID
cls.galleries_modified=True # galleries have been modified, save during next save turn
break
logging.info(f"\rDownloaded gallery {nhentai_ID} from \"{NHENTAI_GALLERY_API_URL}/{nhentai_ID}\".")

cls.galleries=sorted(cls.galleries, key=lambda gallery: int(gallery["id"])) # sort galleries by ID
if nhentai_ID%10==0: # only save to file roughly every 10 galleries to save time
cls.save_galleries()

return gallery


Expand Down Expand Up @@ -255,9 +254,15 @@ def save_galleries(cls) -> None:
Saves galleries to file.
"""

if len(cls.galleries)==0 or cls.galleries_modified==False: # don't save if nothing to save, might prevent overwriting galleries file with uninitialised galleries
return


logging.info(f"Saving galleries in \"{cls.GALLERIES_FILEPATH}\"...")
with open(cls.GALLERIES_FILEPATH, "wt") as galleries_file:
galleries_file.write(json.dumps(cls.galleries, indent=4))
logging.info(f"\rSaved galleries in \"{cls.GALLERIES_FILEPATH}\".")

cls.galleries_modified=False # reset modified flag

return
5 changes: 4 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def main():
logging.info("--------------------------------------------------")
logging.info(f"{KFSfstr.notation_abs(i+1, 0, round_static=True)}/{KFSfstr.notation_abs(len(hentai_ID_list), 0, round_static=True)} ({KFSfstr.notation_abs((i+1)/(len(hentai_ID_list)), 2, round_static=True)})")

if (i+1)%100==0: # save galleries to file, only every 100 hentai to save time
Hentai.save_galleries()

try:
hentai=Hentai(hentai_ID, cookies, headers) # create hentai object
except ValueError: # if hentai does not exist:
Expand All @@ -67,7 +70,7 @@ def main():
logging.info("--------------------------------------------------")


Hentai.save_galleries() # now save all galleries to file, might not have all saved yet because usually only save roughly every 10 galleries to file
Hentai.save_galleries() # save all galleries to file

logging.info("Deleting leftover image directories...")
for hentai_ID in hentai_ID_list: # attempt final cleanup
Expand Down

0 comments on commit 282b6f4

Please sign in to comment.