Skip to content

Commit

Permalink
✅ Add DeleteIMG module for deleting images in folder
Browse files Browse the repository at this point in the history
  • Loading branch information
NTGNguyen committed Feb 22, 2024
1 parent a72d505 commit a6ba6ac
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/modules/delete_img.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit a6ba6ac

Please sign in to comment.