Skip to content

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

Adjust Flickr max records to account for incorrect reporting (#1031)

Staci Mullins2d038c9a8017
diff --git a/openverse_catalog/dags/providers/provider_api_scripts/flickr.py b/openverse_catalog/dags/providers/provider_api_scripts/flickr.py
index 6f7f97339..74235295d 100644
--- a/openverse_catalog/dags/providers/provider_api_scripts/flickr.py
+++ b/openverse_catalog/dags/providers/provider_api_scripts/flickr.py
@@ -50,7 +50,11 @@ class FlickrDataIngester(TimeDelineatedProviderDataIngester):
batch_limit = 500
retries = 5
- max_records = 4_000
+ # Flickr will return a maximum of 4,000 unique records per query. In practice,
+ # we often see more records returned for a query than the given `resultCount`, so
+ # we cap `max_records` artificially low to accommodate this.
+ max_unique_records = 4_000 # Actual maximum, above which we should raise an error
+ max_records = 3_000 # Attempt to break ingestion into time slices of this size
division_threshold = 20_000
min_divisions = 12
max_divisions = 60
@@ -258,10 +262,10 @@ class FlickrDataIngester(TimeDelineatedProviderDataIngester):
# Call the parent method in order to update the fetched_count
should_continue = super().get_should_continue(response_json)
- # Return early if more than the max_records have been ingested.
+ # Return early if more than the maximum unique records have been ingested.
# This could happen if we did not break the ingestion down into
# small enough divisions.
- if self.fetched_count > self.max_records:
+ if self.fetched_count > self.max_unique_records:
raise AirflowException(
f"{self.fetched_count} records retrieved, but there is a"
f" limit of {self.max_records}. Consider increasing the"
diff --git a/tests/dags/providers/provider_api_scripts/test_flickr.py b/tests/dags/providers/provider_api_scripts/test_flickr.py
index 3f436d6fe..7167dec32 100644
--- a/tests/dags/providers/provider_api_scripts/test_flickr.py
+++ b/tests/dags/providers/provider_api_scripts/test_flickr.py
@@ -1,7 +1,9 @@
import json
import os
+from unittest import mock
import pytest
+from airflow.exceptions import AirflowException
from common.licenses import LicenseInfo
from providers.provider_api_scripts.flickr import FlickrDataIngester
@@ -69,6 +71,35 @@ def test_get_record_count_from_response():
assert count == 30
+ "fetched_count, super_should_continue, expected_should_continue",
+ (
+ (1000, True, True),
+ # Return False if super().get_should_continue() is False
+ (1000, False, False),
+ (4000, True, True),
+ # Raise exception if fetched_count exceeds max_unique_records
+ pytest.param(
+ 4001,
+ True,
+ False,
+ marks=pytest.mark.raises(exception=AirflowException),
+ ),
+ ),
+)
+def test_get_should_continue(
+ fetched_count, super_should_continue, expected_should_continue
+):
+ with mock.patch(
+ "providers.provider_api_scripts.time_delineated_provider_data_ingester.TimeDelineatedProviderDataIngester.get_should_continue",
+ return_value=super_should_continue,
+ ):
+ ingester = FlickrDataIngester(date=FROZEN_DATE)
+ ingester.fetched_count = fetched_count
+
+ assert ingester.get_should_continue({}) == expected_should_continue
+
+
@pytest.mark.parametrize(
"response_json, expected_response",
[