From 490efee372eb1fc89e8a544fdea66d0b56d5dd60 Mon Sep 17 00:00:00 2001 From: monosans Date: Mon, 6 Feb 2023 20:59:03 +0300 Subject: [PATCH] Add Debug option --- config.ini | 3 +++ proxy_scraper_checker/__main__.py | 19 +++++++++++++----- .../proxy_scraper_checker.py | 20 ++++++++++++++----- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/config.ini b/config.ini index bb97f20..cdf2ac3 100644 --- a/config.ini +++ b/config.ini @@ -29,6 +29,9 @@ SortBySpeed = yes ; Leave empty to save the proxies to the current directory. SavePath = +; Enable output of all errors +Debug = no + [Folders] ; This section is ignored if CheckWebsite is not 'default'. diff --git a/proxy_scraper_checker/__main__.py b/proxy_scraper_checker/__main__.py index bced22f..efc1e61 100644 --- a/proxy_scraper_checker/__main__.py +++ b/proxy_scraper_checker/__main__.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio import logging import sys +from configparser import ConfigParser import rich.traceback from rich.console import Console @@ -26,10 +27,10 @@ def set_event_loop_policy() -> None: uvloop.install() -def configure_logging(console: Console) -> None: +def configure_logging(console: Console, *, debug: bool) -> None: rich.traceback.install(console=console) logging.basicConfig( - level=logging.INFO, + level=logging.DEBUG if debug else logging.INFO, format="%(message)s", datefmt="%Y-%m-%d %H:%M:%S", handlers=( @@ -43,13 +44,21 @@ def configure_logging(console: Console) -> None: ) +def get_config(file: str) -> ConfigParser: + cfg = ConfigParser(interpolation=None) + cfg.read(file, encoding="utf-8") + return cfg + + def main() -> None: set_event_loop_policy() - console = Console() - configure_logging(console) + cfg = get_config("config.ini") - psc = ProxyScraperChecker.from_ini("config.ini", console=console) + console = Console() + configure_logging(console, debug=cfg["General"].getboolean("Debug", False)) + + psc = ProxyScraperChecker.from_configparser(cfg, console=console) asyncio.run(psc.run()) diff --git a/proxy_scraper_checker/proxy_scraper_checker.py b/proxy_scraper_checker/proxy_scraper_checker.py index 75ee356..83cbbbb 100644 --- a/proxy_scraper_checker/proxy_scraper_checker.py +++ b/proxy_scraper_checker/proxy_scraper_checker.py @@ -173,14 +173,12 @@ class ProxyScraperChecker: self.sem = asyncio.Semaphore(max_connections) @classmethod - def from_ini( + def from_configparser( cls: Type[TProxyScraperChecker], - file_name: str, + cfg: ConfigParser, *, console: Optional[Console] = None, ) -> TProxyScraperChecker: - cfg = ConfigParser(interpolation=None) - cfg.read(file_name, encoding="utf-8") general = cfg["General"] folders = cfg["Folders"] http = cfg["HTTP"] @@ -261,7 +259,13 @@ class ProxyScraperChecker: status = response.status text = await response.text() except Exception as e: - logger.error("%s | %s | %s", source, e.__class__.__qualname__, e) + logger.error( + "%s | %s.%s | %s", + source, + e.__class__.__module__, + e.__class__.__qualname__, + e, + ) else: proxies = tuple(self.regex.finditer(text)) if proxies: @@ -300,6 +304,12 @@ class ProxyScraperChecker: if isinstance(e, OSError) and e.errno == 24: logger.error("Please, set MaxConnections to lower value.") + logger.debug( + "%s.%s | %s", + e.__class__.__module__, + e.__class__.__qualname__, + e, + ) self.proxies[proto].remove(proxy) progress.update(task, advance=1)