12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- from aiohttp import ClientSession, TCPConnector
- from time import time
- from asyncio import gather
- import re
- from utils.config import get_config
- config = get_config()
- timeout = 15
- async def get_speed(url, timeout=timeout, proxy=None):
- """
- Get the speed of the url
- """
- async with ClientSession(
- connector=TCPConnector(verify_ssl=False), trust_env=True
- ) as session:
- start = time()
- end = None
- try:
- async with session.get(url, timeout=timeout, proxy=proxy) as response:
- resStatus = response.status
- if resStatus == 200:
- end = time()
- else:
- return float("inf")
- except Exception as e:
- return float("inf")
- return int(round((end - start) * 1000)) if end else float("inf")
- async def sort_urls_by_speed_and_resolution(infoList):
- """
- Sort by speed and resolution
- """
- response_times = await gather(*(get_speed(url) for url, _, _ in infoList))
- valid_responses = [(info, rt) for info, rt in zip(infoList, response_times)]
- def extract_resolution(resolution_str):
- numbers = re.findall(r"\d+x\d+", resolution_str)
- if numbers:
- width, height = map(int, numbers[0].split("x"))
- return width * height
- else:
- return 0
- default_response_time_weight = 0.5
- default_resolution_weight = 0.5
- response_time_weight = getattr(
- config, "response_time_weight", default_response_time_weight
- )
- resolution_weight = getattr(config, "resolution_weight", default_resolution_weight)
- # Check if weights are valid
- if not (
- 0 <= response_time_weight <= 1
- and 0 <= resolution_weight <= 1
- and response_time_weight + resolution_weight == 1
- ):
- response_time_weight = default_response_time_weight
- resolution_weight = default_resolution_weight
- def combined_key(item):
- (_, _, resolution), response_time = item
- resolution_value = extract_resolution(resolution) if resolution else 0
- return (
- -(response_time_weight * response_time)
- + resolution_weight * resolution_value
- )
- sorted_res = sorted(valid_responses, key=combined_key, reverse=True)
- return sorted_res
|