jenkins-bot has submitted this change and it was merged. Change subject: create a new WikidataBot class for cacheSources() and getSource() for claimit.py, coordinate_import.py and harvest_template.py ......................................................................
create a new WikidataBot class for cacheSources() and getSource() for claimit.py, coordinate_import.py and harvest_template.py bug: 62438 this also allows for non-Wikipedia sources in claimit.py and coordinate_import.py as with change I29df011b82fd8687d4c38def62d0c0ea3a1b7994 bug: 62991 Change-Id: Ic05987911f0878d42a06cf5af842134bd9d615e8 --- M pywikibot/bot.py M scripts/claimit.py M scripts/coordinate_import.py M scripts/harvest_template.py 4 files changed, 45 insertions(+), 85 deletions(-) Approvals: Merlijn van Deen: Looks good to me, approved jenkins-bot: Verified diff --git a/pywikibot/bot.py b/pywikibot/bot.py index 3aa8189..7beda63 100644 --- a/pywikibot/bot.py +++ b/pywikibot/bot.py @@ -21,6 +21,7 @@ import os.path import sys import re +import json _logger = "bot" @@ -815,3 +816,32 @@ if choice != 'n': page.put(newtext, async=(choice == 'a')) + + +class WikidataBot: + """ + Generic Wikidata Bot to be subclassed + used in claimit.py, coordinate_import.py and harvest_template.py + """ + + def cacheSources(self): + """ + Fetches the sources from the onwiki list + and stores it internally + """ + page = pywikibot.Page(self.repo, u'List of wikis/python', ns=4) + self.source_values = json.loads(page.get()) + for family_code, family in self.source_values.iteritems(): + for source_lang in family: + self.source_values[family_code][source_lang] = pywikibot.ItemPage(self.repo, + family[source_lang]) + + def getSource(self, site): + """ + Get the source for the specified site, + if possible + """ + if site.family.name in self.source_values and site.code in self.source_values[site.family.name]: + source = pywikibot.Claim(self.repo, 'P143') + source.setTarget(self.source_values.get(site.family.name).get(site.code)) + return source diff --git a/scripts/claimit.py b/scripts/claimit.py index db8ff17..64beeb2 100755 --- a/scripts/claimit.py +++ b/scripts/claimit.py @@ -50,19 +50,18 @@ """ # # (C) Legoktm, 2013 -# (C) Pywikibot team, 2013 +# (C) Pywikibot team, 2013-2014 # # Distributed under the terms of the MIT license. # __version__ = '$Id$' # -import json import pywikibot -from pywikibot import pagegenerators +from pywikibot import pagegenerators, WikidataBot -class ClaimRobot: +class ClaimRobot(WikidataBot): """ A bot to add Wikidata claims """ @@ -79,28 +78,6 @@ self.exists_arg = exists_arg self.repo = pywikibot.Site().data_repository() self.cacheSources() - - def getSource(self, lang): - """ - Get the source for the specified language, - if possible - """ - if lang in self.source_values: - source = pywikibot.Claim(self.repo, 'p143') - source.setTarget(self.source_values.get(lang)) - return source - - def cacheSources(self): - """ - Fetches the sources from the onwiki list - and stores it internally - """ - page = pywikibot.Page(self.repo, u'Wikidata:List of wikis/python') - self.source_values = json.loads(page.get()) - self.source_values = self.source_values['wikipedia'] - for source_lang in self.source_values: - self.source_values[source_lang] = pywikibot.ItemPage(self.repo, - self.source_values[source_lang]) def run(self): """ @@ -147,7 +124,7 @@ % (claim.getID(), claim.getTarget())) item.addClaim(claim) # A generator might yield pages from multiple languages - source = self.getSource(page.site.language()) + source = self.getSource(page.site) if source: claim.addSource(source, bot=True) # TODO FIXME: We need to check that we aren't adding a diff --git a/scripts/coordinate_import.py b/scripts/coordinate_import.py index f89f55f..d732c22 100644 --- a/scripts/coordinate_import.py +++ b/scripts/coordinate_import.py @@ -18,18 +18,17 @@ """ # # (C) Multichill 2014 -# (C) Pywikibot team, 2013 +# (C) Pywikibot team, 2013-2014 # # Distributed under the terms of MIT License. # __version__ = '$Id$' # -import json import pywikibot -from pywikibot import pagegenerators +from pywikibot import pagegenerators, WikidataBot -class coordImportRobot: +class CoordImportRobot(WikidataBot): """ A bot to import coordinates to Wikidata """ @@ -40,31 +39,8 @@ """ self.generator = pagegenerators.PreloadingGenerator(generator) - self.site = pywikibot.Site() self.repo = pywikibot.Site().data_repository() self.cacheSources() - - def getSource(self, lang): - """ - Get the source for the specified language, - if possible - """ - if lang in self.source_values: - source = pywikibot.Claim(self.repo, 'p143') - source.setTarget(self.source_values.get(lang)) - return source - - def cacheSources(self): - """ - Fetches the sources from the onwiki list - and stores it internally - """ - page = pywikibot.Page(self.repo, u'Wikidata:List of wikis/python') - self.source_values = json.loads(page.get()) - self.source_values = self.source_values['wikipedia'] - for source_lang in self.source_values: - self.source_values[source_lang] = pywikibot.ItemPage(self.repo, - self.source_values[source_lang]) def run(self): """ @@ -88,7 +64,7 @@ pywikibot.output(u'Adding %s, %s to %s' % (coordinate.lat, coordinate.lon, item.title())) item.addClaim(newclaim) - source = self.getSource(page.site.language()) + source = self.getSource(page.site) if source: newclaim.addSource(source, bot=True) @@ -102,7 +78,7 @@ generator = gen.getCombinedGenerator() - coordbot = coordImportRobot(generator) + coordbot = CoordImportRobot(generator) coordbot.run() if __name__ == "__main__": diff --git a/scripts/harvest_template.py b/scripts/harvest_template.py index 79adb7c..ec85415 100755 --- a/scripts/harvest_template.py +++ b/scripts/harvest_template.py @@ -16,7 +16,7 @@ """ # # (C) Multichill, Amir, 2013 -# (C) Pywikibot team, 2013 +# (C) Pywikibot team, 2013-2014 # # Distributed under the terms of MIT License. # @@ -24,14 +24,13 @@ # import re -import json import pywikibot -from pywikibot import pagegenerators as pg +from pywikibot import pagegenerators as pg, WikidataBot docuReplacements = {'¶ms;': pywikibot.pagegenerators.parameterHelp} -class HarvestRobot: +class HarvestRobot(WikidataBot): """ A bot to add Wikidata claims """ @@ -50,28 +49,6 @@ self.repo = pywikibot.Site().data_repository() self.cacheSources() - def getSource(self, site): - """ - Get the source for the specified site, - if possible - """ - if site.family.name in self.source_values and site.code in self.source_values[site.family.name]: - source = pywikibot.Claim(self.repo, 'P143') - source.setTarget(self.source_values.get(site.family.name).get(site.code)) - return source - - def cacheSources(self): - """ - Fetches the sources from the onwiki list - and stores it internally - """ - page = pywikibot.Page(self.repo, u'List of wikis/python', ns=4) - self.source_values = json.loads(page.get()) - for family_code, family in self.source_values.iteritems(): - for source_lang in family: - self.source_values[family_code][source_lang] = pywikibot.ItemPage(self.repo, - family[source_lang]) - def run(self): """ Starts the robot. @@ -79,7 +56,7 @@ self.templateTitles = self.getTemplateSynonyms(self.templateTitle) for page in self.generator: try: - self.procesPage(page) + self.processPage(page) except Exception as e: pywikibot.exception(tb=True) @@ -97,9 +74,9 @@ titles.append(temp.title(withNamespace=False)) return titles - def procesPage(self, page): + def processPage(self, page): """ - Proces a single page + Process a single page """ item = pywikibot.ItemPage.fromPage(page) pywikibot.output('Processing %s' % page) -- To view, visit https://gerrit.wikimedia.org/r/126213 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: Ic05987911f0878d42a06cf5af842134bd9d615e8 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Ricordisamoa <ricordisa...@live.it> Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com> Gerrit-Reviewer: Merlijn van Deen <valhall...@arctus.nl> Gerrit-Reviewer: Multichill <maar...@mdammers.nl> Gerrit-Reviewer: Ricordisamoa <ricordisa...@live.it> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits