This commit is contained in:
2025-05-07 08:17:22 +10:00
parent dbe76795c2
commit ad21bac601
2 changed files with 70 additions and 30 deletions
+8 -8
View File
@@ -447,22 +447,22 @@ def upload_image_to_wp(image_url, post_title, wp_base_url, wp_username, wp_passw
}
logging.info(f"Fetching image from {image_url} for '{post_title}'")
for attempt in range(MAX_RETRIES):
for attempt in range(3):
try:
image_response = requests.get(image_url, headers=image_headers, timeout=IMAGE_UPLOAD_TIMEOUT)
if image_response.status_code == 429:
wait_time = RETRY_BACKOFF * (2 ** attempt)
logging.warning(f"Rate limit hit for {image_url}. Retrying after {wait_time}s (attempt {attempt+1}/{MAX_RETRIES}).")
wait_time = 10 * (2 ** attempt)
logging.warning(f"Rate limit hit for {image_url}. Retrying after {wait_time}s (attempt {attempt+1}/3).")
time.sleep(wait_time)
continue
image_response.raise_for_status()
break
except requests.exceptions.RequestException as e:
logging.warning(f"Image fetch failed for {image_url} (attempt {attempt+1}/{MAX_RETRIES}): {e}")
if attempt == MAX_RETRIES - 1:
logging.error(f"Failed to fetch image {image_url} after {MAX_RETRIES} attempts")
logging.warning(f"Image fetch failed for {image_url} (attempt {attempt+1}/3): {e}")
if attempt == 2:
logging.error(f"Failed to fetch image {image_url} after 3 attempts")
return None
time.sleep(RETRY_BACKOFF * (2 ** attempt))
time.sleep(10 * (2 ** attempt))
else:
logging.error(f"Failed to fetch image {image_url} after retries")
return None
@@ -490,7 +490,7 @@ def upload_image_to_wp(image_url, post_title, wp_base_url, wp_username, wp_passw
logging.info(f"Uploaded image '{safe_title}.jpg' to WP (ID: {image_id}) with caption '{caption}'")
return image_id
except Exception as e:
logging.error(f"Image upload to WP failed for '{post_title}': {e}", exc_info=True)
logging.error(f"Image upload to WP failed for '{post_title}': {e}")
print(f"Image upload to WP failed for '{post_title}': {e}")
return None