-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetImage.py
More file actions
65 lines (54 loc) · 2.45 KB
/
Copy pathGetImage.py
File metadata and controls
65 lines (54 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from curl_cffi.requests import AsyncSession
import io
from Atlanticexpress import atlanticexpress_img
from Auctionhistory import auctionhistory_img
from Bid import bid_img
from Autotorgby import autotorgby_img
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(name)s - %(message)s'
)
async def get_image(vin):
resource_functions = {
"autotorgby": autotorgby_img,
"auctionhistory": auctionhistory_img,
"bid": bid_img,
"atlanticexpress": atlanticexpress_img,
}
images_urls = []
for resource_name, resource_function in resource_functions.items():
logger.info(f"Обрабатывается ресурс: {resource_name}")
result = await resource_function(vin)
current_images_urls, current_lot_url = result
if current_images_urls:
logger.info(f"Найдено {len(current_images_urls)} изображений на ресурсе {resource_name}.")
images_urls.extend(current_images_urls)
lot_url = current_lot_url
break
if not images_urls:
logger.warning(f"Изображения для VIN {vin} не найдены ни на одном ресурсе.")
lot_url = current_lot_url
return [], lot_url
try:
async with AsyncSession() as client:
images = []
for idx, img_url in enumerate(images_urls):
try:
img_response = await client.get(img_url, impersonate="edge101")
if img_response.status_code == 200:
if len(img_response.content) > 0:
image_buffer = io.BytesIO(img_response.content)
image_buffer.seek(0)
images.append(image_buffer)
else:
logger.warning(f"Изображение пустое: {img_url}")
else:
logger.error(f"Ошибка загрузки изображения {img_url}: статус {img_response.status_code}")
except Exception as e:
logger.error(f"Ошибка при обработке изображения {img_url}: {e}")
except Exception as e:
logger.error(f"Ошибка при запросе: {e}")
return [], lot_url
return images, lot_url