jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1174762?usp=email )
Change subject: Add get_value_at_timestamp API to ItemPage ...................................................................... Add get_value_at_timestamp API to ItemPage Bug: T400612 Change-Id: I61e9396a29caad927ce7b3bdfca35f9a58b98c55 Signed-off-by: Strainu <[email protected]> --- M pywikibot/family.py M pywikibot/page/_wikibase.py M tests/wikibase_tests.py 3 files changed, 100 insertions(+), 0 deletions(-) Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved diff --git a/pywikibot/family.py b/pywikibot/family.py index 0c4a979..9197646 100644 --- a/pywikibot/family.py +++ b/pywikibot/family.py @@ -1111,6 +1111,16 @@ .. versionadded:: 8.2 """ + @property + def interval_start_property(self) -> str: + """Return the property for the start of an interval.""" + return 'P580' + + @property + def interval_end_property(self) -> str: + """Return the property for the end of an interval.""" + return 'P582' + def calendarmodel(self, code) -> str: """Default calendar model for WbTime datatype.""" return 'http://www.wikidata.org/entity/Q1985727' diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py index 3c1df0a..9c58032 100644 --- a/pywikibot/page/_wikibase.py +++ b/pywikibot/page/_wikibase.py @@ -1358,6 +1358,7 @@ :raises UnknownExtensionError: site has no Wikibase extension """ + def find_best_claim(claims): """Find the first best ranked claim.""" index = None @@ -1374,6 +1375,67 @@ return find_best_claim(self.claims[prop]) return None + def get_value_at_timestamp( + self, + prop: str, + timestamp: pywikibot.WbTime, + lang: str = 'en' + ): + """Return the best value for this page at a given timestamp. + + :param prop: property id, "P###" + :param timestamp: the timestamp to check the value at + :param lang: the language to return the value in + :return: WbRepresentation object given by Wikibase property number for + this page object and valid for the given timestamp and language. + :rtype: pywikibot.WbRepresentation or None + + :raises NoWikibaseEntityError: site has no time interval properties + """ + if not hasattr(self.site.family, 'interval_start_property') or \ + not hasattr(self.site.family, 'interval_end_property'): + raise NoWikibaseEntityError( + f'{self.site.family} does not have time interval properties') + startp = self.site.family.interval_start_property + endp = self.site.family.interval_end_property + + def timestamp_in_interval(p, ts): + """Check if timestamp is within the qualifiers.""" + q1 = p.qualifiers.get(startp, []) + q2 = p.qualifiers.get(endp, []) + d1 = d2 = None + if q1: + d1 = q1[0].getTarget() + if q2: + d2 = q2[0].getTarget() + if d1 and d2: + return d1 <= ts <= d2 + if d1: + return d1 <= ts + if d2: + return d2 >= ts + return False + + def find_value_at_timestamp(claims, ts, language): + """Find the first best ranked claim at a given timestamp.""" + sorted_claims = sorted(claims, + key=lambda c: c.qualifiers.get(startp)[ + 0].getTarget() if c.qualifiers.get( + startp) else pywikibot.WbTime(0), + reverse=True) + for claim in sorted_claims: + if timestamp_in_interval(claim, ts): + if (claim.type == 'monolingualtext' + and claim.getTarget().language != language): + continue + else: + return claim.getTarget() + return None + + if prop in self.claims: + return find_value_at_timestamp(self.claims[prop], timestamp, lang) + return None + class Property: diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index 47a736d..e5fbac6 100755 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -1509,6 +1509,34 @@ self.assertEqual(item.get_best_claim('P17').getTarget(), pywikibot.ItemPage(wikidata, 'Q142')) + def test_get_value_at_timestamp(self) -> None: + """Test getting the value of a claim at a specific timestamp.""" + wikidata = self.get_repo() + item = pywikibot.ItemPage(wikidata, 'Q90') + item.get() + wbtime = pywikibot.WbTime(year=2021, month=1, day=1, site=wikidata) + claim = item.get_value_at_timestamp('P17', wbtime) + self.assertEqual(claim, pywikibot.ItemPage(wikidata, 'Q142')) + + def test_with_monolingual_good_language(self) -> None: + """Test getting a monolingual text claim with a good language.""" + wikidata = self.get_repo() + item = pywikibot.ItemPage(wikidata, 'Q183') + item.get() + wbtime = pywikibot.WbTime(year=2021, month=1, day=1, site=wikidata) + claim = item.get_value_at_timestamp('P1448', wbtime, 'ru') + self.assertIsInstance(claim, pywikibot.WbMonolingualText) + self.assertEqual(claim.language, 'ru') + + def test_with_monolingual_wrong_language(self) -> None: + """Test getting a monolingual text claim with a good language.""" + wikidata = self.get_repo() + item = pywikibot.ItemPage(wikidata, 'Q183') + item.get() + wbtime = pywikibot.WbTime(year=2021, month=1, day=1, site=wikidata) + claim = item.get_value_at_timestamp('P1448', wbtime, 'en') + self.assertIsNone(claim, None) + if __name__ == '__main__': with suppress(SystemExit): -- To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1174762?usp=email To unsubscribe, or for help writing mail filters, visit https://gerrit.wikimedia.org/r/settings?usp=email Gerrit-MessageType: merged Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Change-Id: I61e9396a29caad927ce7b3bdfca35f9a58b98c55 Gerrit-Change-Number: 1174762 Gerrit-PatchSet: 8 Gerrit-Owner: Strainu <[email protected]> Gerrit-Reviewer: Xqt <[email protected]> Gerrit-Reviewer: jenkins-bot
_______________________________________________ Pywikibot-commits mailing list -- [email protected] To unsubscribe send an email to [email protected]
