Commit
·
70b8860
1
Parent(s):
c90fceb
Use WB scaled-down images
Browse files
app.py
CHANGED
@@ -83,10 +83,10 @@ def get_additional_images(id_product_money: str, marketplace: str) -> List[str]:
|
|
83 |
if result and result.more_images:
|
84 |
print(f"Wildberries raw more_images: {result.more_images}")
|
85 |
try:
|
86 |
-
|
87 |
-
if isinstance(
|
88 |
-
#
|
89 |
-
return
|
90 |
return []
|
91 |
except Exception as e:
|
92 |
print(f"Error parsing JSON: {str(e)}")
|
@@ -97,25 +97,33 @@ def get_additional_images(id_product_money: str, marketplace: str) -> List[str]:
|
|
97 |
|
98 |
def try_scaled_image_url(client: httpx.Client, url: str, marketplace: str, max_retries: int = 3) -> str:
|
99 |
"""Try to get a scaled version of the image URL, fall back to original if not available."""
|
|
|
|
|
100 |
if marketplace == 'lamoda':
|
101 |
scaled_url = url.replace('product', 'img600x866')
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
118 |
return url
|
|
|
|
|
|
|
|
|
119 |
return url
|
120 |
|
121 |
def download_and_encode_images(image_urls: List[str], marketplace: str) -> List[Dict]:
|
|
|
83 |
if result and result.more_images:
|
84 |
print(f"Wildberries raw more_images: {result.more_images}")
|
85 |
try:
|
86 |
+
data = json.loads(result.more_images)
|
87 |
+
if isinstance(data, list):
|
88 |
+
# Extract image URLs from the JSON structure
|
89 |
+
return [item.get('image_url') for item in data if item.get('image_url')]
|
90 |
return []
|
91 |
except Exception as e:
|
92 |
print(f"Error parsing JSON: {str(e)}")
|
|
|
97 |
|
98 |
def try_scaled_image_url(client: httpx.Client, url: str, marketplace: str, max_retries: int = 3) -> str:
|
99 |
"""Try to get a scaled version of the image URL, fall back to original if not available."""
|
100 |
+
scaled_url = url
|
101 |
+
|
102 |
if marketplace == 'lamoda':
|
103 |
scaled_url = url.replace('product', 'img600x866')
|
104 |
+
elif marketplace == 'wildberries':
|
105 |
+
scaled_url = url.replace('/big/', '/c516x688/')
|
106 |
+
else:
|
107 |
+
return url # No scaling for other marketplaces
|
108 |
+
|
109 |
+
for attempt in range(max_retries):
|
110 |
+
try:
|
111 |
+
response = client.get(scaled_url, timeout=5.0)
|
112 |
+
if response.status_code == 200:
|
113 |
+
print(f"Using scaled image: {scaled_url}")
|
114 |
+
return scaled_url
|
115 |
+
else:
|
116 |
+
print(f"Scaled image not available (status {response.status_code}), using original: {url}")
|
117 |
+
return url
|
118 |
+
except httpx.TimeoutException:
|
119 |
+
print(f"Timeout checking scaled image (attempt {attempt + 1}/{max_retries})")
|
120 |
+
if attempt == max_retries - 1:
|
121 |
+
print(f"Max retries reached, using original: {url}")
|
122 |
return url
|
123 |
+
except Exception as e:
|
124 |
+
print(f"Error checking scaled image: {type(e).__name__}: {str(e)}")
|
125 |
+
return url
|
126 |
+
|
127 |
return url
|
128 |
|
129 |
def download_and_encode_images(image_urls: List[str], marketplace: str) -> List[Dict]:
|
test.py
CHANGED
@@ -82,10 +82,10 @@ def get_additional_images(id_product_money: str, marketplace: str) -> List[str]:
|
|
82 |
if result and result.more_images:
|
83 |
print(f"Wildberries raw more_images: {result.more_images}")
|
84 |
try:
|
85 |
-
|
86 |
-
if isinstance(
|
87 |
-
#
|
88 |
-
return
|
89 |
return []
|
90 |
except Exception as e:
|
91 |
print(f"Error parsing JSON: {str(e)}")
|
@@ -96,25 +96,33 @@ def get_additional_images(id_product_money: str, marketplace: str) -> List[str]:
|
|
96 |
|
97 |
def try_scaled_image_url(client: httpx.Client, url: str, marketplace: str, max_retries: int = 3) -> str:
|
98 |
"""Try to get a scaled version of the image URL, fall back to original if not available."""
|
|
|
|
|
99 |
if marketplace == 'lamoda':
|
100 |
scaled_url = url.replace('product', 'img600x866')
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
|
|
|
|
117 |
return url
|
|
|
|
|
|
|
|
|
118 |
return url
|
119 |
|
120 |
def download_and_encode_images(image_urls: List[str], marketplace: str) -> List[Dict]:
|
|
|
82 |
if result and result.more_images:
|
83 |
print(f"Wildberries raw more_images: {result.more_images}")
|
84 |
try:
|
85 |
+
data = json.loads(result.more_images)
|
86 |
+
if isinstance(data, list):
|
87 |
+
# Extract image URLs from the JSON structure
|
88 |
+
return [item.get('image_url') for item in data if item.get('image_url')]
|
89 |
return []
|
90 |
except Exception as e:
|
91 |
print(f"Error parsing JSON: {str(e)}")
|
|
|
96 |
|
97 |
def try_scaled_image_url(client: httpx.Client, url: str, marketplace: str, max_retries: int = 3) -> str:
|
98 |
"""Try to get a scaled version of the image URL, fall back to original if not available."""
|
99 |
+
scaled_url = url
|
100 |
+
|
101 |
if marketplace == 'lamoda':
|
102 |
scaled_url = url.replace('product', 'img600x866')
|
103 |
+
elif marketplace == 'wildberries':
|
104 |
+
scaled_url = url.replace('/big/', '/c516x688/')
|
105 |
+
else:
|
106 |
+
return url # No scaling for other marketplaces
|
107 |
+
|
108 |
+
for attempt in range(max_retries):
|
109 |
+
try:
|
110 |
+
response = client.get(scaled_url, timeout=5.0)
|
111 |
+
if response.status_code == 200:
|
112 |
+
print(f"Using scaled image: {scaled_url}")
|
113 |
+
return scaled_url
|
114 |
+
else:
|
115 |
+
print(f"Scaled image not available (status {response.status_code}), using original: {url}")
|
116 |
+
return url
|
117 |
+
except httpx.TimeoutException:
|
118 |
+
print(f"Timeout checking scaled image (attempt {attempt + 1}/{max_retries})")
|
119 |
+
if attempt == max_retries - 1:
|
120 |
+
print(f"Max retries reached, using original: {url}")
|
121 |
return url
|
122 |
+
except Exception as e:
|
123 |
+
print(f"Error checking scaled image: {type(e).__name__}: {str(e)}")
|
124 |
+
return url
|
125 |
+
|
126 |
return url
|
127 |
|
128 |
def download_and_encode_images(image_urls: List[str], marketplace: str) -> List[Dict]:
|