Skip to content

Commit

Permalink
🚧 Having dinner
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Feb 24, 2024
1 parent 19f57e6 commit 68e4abf
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 48 deletions.
9 changes: 3 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
from pprint import pformat
from src import (Browser, Login, Action,
PrintIntro, Config, UserOptions, LinkParse,
print_title, pause,
DownloadImages, CreatePDF, DeleteIMG,
print_title, pause, create_directory,
logger)
from src.constants import DOWNLOAD_DIR
from src.utils.utils import create_directory
from src.modules.download_images import DownloadImages
from src.modules.create_pdf import CreatePDF
from src.modules.delete_img import DeleteIMG


def main() -> None:
Expand Down Expand Up @@ -39,7 +36,7 @@ def main() -> None:

if create_directory(DOWNLOAD_DIR, True):
print_title('DOWNLOAD')
DownloadImages.dowload_images(user_options.links, DOWNLOAD_DIR)
DownloadImages(user_options.links, DOWNLOAD_DIR).dowload_images()
if user_options.create_pdf:
print_title('PDF')
CreatePDF.create_pdf(DOWNLOAD_DIR, user_options.links)
Expand Down
4 changes: 2 additions & 2 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@


from .bot import Browser, Login, Action
from .modules import PrintIntro, Config, UserOptions, LinkParse
from .modules import PrintIntro, Config, UserOptions, LinkParse, DownloadImages, CreatePDF, DeleteIMG
from .modules import setup_argparse
from .utils import logger, print_title, pause
from .utils import logger, print_title, pause, create_directory
2 changes: 1 addition & 1 deletion src/bot/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __book_preview_to_page(link: str) -> str:
query = parse_qs(parser.query)
subfolder_value: str = query.get('subfolder', '')[0]
doc_value: str = query.get('doc', '')[0]
page_link: str = f'https//ir.vnulib.edu.vn/flowpaper/services/view.php?doc={doc_value}&format=jpg&subfolder={subfolder_value}' # nopep8
page_link: str = f'https://ir.vnulib.edu.vn/flowpaper/services/view.php?doc={doc_value}&format=jpg&subfolder={subfolder_value}' # nopep8
return page_link

@staticmethod
Expand Down
3 changes: 3 additions & 0 deletions src/modules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
from .user_options import UserOptions
from .link_parse import LinkParse
from .argpase import setup_argparse
from .download_images import DownloadImages
from .create_pdf import CreatePDF
from .delete_img import DeleteIMG
81 changes: 47 additions & 34 deletions src/modules/download_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ class DownloadImages:
Args:
- links (list[Link]): The list of links object
- download_directory (str): Download directory
"""

def __init__(self, links: list[Link]) -> None:
def __init__(self, links: list[Link], download_directory: str) -> None:
self.links: list[Link] = links
self.download_directory: str = os.pathsep.join(download_directory.split('/'))

@staticmethod
def download_image_from_page(link: str) -> bytes:
Expand All @@ -30,59 +32,70 @@ def download_image_from_page(link: str) -> bytes:
Return:
- Bytes : The datas of images
"""
response: Response = requests.get(
link, stream=True, timeout=10)
return response.content
return requests.get(link, stream=True, timeout=10).content

@staticmethod
def download_images_from_page_link_or_preview_link(link: LinkFile, download_directory: str) -> None:
"""Dowload All book's images from page link and previews link
def book_download(self, link: LinkFile, folder_path: str) -> None:
"""Download images for book
Args:
- link (LinkFile): link which that type is page link
link (LinkFile): Link object
folder_path (str): Destination for images to be downloaded
"""
folder_name: str = link.name
folder_path: str = os.path.join(download_directory, folder_name)
if create_directory(folder_path):
if create_directory(folder_path, force=True):
number_of_pages: int = link.num_pages
with alive_bar(number_of_pages) as bar: # pylint: disable=disallowed-name
for page_num in range(1, number_of_pages + 1):
sub_link: str = create_page_link(link.page_link, page_num)
image_bytes: bytes = DownloadImages.download_image_from_page(
image_bytes: bytes = self.download_image_from_page(
sub_link)
image: ImagePIL = Image.open(BytesIO(image_bytes))
image_path: str = os.path.join(
download_directory, f'image_{page_num}.jpg')
self.download_directory, f'image_{page_num}.jpg')
image.save(image_path)
bar() # pylint: disable=not-callable

@staticmethod
def download_images_from_book_link(links: Link, download_directory: str) -> None:
def book_handler(self, links: Link) -> None:
"""Download images from the preview link.
Args:
links (Link): The preview link.
download_directory (str): The directory to save the downloaded images.
"""
folder_book_name: str = links.name
folder_path: str = os.path.join(download_directory, folder_book_name)
if create_directory(folder_path):
folder_path: str = os.path.join(self.download_directory, links.name)
if create_directory(folder_path, force=True):
for link in links.files:
DownloadImages.download_images_from_page_link_or_preview_link(
link, folder_path)
self.book_download(link=link, folder_path=folder_path)

# Nguyen Xu lys di nha:))
def preview_and_page_download(self, link: LinkFile, folder_path: str) -> None:
if create_directory(folder_path, force=True):
number_of_pages: int = link.num_pages
with alive_bar(number_of_pages) as bar: # pylint: disable=disallowed-name
for page_num in range(1, number_of_pages + 1):
sub_link: str = create_page_link(link.page_link, page_num)
image_bytes: bytes = self.download_image_from_page(
sub_link)
image: ImagePIL = Image.open(BytesIO(image_bytes))
image_path: str = os.path.join(
self.download_directory, f'image_{page_num}.jpg')
image.save(image_path)
bar() # pylint: disable=not-callable

@staticmethod
def dowload_images(links: list[Link], download_directory: str) -> None:
"""Dowload Images from list of Link
def preview_and_page_handler(self, link: LinkFile) -> None:
"""Dowload All book's images from page link and previews link
Args:
-links (list[Link]):List of Link
-download_directory(str):the directory save dowloads folder
Args:
- link (LinkFile): link which that type is page link
"""
for link in links:
if link.original_type == 'page' or link.original_type == 'preview':
DownloadImages.download_images_from_page_link_or_preview_link(
link.files[0], download_directory)
if link.original_type == 'book':
DownloadImages.download_images_from_book_link(
link, download_directory)
folder_name: str = link.name
folder_path: str = os.path.join(self.download_directory, folder_name)
if create_directory(folder_path, force=True):
self.download_image_from_page(link=link, folder_path=folder_path)

def dowload_images(self) -> None:
"""Dowload Images from list of Link"""
for link in self.links:
match link.original_type:
case 'book':
self.book_handler(link)
case 'page' | 'preview':
self.preview_and_page_handler(link.files[0])
10 changes: 5 additions & 5 deletions src/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ def create_directory(directory: str, force: bool | None = None) -> bool:
except PermissionError as e:
logger.error(msg=e)
raise e
logger.info(msg=f'{directory} was removed recursively!')
logger.info(msg=f'\'{directory}\' was removed recursively!')
else:
logger.info(
msg=f'Skip creating {directory}')
msg=f'Skip creating \'{directory}\'')
return False
makedirs(name=directory)
logger.info(msg=f'{directory} was created!')
logger.info(msg=f'\'{directory}\' was created!')
return True


Expand All @@ -67,10 +67,10 @@ def remove_directory(directory: str) -> bool:
except PermissionError as e:
logger.error(msg=e)
raise e
logger.info(msg=f'{directory} was removed recursively!')
logger.info(msg=f'\'{directory}\' was removed recursively!')
return True
logger.info(
msg=f'{directory} was not found. Skip removing it')
msg=f'\'{directory}\' was not found. Skip removing it')
return False


Expand Down

0 comments on commit 68e4abf

Please sign in to comment.