A photo filed under "<year> Formula One season cars" is of that season, and the service refuses a pick without a date: a season candidate whose file gives none takes the season's year, marked "date_from": "category". An undated layout candidate still stays out. Claude-Session: https://claude.ai/code/session_01NcbgfJAa38xFpzy69BWrqh
117 lines
6.0 KiB
Python
117 lines
6.0 KiB
Python
"""The candidate tool reads a Commons file's date, licence and credit as docs/posters.md asks, and ranks marks first."""
|
|
import importlib.util
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
_spec = importlib.util.spec_from_file_location("commons_candidates", ROOT / "tools" / "commons_candidates.py")
|
|
cc = importlib.util.module_from_spec(_spec)
|
|
_spec.loader.exec_module(cc)
|
|
|
|
|
|
def page(title="File:A.jpg", mime="image/jpeg", width=3000, height=2000, **meta):
|
|
"""One page of an imageinfo answer, as the Action API gives it (formatversion 2)."""
|
|
return {"title": title, "imageinfo": [{
|
|
"mime": mime, "width": width, "height": height,
|
|
"thumburl": "https://upload.wikimedia.org/x/500px-A.jpg?utm_source=commons.wikimedia.org",
|
|
"descriptionurl": "https://commons.wikimedia.org/wiki/File:A.jpg",
|
|
"extmetadata": {k: {"value": v} for k, v in meta.items()},
|
|
}]}
|
|
|
|
|
|
class PhotoDateTest(unittest.TestCase):
|
|
def test_forms_seen_on_commons(self):
|
|
cases = {
|
|
"2018-04-22": "2018-04-22",
|
|
"2008-06-26 18:57:32": "2008-06-26",
|
|
"1983-01": "1983-01",
|
|
"Taken on 29 December 2016, 12:19:34": "2016-12-29",
|
|
"28 August 2011, 13:44 (according to Exif data)": "2011-08-28",
|
|
'<time class="dtstart" datetime="2016-12-29">29 December 2016</time>': "2016-12-29",
|
|
"circa 1930date QS:P,+1930-00-00T00:00:00Z/9": "1930",
|
|
"07-06-1954": "1954", # day and month order unknown: the year only
|
|
}
|
|
for raw, want in cases.items():
|
|
with self.subTest(raw=raw):
|
|
self.assertEqual(cc.photo_date(raw), want)
|
|
|
|
def test_what_does_not_say_when_it_was_taken(self):
|
|
for raw in ["1 December 2006 (original upload date)", "1960s", "between 1950 and 1955", ""]:
|
|
with self.subTest(raw=raw):
|
|
self.assertIsNone(cc.photo_date(raw))
|
|
|
|
|
|
class CandidateTest(unittest.TestCase):
|
|
def test_a_free_file_becomes_a_pick_with_the_designs_credit(self):
|
|
c, why = cc.candidate(page(LicenseShortName="CC BY-SA 2.0", License="cc-by-sa-2.0",
|
|
Artist='<a href="https://www.flickr.com/x">Nathanael Majoros</a>',
|
|
DateTimeOriginal="2008-06-26 18:57:32", Assessments="quality|featured"), "search: aerial")
|
|
self.assertEqual(why, "")
|
|
self.assertEqual(c["file"], "A.jpg")
|
|
self.assertEqual(c["author"], "Nathanael Majoros")
|
|
self.assertEqual(c["credit"], "Photo: Nathanael Majoros, CC BY-SA 2.0, via Wikimedia Commons")
|
|
self.assertEqual(c["date"], "2008-06-26")
|
|
self.assertEqual(c["marks"], ["quality", "featured"])
|
|
self.assertNotIn("utm_", c["thumb"])
|
|
|
|
def test_attribution_asked_by_the_source_wins_over_the_artist(self):
|
|
c, _ = cc.candidate(page(LicenseShortName="CC BY-SA 3.0 de", License="cc-by-sa-3.0-de", Artist="Engelbert Reineke",
|
|
Attribution="Bundesarchiv, B 145 Bild-F029226-0006 / Engelbert Reineke / CC-BY-SA 3.0"), "x")
|
|
self.assertEqual(c["author"], "Bundesarchiv, B 145 Bild-F029226-0006 / Engelbert Reineke / CC-BY-SA 3.0")
|
|
|
|
def test_only_cc0_public_domain_cc_by_and_cc_by_sa(self):
|
|
free = {"cc0": "CC0", "pd": "Public domain", "cc-by-2.0": "CC BY 2.0", "cc-by-sa-4.0": "CC BY-SA 4.0",
|
|
"cc-by-sa-3.0-de": "CC BY-SA 3.0 de", "": "Public domain"}
|
|
for code, short in free.items():
|
|
with self.subTest(code=code):
|
|
self.assertIsNotNone(cc.candidate(page(License=code, LicenseShortName=short), "x")[0])
|
|
for code, short in {"fal": "FAL", "gfdl": "GFDL", "attribution": "Attribution", "": "Copyrighted free use"}.items():
|
|
with self.subTest(code=code or short):
|
|
c, why = cc.candidate(page(License=code, LicenseShortName=short), "x")
|
|
self.assertIsNone(c)
|
|
self.assertTrue(why.startswith("licence"))
|
|
|
|
def test_maps_and_drawings_are_no_photos(self):
|
|
c, why = cc.candidate(page(mime="image/svg+xml", License="cc0", LicenseShortName="CC0"), "x")
|
|
self.assertIsNone(c)
|
|
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):
|
|
return {"file": file, "categories": cats, "found": found}
|
|
self.assertTrue(cc.says_aerial(hit("Hungaroring, April 28, 2018 SkySat.jpg", [], ["search: aerial"])))
|
|
self.assertTrue(cc.says_aerial(hit("IMG 0211.JPG", ["Aerial photographs of Mogyoród"], ["search: aérienne"])))
|
|
self.assertTrue(cc.says_aerial(hit("Zolder racetrack.jpg", [], ["Wikidata aerial view (P8592)"])))
|
|
self.assertTrue(cc.says_aerial(hit("Bernhardjr2016.jpg", [], ["search: aerial", "search: drone"])))
|
|
self.assertFalse(cc.says_aerial(hit("FRECA 2025 Mogyoród Race (2).jpg", ["FRECA 2025"], ["search: aérienne"])))
|
|
|
|
|
|
class RankTest(unittest.TestCase):
|
|
def test_marks_then_size_then_landscape(self):
|
|
def cand(name, marks, w, h):
|
|
return {"file": name, "marks": marks, "width": w, "height": h}
|
|
ranked = sorted([
|
|
cand("small landscape", [], 2000, 1000),
|
|
cand("big portrait", [], 2000, 3000),
|
|
cand("big landscape", [], 3000, 2000),
|
|
cand("marked small", ["quality"], 800, 600),
|
|
], key=cc.rank_key)
|
|
self.assertEqual([c["file"] for c in ranked], ["marked small", "big landscape", "big portrait", "small landscape"])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|