diff --git a/requirements.txt b/requirements.txt index 8c9ba31..b043548 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ requests==2.28.1 beautifulsoup4==4.11.1 +click==8.1.3 diff --git a/stanis-tits.py b/stanis-tits.py index de8d998..80ebb8c 100755 --- a/stanis-tits.py +++ b/stanis-tits.py @@ -7,13 +7,45 @@ import shutil import os.path import requests from bs4 import BeautifulSoup +import click __author__ = "Alexander Popov" -__version__ = "1.0.2" +__version__ = "1.0.3" __license__ = "Unlicense" # Путь к директории для загрузки изображений DOWNLOAD_DIRECTORY = "./images" +# Количество загружаемых страниц +DOWNLOAD_PAGES = 0 + + +def print_version(ctx, param, value): + if not value or ctx.resilient_parsing: + return + click.echo("Версия: {0}".format(__version__)) + ctx.exit() + + +@click.command(add_help_option=False) +@click.option( + "--directory", + default="./images", + help="Путь к директории для загрузки изображений.", +) +@click.option("--pages", default=0, help="Количество загружаемых страниц.") +@click.option( + "--version", + "-v", + is_flag=True, + callback=print_version, + is_eager=True, + help="Отобразить версию программы.", +) +@click.help_option("--help", "-h", help="Показать эту справку") +def click(directory, pages, version): + DOWNLOAD_DIRECTORY = directory + DOWNLOAD_PAGES = pages + main(DOWNLOAD_DIRECTORY, DOWNLOAD_PAGES) def get_images_links(page): @@ -71,11 +103,10 @@ def image_download(image): ) -if __name__ == "__main__": +def main(download_directory, download_pages): """Главный цикл программы""" - - if not os.path.exists(DOWNLOAD_DIRECTORY): - os.mkdir(DOWNLOAD_DIRECTORY) + if not os.path.exists(download_directory): + os.mkdir(download_directory) current_page = 0 downloaded_counter = 0 @@ -89,6 +120,12 @@ if __name__ == "__main__": images = get_images_links(current_page) images.sort() + # Проверка на количество загруженных страниц + # аргумент --pages + if current_page >= download_pages and download_pages != 0: + print("\nВсего загружено {0} файлов.".format(downloaded_counter)) + quit() + # По очереди скачиваем изображения for image in images: image_download(image) @@ -108,5 +145,9 @@ if __name__ == "__main__": current_page += 1 except KeyboardInterrupt: - print("Всего загружено {0} файлов.".format(downloaded_counter)) + print("\nВсего загружено {0} файлов.".format(downloaded_counter)) quit() + + +if __name__ == "__main__": + click()