main.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import os
  2. import time
  3. import random
  4. import asyncio
  5. import colorama
  6. from colorama.ansi import Fore
  7. from user_agent.base import generate_user_agent
  8. from aiohttp import ClientSession
  9. from aiohttp_socks import ProxyConnector, ProxyType
  10. from typing import Iterable
  11. from proxyORM import Proxy
  12. from parse import ParseProxyPattern, parse_search, judges_by_proxy, ResponseSerch
  13. from input_output import read_rows, write_file
  14. from config import REQUEST_COUNT, DIR_WORK
  15. async def check(proxy:Proxy) -> Proxy|None:
  16. async with ClientSession(connector=ProxyConnector(
  17. proxy_type=proxy.proxy_type,
  18. host=proxy.host,
  19. port=proxy.port,
  20. username=proxy.username,
  21. password=proxy.password)) as session:
  22. start_time = time.perf_counter()
  23. async with session.get(
  24. url=random.choice(judges_by_proxy),
  25. headers= {
  26. 'User-Agent': generate_user_agent()},
  27. timeout=20) as response:
  28. if response.ok:
  29. proxy.status = True
  30. html_obj = (await response.text())
  31. proxy.timeout = time.perf_counter() - start_time
  32. if ip := parse_search(ResponseSerch.host, html_obj):
  33. proxy.anonymous = True if proxy.host == ip else False
  34. proxy.print_log_result()
  35. return proxy
  36. else:
  37. return response.raise_for_status()
  38. async def choice_proxy_type(proxy:Proxy):
  39. global REQUEST_COUNT
  40. if proxy.proxy_type:
  41. REQUEST_COUNT += 1
  42. try:
  43. return await check(proxy=proxy)
  44. except Exception:
  45. proxy.status = False
  46. proxy.print_log_result()
  47. return proxy
  48. else:
  49. for proxy_type in ProxyType:
  50. REQUEST_COUNT += 1
  51. try:
  52. proxy.proxy_type = proxy_type
  53. return await check(proxy=proxy)
  54. except Exception:
  55. continue
  56. proxy.status = False
  57. proxy.print_log_result()
  58. return proxy
  59. async def soft(proxy_iters:Iterable) -> None:
  60. tasks = []
  61. for proxy in proxy_iters:
  62. task = asyncio.create_task(
  63. choice_proxy_type(proxy=Proxy(
  64. proxy_type_=parse_search(ParseProxyPattern.proxy_type, proxy),
  65. host=parse_search(ParseProxyPattern.host, proxy),
  66. port=parse_search(ParseProxyPattern.port, proxy),
  67. username=parse_search(ParseProxyPattern.username, proxy),
  68. password=parse_search(ParseProxyPattern.password, proxy))))
  69. tasks.append(task)
  70. results = await asyncio.gather(*tasks)
  71. write_file(
  72. pathfile=DIR_WORK + '/good.txt',
  73. write=(
  74. proxy.proxy_string() + '\n' for proxy in results if proxy.status))
  75. def main() -> None:
  76. start = time.time()
  77. asyncio.run(soft(proxy_iters=read_rows(
  78. pathfile=DIR_WORK + '/check.txt')))
  79. print(f'{colorama.Fore.CYAN + "Время работы" + colorama.Fore.YELLOW + ":"}\
  80. {colorama.Fore.MAGENTA + str(time.time() - start)}' + colorama.Fore.RESET)
  81. print(Fore.BLUE + f"Количество сделанных запроса: {Fore.MAGENTA + str(REQUEST_COUNT)}")
  82. if __name__ == '__main__':
  83. main()