mirror of
https://github.com/kuhyx/testsAndMisc-archive.git
synced 2026-07-04 12:43:15 +02:00
fix: enable all pylint checks by wrapping scripts in main()
- Remove too-few-public-methods and invalid-name from disabled checks - Wrap module-level code in main() functions: - generate_jpeg.py - random_digits.py - generate_cats.py - scrape_comics.py - Rename download_image -> _download_image (private function) Pylint score: 10.00/10 with all checks enabled
This commit is contained in:
parent
077a31cb54
commit
3a477dd868
@ -40,16 +40,22 @@ def _download_single_image(image_url: str) -> None:
|
||||
_logger.exception("Failed to download %s", image_url)
|
||||
|
||||
|
||||
requests_send = 0
|
||||
while requests_send < MAX_REQUESTS:
|
||||
res = requests.get(
|
||||
"https://api.thecatapi.com/v1/images/search?limit=100&api_key=",
|
||||
timeout=REQUEST_TIMEOUT,
|
||||
)
|
||||
requests_send += 1
|
||||
response = json.loads(res.text)
|
||||
urls = [cat.get("url") for cat in response]
|
||||
def main() -> None:
|
||||
"""Download cat images from TheCatAPI."""
|
||||
requests_sent = 0
|
||||
while requests_sent < MAX_REQUESTS:
|
||||
res = requests.get(
|
||||
"https://api.thecatapi.com/v1/images/search?limit=100&api_key=",
|
||||
timeout=REQUEST_TIMEOUT,
|
||||
)
|
||||
requests_sent += 1
|
||||
response = json.loads(res.text)
|
||||
urls = [cat.get("url") for cat in response]
|
||||
|
||||
Path("./CATS2").mkdir(parents=True, exist_ok=True)
|
||||
for url in urls:
|
||||
_download_single_image(url)
|
||||
Path("./CATS2").mkdir(parents=True, exist_ok=True)
|
||||
for url in urls:
|
||||
_download_single_image(url)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@ -70,7 +70,9 @@ def _parse_single_number(num_str: str) -> tuple[float, int] | None:
|
||||
|
||||
MIN_ARGS = 2
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
def main() -> None:
|
||||
"""Run the number randomizer from command line arguments."""
|
||||
if len(sys.argv) < MIN_ARGS:
|
||||
_logger.info(
|
||||
"Usage: python random_digits.py <number1> <number2> ... "
|
||||
@ -78,27 +80,27 @@ if __name__ == "__main__":
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
input_string = " ".join(sys.argv[1:])
|
||||
numbers, decimal_counts = parse_input(input_string)
|
||||
args_string = " ".join(sys.argv[1:])
|
||||
numbers, decimal_counts = parse_input(args_string)
|
||||
|
||||
if not numbers:
|
||||
_logger.error("No valid numbers provided.")
|
||||
sys.exit(1)
|
||||
|
||||
min_percentage = DEFAULT_MIN_PERCENTAGE
|
||||
max_percentage = DEFAULT_MAX_PERCENTAGE
|
||||
min_pct = DEFAULT_MIN_PERCENTAGE
|
||||
max_pct = DEFAULT_MAX_PERCENTAGE
|
||||
|
||||
try:
|
||||
if len(sys.argv) > len(numbers) + 1:
|
||||
with contextlib.suppress(ValueError):
|
||||
min_percentage = float(sys.argv[len(numbers) + 1])
|
||||
min_pct = float(sys.argv[len(numbers) + 1])
|
||||
if len(sys.argv) > len(numbers) + 2:
|
||||
with contextlib.suppress(ValueError):
|
||||
max_percentage = float(sys.argv[len(numbers) + 2])
|
||||
max_pct = float(sys.argv[len(numbers) + 2])
|
||||
|
||||
randomized_numbers = randomize_numbers(numbers, min_percentage, max_percentage)
|
||||
randomized = randomize_numbers(numbers, min_pct, max_pct)
|
||||
formatted_numbers = []
|
||||
for i, num in enumerate(randomized_numbers):
|
||||
for i, num in enumerate(randomized):
|
||||
format_str = f".{decimal_counts[i]}f"
|
||||
formatted_numbers.append(float(format(num, format_str)))
|
||||
|
||||
@ -108,3 +110,7 @@ if __name__ == "__main__":
|
||||
_logger.exception("Error processing numbers")
|
||||
_logger.exception("Please provide valid numbers and percentages.")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@ -18,24 +18,8 @@ _logger = logging.getLogger(__name__)
|
||||
|
||||
REQUEST_TIMEOUT = 30 # seconds
|
||||
|
||||
# Initialize argument parser to accept the website URL as an argument
|
||||
parser = argparse.ArgumentParser(description="Download images from a comic website.")
|
||||
parser.add_argument(
|
||||
"url", type=str, help="The URL of the website to start downloading images from"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
# Initialize WebDriver (Use the appropriate driver for your browser)
|
||||
driver = webdriver.Chrome()
|
||||
|
||||
# Open the website from the passed argument
|
||||
url = args.url
|
||||
_logger.info("Opening the website: %s", url)
|
||||
driver.get(url)
|
||||
|
||||
|
||||
# A function to download images by URL
|
||||
def download_image(image_url: str) -> bool:
|
||||
def _download_image(image_url: str) -> bool:
|
||||
"""Download an image from a URL and save it locally."""
|
||||
# Extract image name from URL
|
||||
image_name = Path(urlparse(image_url).path).name
|
||||
@ -53,37 +37,58 @@ def download_image(image_url: str) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
# No need to define a specific number of images now
|
||||
count = 1
|
||||
def main() -> None:
|
||||
"""Download comic images from a website using Selenium."""
|
||||
# Initialize argument parser to accept the website URL as an argument
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Download images from a comic website."
|
||||
)
|
||||
parser.add_argument(
|
||||
"url", type=str, help="The URL of the website to start downloading images from"
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
while True:
|
||||
_logger.info("Processing image %s...", count)
|
||||
# Initialize WebDriver (Use the appropriate driver for your browser)
|
||||
driver = webdriver.Chrome()
|
||||
|
||||
# Find the image element by its ID
|
||||
image_element = driver.find_element(By.ID, "cc-comic")
|
||||
# Open the website from the passed argument
|
||||
_logger.info("Opening the website: %s", args.url)
|
||||
driver.get(args.url)
|
||||
|
||||
# Get the image URL from the 'src' attribute
|
||||
current_image_url = image_element.get_attribute("src")
|
||||
_logger.info("Found image URL: %s", current_image_url)
|
||||
image_count = 1
|
||||
|
||||
# Download the image if it doesn't already exist
|
||||
if download_image(current_image_url):
|
||||
count += 1 # Increment count only if the image was downloaded
|
||||
while True:
|
||||
_logger.info("Processing image %s...", image_count)
|
||||
|
||||
# Try to find the 'Next' button by its class
|
||||
try:
|
||||
_logger.info("Clicking the 'Next' button to load the next image...")
|
||||
next_button = driver.find_element(By.CSS_SELECTOR, "a.cc-next")
|
||||
# Find the image element by its ID
|
||||
image_element = driver.find_element(By.ID, "cc-comic")
|
||||
|
||||
# Navigate to the URL in the 'href' of the next button
|
||||
next_button_url = next_button.get_attribute("href")
|
||||
driver.get(next_button_url)
|
||||
# Get the image URL from the 'src' attribute
|
||||
current_image_url = image_element.get_attribute("src")
|
||||
_logger.info("Found image URL: %s", current_image_url)
|
||||
|
||||
except NoSuchElementException:
|
||||
# If the 'Next' button is not found, it means we've reached the last image
|
||||
_logger.info("No 'Next' button found. Reached the end of images.")
|
||||
break
|
||||
# Download the image if it doesn't already exist
|
||||
if _download_image(current_image_url):
|
||||
image_count += 1 # Increment count only if the image was downloaded
|
||||
|
||||
# Close the browser
|
||||
_logger.info("All images processed, closing the browser.")
|
||||
driver.quit()
|
||||
# Try to find the 'Next' button by its class
|
||||
try:
|
||||
_logger.info("Clicking the 'Next' button to load the next image...")
|
||||
next_button = driver.find_element(By.CSS_SELECTOR, "a.cc-next")
|
||||
|
||||
# Navigate to the URL in the 'href' of the next button
|
||||
next_button_url = next_button.get_attribute("href")
|
||||
driver.get(next_button_url)
|
||||
|
||||
except NoSuchElementException:
|
||||
# If the 'Next' button is not found, it means we've reached the last image
|
||||
_logger.info("No 'Next' button found. Reached the end of images.")
|
||||
break
|
||||
|
||||
# Close the browser
|
||||
_logger.info("All images processed, closing the browser.")
|
||||
driver.quit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user