diff --git a/tests/test_commons_candidates.py b/tests/test_commons_candidates.py index a4f1ccc..37221a4 100644 --- a/tests/test_commons_candidates.py +++ b/tests/test_commons_candidates.py @@ -77,6 +77,17 @@ class CandidateTest(unittest.TestCase): self.assertIn("not a photo", why) +class SeasonDateTest(unittest.TestCase): + def test_an_undated_season_photo_takes_its_seasons_year_and_says_so(self): + c = cc.dated_by_season({"file": "Kubica spa.jpg", "date": None}, 2008) + self.assertEqual((c["date"], c["date_from"]), ("2008", "category")) + + def test_a_dated_season_photo_keeps_its_own_date(self): + c = cc.dated_by_season({"file": "A.jpg", "date": "2009-05-01"}, 2008) + self.assertEqual(c["date"], "2009-05-01") + self.assertNotIn("date_from", c) + + class AerialTest(unittest.TestCase): def test_a_search_hit_is_aerial_only_when_commons_says_so(self): def hit(file, cats, found): diff --git a/tools/commons_candidates.py b/tools/commons_candidates.py index 5e49061..fe7fb8e 100644 --- a/tools/commons_candidates.py +++ b/tools/commons_candidates.py @@ -16,7 +16,8 @@ A search hit counts as an aerial photo only when its title or a category says so word in its description alone matched hundreds of race photos taken from the ground. A candidate holds the fields of a pick (file, licence, author, credit, date) and what the picker shows beside them (thumb, page, width, height, marks, restrictions, found). A layout's key is its path under circuits/ without an extension, the key circuits/photos.json -uses; a season's is its year. +uses; a season's is its year. A season candidate whose file gives no date takes the season's year, marked +"date_from": "category". Commons' rules, kept here: a descriptive User-Agent with the app's address and no person's; one API request at a time and at most five a second (a request starts at least REQUEST_GAP after the one before); a 429 or a maxlag @@ -393,6 +394,15 @@ def says_aerial(c: dict) -> bool: return len(c["found"]) >= 2 +def dated_by_season(c: dict, year: int) -> dict: + """A season candidate is of its season: one with no date of its own takes the season's year, and says so. (A + layout candidate with no date stays out: a circuit's photo may show any of its layouts.)""" + if not c["date"]: + c["date"] = str(year) + c["date_from"] = "category" + return c + + def in_years(c: dict, first: int, last: int) -> bool: return bool(c["date"]) and first <= int(c["date"][:4]) <= last @@ -447,7 +457,7 @@ def main() -> int: if not c: skipped[why] = skipped.get(why, 0) + 1 elif c["file"] not in found: - found[c["file"]] = c + found[c["file"]] = dated_by_season(c, year) out_seasons[str(year)] = [public(c) for c in sorted(found.values(), key=rank_key)[:SEASON_KEEP]] print(f"season {year}: {len(found)} licensed photo(s), kept {len(out_seasons[str(year)])} " f"({fetch.count} requests)", file=sys.stderr)