alexander-lazarin commited on
Commit
70b8860
·
1 Parent(s): c90fceb

Use WB scaled-down images

Browse files
Files changed (2) hide show
  1. app.py +28 -20
  2. test.py +28 -20
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
- urls = json.loads(result.more_images)
87
- if isinstance(urls, list) and len(urls) > 0:
88
- # Split the URLs by semicolons
89
- return urls[0].split(';')
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
- for attempt in range(max_retries):
103
- try:
104
- response = client.get(scaled_url, timeout=5.0)
105
- if response.status_code == 200:
106
- print(f"Using scaled image: {scaled_url}")
107
- return scaled_url
108
- else:
109
- print(f"Scaled image not available (status {response.status_code}), using original: {url}")
110
- return url
111
- except httpx.TimeoutException:
112
- print(f"Timeout checking scaled image (attempt {attempt + 1}/{max_retries})")
113
- if attempt == max_retries - 1:
114
- print(f"Max retries reached, using original: {url}")
115
- return url
116
- except Exception as e:
117
- print(f"Error checking scaled image: {type(e).__name__}: {str(e)}")
 
 
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
- urls = json.loads(result.more_images)
86
- if isinstance(urls, list) and len(urls) > 0:
87
- # Split the URLs by semicolons
88
- return urls[0].split(';')
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
- for attempt in range(max_retries):
102
- try:
103
- response = client.get(scaled_url, timeout=5.0)
104
- if response.status_code == 200:
105
- print(f"Using scaled image: {scaled_url}")
106
- return scaled_url
107
- else:
108
- print(f"Scaled image not available (status {response.status_code}), using original: {url}")
109
- return url
110
- except httpx.TimeoutException:
111
- print(f"Timeout checking scaled image (attempt {attempt + 1}/{max_retries})")
112
- if attempt == max_retries - 1:
113
- print(f"Max retries reached, using original: {url}")
114
- return url
115
- except Exception as e:
116
- print(f"Error checking scaled image: {type(e).__name__}: {str(e)}")
 
 
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]: