Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Treat any non 200 status as failure for thingiverse (#940)

sarayourfriendd311ec2e9b31
diff --git a/api/catalog/api/controllers/search_controller.py b/api/catalog/api/controllers/search_controller.py
index 9dc0f699e..b54612569 100644
--- a/api/catalog/api/controllers/search_controller.py
+++ b/api/catalog/api/controllers/search_controller.py
@@ -20,8 +20,8 @@ import catalog.api.models as models
from catalog.api.constants.sorting import INDEXED_ON
from catalog.api.serializers import media_serializers
from catalog.api.utils import tallies
+from catalog.api.utils.check_dead_links import check_dead_links
from catalog.api.utils.dead_link_mask import get_query_hash, get_query_mask
-from catalog.api.utils.validate_images import validate_images
ELASTICSEARCH_MAX_RESULT_WINDOW = 10000
@@ -179,7 +179,7 @@ def _post_process_results(
if filter_dead:
query_hash = get_query_hash(s)
- validate_images(query_hash, start, results, to_validate)
+ check_dead_links(query_hash, start, results, to_validate)
if len(results) == 0:
# first page is all dead links
diff --git a/api/catalog/api/utils/validate_images.py b/api/catalog/api/utils/check_dead_links.py
similarity index 92%
rename from api/catalog/api/utils/validate_images.py
rename to api/catalog/api/utils/check_dead_links.py
index 1bfcab7ac..7fa11db97 100644
--- a/api/catalog/api/utils/validate_images.py
+++ b/api/catalog/api/utils/check_dead_links.py
@@ -52,7 +52,7 @@ async def _make_head_requests(urls: list[str]) -> list[tuple[str, int]]:
return responses.result()
-def validate_images(
+def check_dead_links(
query_hash: str, start_slice: int, results: list[Hit], image_urls: list[str]
) -> None:
"""
@@ -64,7 +64,7 @@ def validate_images(
Results are cached in redis and shared amongst all API servers in the
cluster.
"""
- logger = parent_logger.getChild("validate_images")
+ logger = parent_logger.getChild("check_dead_links")
if not image_urls:
logger.info("no image urls to validate")
return
@@ -119,7 +119,14 @@ def validate_images(
for idx, _ in enumerate(cached_statuses):
del_idx = len(cached_statuses) - idx - 1
status = cached_statuses[del_idx]
- if status == 429 or status == 403:
+ # thingiverse treated as failure despite the suspect status code
+ # due to issues described here:
+ # https://github.com/WordPress/openverse/issues/900
+ if (
+ status == 429
+ or status == 403
+ and results[del_idx]["provider"] != "thingiverse"
+ ):
logger.warning(
"Image validation failed due to rate limiting or blocking. "
f"url={image_urls[idx]} "
diff --git a/api/test/audio_integration_test.py b/api/test/audio_integration_test.py
index 6a90b146e..d785b0f88 100644
--- a/api/test/audio_integration_test.py
+++ b/api/test/audio_integration_test.py
@@ -28,7 +28,7 @@ import pytest
import requests
from django_redis import get_redis_connection
-from catalog.api.utils.validate_images import CACHE_PREFIX
+from catalog.api.utils.check_dead_links import CACHE_PREFIX
@pytest.fixture
diff --git a/api/test/dead_link_filter_test.py b/api/test/dead_link_filter_test.py
index dbca58e65..27fb59a6a 100644
--- a/api/test/dead_link_filter_test.py
+++ b/api/test/dead_link_filter_test.py
@@ -57,13 +57,13 @@ def empty_validation_cache(monkeypatch):
return [None] * len(image_urls)
monkeypatch.setattr(
- "catalog.api.utils.validate_images._get_cached_statuses",
+ "catalog.api.utils.check_dead_links._get_cached_statuses",
get_empty_cached_statuses,
)
_MAKE_HEAD_REQUESTS_MODULE_PATH = (
- "catalog.api.utils.validate_images._make_head_requests"
+ "catalog.api.utils.check_dead_links._make_head_requests"
)
diff --git a/api/test/unit/utils/validate_images_test.py b/api/test/unit/utils/check_dead_links_test.py
similarity index 60%
rename from api/test/unit/utils/validate_images_test.py
rename to api/test/unit/utils/check_dead_links_test.py
index f0af832b1..fd9d229cf 100644
--- a/api/test/unit/utils/validate_images_test.py
+++ b/api/test/unit/utils/check_dead_links_test.py
@@ -4,7 +4,7 @@ from unittest import mock
import aiohttp
import pook
-from catalog.api.utils.validate_images import HEADERS, validate_images
+from catalog.api.utils.check_dead_links import HEADERS, check_dead_links
@mock.patch.object(aiohttp, "ClientSession", wraps=aiohttp.ClientSession)
@@ -22,7 +22,7 @@ def test_sends_user_agent(wrapped_client_session: mock.AsyncMock):
.mock
)
- validate_images(query_hash, start_slice, results, image_urls)
+ check_dead_links(query_hash, start_slice, results, image_urls)
assert head_mock.calls == len(results)
requested_urls = [req.rawurl for req in head_mock.matches]
@@ -50,9 +50,34 @@ def test_handles_timeout():
with mock.patch(
"aiohttp.client.ClientSession._request", side_effect=raise_timeout_error
):
- validate_images(query_hash, start_slice, results, image_urls)
+ check_dead_links(query_hash, start_slice, results, image_urls)
- # `validate_images` directly modifies the results list
+ # `check_dead_links` directly modifies the results list
# if the results are timing out then they're considered dead and discarded
# so should not appear in the final list of results.
assert len(results) == 0
+
+
+def test_thingiverse_403_considered_dead():
+ query_hash = "test_thingiverse_403_considered_dead"
+ results = [
+ {"identifier": i, "provider": "thingiverse" if i % 2 else "flickr"}
+ for i in range(4)
+ ]
+ image_urls = [f"https://example.org/{i}" for i in range(len(results))]
+ start_slice = 0
+
+ head_mock = (
+ pook.head(pook.regex(r"https://example.org/\d"))
+ .times(len(results))
+ .reply(403)
+ .mock
+ )
+
+ check_dead_links(query_hash, start_slice, results, image_urls)
+
+ assert head_mock.calls == len(results)
+
+ # All the "thingiverse" results should have been filtered out
+ # whereas the flickr results should be left
+ assert all([r["provider"] == "flickr" for r in results])