Files
proxy-scraper-checker/proxy_scraper_checker/checker.py
T

83 lines
2.1 KiB
Python
Raw Normal View History

2024-01-21 17:45:01 +00:00
from __future__ import annotations
import asyncio
import logging
2024-07-09 03:08:30 +03:00
from typing import TYPE_CHECKING
2024-01-21 17:45:01 +00:00
2024-11-22 14:32:29 +03:00
from proxy_scraper_checker import sort
from proxy_scraper_checker.counter import IncrInt
2024-06-03 14:08:42 +05:00
if TYPE_CHECKING:
2024-10-17 14:18:27 +03:00
from collections.abc import Mapping
2024-07-09 03:08:30 +03:00
2024-06-03 14:08:42 +05:00
from aiohttp_socks import ProxyType
from rich.progress import Progress, TaskID
2024-11-22 14:32:29 +03:00
from proxy_scraper_checker.proxy import Proxy
from proxy_scraper_checker.settings import Settings
from proxy_scraper_checker.storage import ProxyStorage
2024-01-21 17:45:01 +00:00
2024-11-06 15:05:34 +03:00
_logger = logging.getLogger(__name__)
2024-01-21 17:45:01 +00:00
async def check_one(
*,
counter: IncrInt,
2024-01-21 17:45:01 +00:00
progress: Progress,
proxy: Proxy,
settings: Settings,
storage: ProxyStorage,
task: TaskID,
) -> None:
try:
await proxy.check(settings=settings)
except Exception as e:
# Too many open files
if isinstance(e, OSError) and e.errno == 24: # noqa: PLR2004
2024-11-06 15:05:34 +03:00
_logger.error("Please, set max_connections to lower value")
2024-01-21 17:45:01 +00:00
2024-11-06 15:05:34 +03:00
_logger.debug(
2024-01-21 17:45:01 +00:00
"%s.%s: %s", e.__class__.__module__, e.__class__.__qualname__, e
)
storage.remove(proxy)
else:
counter.incr()
progress.update(task_id=task, advance=1, successful_count=counter.value)
2024-01-21 17:45:01 +00:00
async def check_all(
*,
settings: Settings,
storage: ProxyStorage,
progress: Progress,
proxies_count: Mapping[ProxyType, int],
) -> None:
counters = {
proto: IncrInt()
for proto in sort.PROTOCOL_ORDER
if proto in storage.enabled_protocols
}
2024-01-25 11:49:22 +03:00
progress_tasks = {
2024-01-21 17:45:01 +00:00
proto: progress.add_task(
2024-01-25 11:49:22 +03:00
description="",
2024-01-21 17:45:01 +00:00
total=proxies_count[proto],
module="Checker",
protocol=proto.name,
successful_count=0,
2024-01-21 17:45:01 +00:00
)
for proto in counters
2024-01-21 17:45:01 +00:00
}
2024-01-25 10:30:40 +03:00
await asyncio.gather(
*(
check_one(
counter=counters[proxy.protocol],
2024-01-25 10:30:40 +03:00
progress=progress,
proxy=proxy,
settings=settings,
storage=storage,
2024-01-25 11:49:22 +03:00
task=progress_tasks[proxy.protocol],
2024-01-25 10:30:40 +03:00
)
for proxy in storage
2024-01-21 17:45:01 +00:00
)
2024-01-25 10:30:40 +03:00
)