From a6ba6ac97a7f12495412742064025eb98c5ee587 Mon Sep 17 00:00:00 2001 From: NTGNguyen <23521049@gm.uit.edu.vn> Date: Thu, 22 Feb 2024 23:16:09 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20DeleteIMG=20module=20for=20de?= =?UTF-8?q?leting=20images=20in=20folder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/delete_img.py | 54 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/modules/delete_img.py diff --git a/src/modules/delete_img.py b/src/modules/delete_img.py new file mode 100644 index 0000000..2685134 --- /dev/null +++ b/src/modules/delete_img.py @@ -0,0 +1,54 @@ +"""Delete the images in folder""" + +import os +from .link_parse import Link + + +class DeleteIMG: + """Merge the images of books into PDF files + + Args: + - links (list[Link]): The list of links object + """ + + def __init__(self, links: list[Link]) -> None: + self.links: list[Link] = links + + @staticmethod + def delete_jpg_page_link_or_preview_link(page_directory: str) -> None: + """Delete JPG file in directory + + Args: + directory (str): The directory containing the JPG images. + """ + jpg_files: list[str] = [os.path.join(page_directory, f) for f in os.listdir(page_directory) if f.endswith('.jpg')] + for jpg_file in jpg_files: + os.remove(jpg_file) + + @staticmethod + def delete_jpg_book_link(book_directory: str, link: Link) -> None: + """ + For each subdirectory in a directory, merge all JPG images into a single PDF. + The PDF is saved in the same subdirectory with the name of the subdirectory. + + Args: + directory (str): The directory containing the subdirectories. + link (Link): The book's link + """ + for link_page in link.files: + DeleteIMG.delete_jpg_page_link_or_preview_link(os.path.join(book_directory, link_page.name)) + + @staticmethod + def delete_jpg(dowload_directory: str, links: list[Link]) -> None: + """ + For each subdirectory in a directory, if there are JPG images, delete it + + Ars: + directory (str): The directory containing the subdirectories. + links(list[Link]): The list of Link + """ + for link in links: + if link.original_type == 'book': + DeleteIMG.delete_jpg_book_link(os.path.join(dowload_directory, link.name), link) + else: + DeleteIMG.delete_jpg_page_link_or_preview_link(os.path.join(dowload_directory, link.name))