52 lines
2.4 KiB
Python
52 lines
2.4 KiB
Python
import os
|
|
import requests
|
|
|
|
class ImageDownloader:
|
|
def __init__(self, temp_folder, logger):
|
|
self.temp_folder = temp_folder
|
|
self.logger = logger
|
|
os.makedirs(temp_folder, exist_ok=True)
|
|
self.logger.info(f"ImageDownloader Start in {self.temp_folder}")
|
|
|
|
self.headers = {
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36",
|
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
|
|
"Accept-Language": "en-US,en;q=0.9",
|
|
"Accept-Encoding": "gzip, deflate, br",
|
|
"DNT": "1", # Do Not Track 요청 헤더
|
|
"Connection": "keep-alive",
|
|
"Upgrade-Insecure-Requests": "1",
|
|
"Cache-Control": "max-age=0"
|
|
}
|
|
|
|
def download_image(self, image_url, product_id):
|
|
# 임시 폴더에 이미지 다운로드
|
|
try:
|
|
image_path = os.path.join(self.temp_folder, f"{product_id}.jpg")
|
|
response = requests.get(image_url, headers=self.headers, stream=True)
|
|
if response.status_code == 200:
|
|
with open(image_path, 'wb') as file:
|
|
file.write(response.content)
|
|
self.logger.info(f"Success download image from {image_url}")
|
|
return image_path
|
|
except Exception as e:
|
|
self.logger.warning(f"Failed to download image: {e}", exc_info=True)
|
|
|
|
|
|
def download_image_for_searchResult(self, img_url, product_id, index):
|
|
"""이미지 URL에서 이미지를 다운로드하고 로컬 경로를 반환합니다."""
|
|
try:
|
|
response = requests.get(img_url, stream=True)
|
|
if response.status_code == 200:
|
|
file_path = os.path.join(self.temp_folder, f"{product_id}_{index}.jpg")
|
|
with open(file_path, "wb") as file:
|
|
for chunk in response.iter_content(1024):
|
|
file.write(chunk)
|
|
self.logger.info(f"Downloaded image for product ID {product_id} at index {index}")
|
|
return file_path
|
|
else:
|
|
self.logger.warning(f"Failed to download image from {img_url}, status code {response.status_code}")
|
|
except Exception as e:
|
|
self.logger.error(f"Error downloading image for search result: {e}")
|
|
return None
|