fix image upload issue

This commit is contained in:
2025-05-07 07:50:54 +10:00
parent 028dfc3fc8
commit 4368bf68a5
2 changed files with 75 additions and 63 deletions
+13 -13
View File
@@ -448,25 +448,24 @@ 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(3):
for attempt in range(MAX_RETRIES):
try:
image_response = requests.get(image_url, headers=image_headers, timeout=10)
image_response = requests.get(image_url, headers=image_headers, timeout=IMAGE_UPLOAD_TIMEOUT)
if image_response.status_code == 429:
wait_time = 10 * (2 ** attempt)
logging.warning(f"Rate limit hit for {image_url}. Retrying after {wait_time}s (attempt {attempt+1}/3).")
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}).")
time.sleep(wait_time)
continue
image_response.raise_for_status()
break
except requests.exceptions.HTTPError as e:
if e.response.status_code == 429:
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
raise
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")
return None
time.sleep(RETRY_BACKOFF * (2 ** attempt))
else:
logging.warning(f"Rate limit hit for {image_url} after retries. Failing image upload.")
logging.error(f"Failed to fetch image {image_url} after retries")
return None
response = requests.post(
@@ -492,7 +491,8 @@ 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}")
logging.error(f"Image upload to WP failed for '{post_title}': {e}", exc_info=True)
print(f"Image upload to WP failed for '{post_title}': {e}")
return None
def determine_paragraph_count(interest_score):