jenkins-bot has submitted this change and it was merged.

Change subject: bot: implement set_retrieved()
......................................................................


bot: implement set_retrieved()

Helper add or modify a retrieved qualifier to a claim.

Change-Id: Iff53eaf80ee88dd3c196047954df96fa5d22f8b4
Signed-off-by: Loic Dachary <l...@dachary.org>
---
M FLOSSbot/bot.py
M tests/test_bot.py
2 files changed, 63 insertions(+), 1 deletion(-)

Approvals:
  Dachary: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/FLOSSbot/bot.py b/FLOSSbot/bot.py
index 20b34e9..16fa42a 100644
--- a/FLOSSbot/bot.py
+++ b/FLOSSbot/bot.py
@@ -16,6 +16,7 @@
 #
 import argparse
 import logging
+from datetime import datetime, timedelta
 
 import pywikibot
 
@@ -48,6 +49,11 @@
             '--user',
             default=None,
             help='wikidata user name')
+        parser.add_argument(
+            '--verification-delay',
+            type=int,
+            default=30,
+            help='days to wait before verifying a claim again')
         return parser
 
     @staticmethod
@@ -107,7 +113,6 @@
 
     def create_entity(self, type, name):
         found = self.search_entity(self.wikidata_site, name, type=type)
-        assert found, type + " " + name + " must exist wikidata"
         entity = {
             "labels": {
                 "en": {
@@ -117,6 +122,7 @@
             },
         }
         if type == 'property':
+            assert found, type + " " + name + " must exist wikidata"
             id = found['id']
             found = self.wikidata_site.loadcontent({'ids': id}, 'datatype')
             assert found, "datatype of " + id + " " + name + " is not found"
@@ -153,3 +159,33 @@
                 if found is not None:
                     break
         return found
+
+    def need_verification(self, claim):
+        now = datetime.utcnow()
+        if self.P_retrieved in claim.qualifiers:
+            previous = claim.qualifiers[self.P_retrieved][0]
+            previous = previous.getTarget()
+            previous = datetime(year=previous.year,
+                                month=previous.month,
+                                day=previous.day)
+            if now - previous > timedelta(
+                    days=self.args.verification_delay):
+                return True
+        return False
+
+    def set_retrieved(self, item, claim, now=datetime.utcnow()):
+        when = pywikibot.WbTime(now.year, now.month, now.day)
+        if self.P_retrieved in claim.qualifiers:
+            self.debug(item, "updating retrieved")
+            retrieved = claim.qualifiers[self.P_retrieved][0]
+            retrieved.setTarget(when)
+            if not self.args.dry_run:
+                self.site.save_claim(claim)
+        else:
+            self.debug(item, "setting retrieved")
+            retrieved = pywikibot.Claim(self.site,
+                                        self.P_retrieved,
+                                        isQualifier=True)
+            retrieved.setTarget(when)
+            if not self.args.dry_run:
+                claim.addQualifier(retrieved, bot=True)
diff --git a/tests/test_bot.py b/tests/test_bot.py
index a4338a3..ff2c70f 100644
--- a/tests/test_bot.py
+++ b/tests/test_bot.py
@@ -17,6 +17,11 @@
 #
 import argparse
 import logging
+import random
+import string
+from datetime import date
+
+import pywikibot
 
 from FLOSSbot.bot import Bot
 from tests.wikidata import TestWikidata
@@ -83,3 +88,24 @@
                     new_content[property]['datatype']), attr
             assert (datatype ==
                     wikidata_content[wikidata_property]['datatype']), attr
+
+    def test_set_retrieved(self):
+        bot = Bot(argparse.Namespace(
+            test=True,
+            user='FLOSSbotCI',
+            dry_run=False,
+            verification_delay=30,
+        ))
+        name = ''.join(random.choice(
+            string.ascii_lowercase) for _ in range(16))
+        item = bot.__getattribute__('Q_' + name)
+        claim = pywikibot.Claim(bot.site,
+                                bot.P_source_code_repository,
+                                0)
+        claim.setTarget("http://repo.com/some";)
+        item.addClaim(claim)
+        bot.set_retrieved(item, claim)
+        assert bot.need_verification(claim) is False
+        bot.set_retrieved(item, claim, date(1965, 11, 2))
+        assert bot.need_verification(claim) is True
+        bot.clear_entity_label(item.getID())

-- 
To view, visit https://gerrit.wikimedia.org/r/311501
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Iff53eaf80ee88dd3c196047954df96fa5d22f8b4
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/bots/FLOSSbot
Gerrit-Branch: master
Gerrit-Owner: Dachary <l...@dachary.org>
Gerrit-Reviewer: Dachary <l...@dachary.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to