Created
November 16, 2016 09:02
-
-
Save Ladsgroup/659934ec9bcbdd56d54c25aae793455f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
""" | |
Fix quantities in wikidata. See T142087 | |
The following parameters are supported: | |
¶ms; | |
-property like P31 | |
-unit like Q3 | |
""" | |
# | |
# Amir Sarabadani, 2016 | |
# | |
# Distributed under the terms of the MIT license. | |
# | |
from __future__ import absolute_import, unicode_literals | |
import pywikibot | |
from pywikibot import pagegenerators | |
from pywikibot.bot import CurrentPageBot | |
docuReplacements = { | |
'¶ms;': pagegenerators.parameterHelp | |
} | |
class QuantityBot(CurrentPageBot): | |
""" | |
A bot to fix quantity changes in Wikidata. | |
""" | |
def __init__(self, generator, **kwargs): | |
""" | |
Constructor. | |
@param generator: the page generator that determines on which pages | |
to work | |
@type generator: generator | |
""" | |
self.availableOptions.update({ | |
'property': None, | |
'unit': None, | |
'repo': pywikibot.Site().data_repository(), | |
}) | |
super(QuantityBot, self).__init__(site=True, **kwargs) | |
self.generator = generator | |
if not self.getOption('property'): | |
raise RuntimeError('Property is needed') | |
def treat(self, page): | |
if page.namespace() != 0: | |
return | |
item = pywikibot.ItemPage(self.getOption('repo'), page.title()) | |
try: | |
item.get() | |
except pywikibot.NoPage: | |
return | |
except pywikibot.IsRedirectPage: | |
return | |
property_to_fix = self.getOption('property') | |
unit = self.getOption('unit') | |
if property_to_fix not in item.claims: | |
return | |
for claim in item.claims[property_to_fix]: | |
value = claim.getTarget() | |
if unit and 'http://www.wikidata.org/entity/' + unit == value.unit: | |
continue | |
if value.upperBound == value.lowerBound: | |
self.clean_claim(claim, item) | |
def clean_claim(self, claim, item): | |
target = claim.getTarget() | |
target.upperBound = None | |
target.lowerBound = None | |
claim.setTarget(target) | |
print('Trying to fix the claim, editing') | |
self.getOption('repo').save_claim(claim) | |
def main(*args): | |
""" | |
Process command line arguments and invoke bot. | |
If args is an empty list, sys.argv is used. | |
@param args: command line arguments | |
@type args: list of unicode | |
""" | |
options = {} | |
local_args = pywikibot.handle_args(args) | |
gen_factory = pagegenerators.GeneratorFactory() | |
for arg in local_args: | |
if gen_factory.handleArg(arg): | |
continue | |
arg, sep, value = arg.partition(':') | |
option = arg[1:] | |
if option in ('summary', 'text'): | |
if not value: | |
pywikibot.input('Please enter a value for ' + arg) | |
options[option] = value | |
elif option == 'property': | |
options[option] = value | |
elif option == 'unit': | |
options[option] = value | |
else: | |
pywikibot.warning('Unknown option %s' % option) | |
gen = gen_factory.getCombinedGenerator() | |
if gen: | |
gen = pagegenerators.PreloadingGenerator(gen) | |
bot = QuantityBot(gen, **options) | |
bot.run() | |
return True | |
else: | |
pywikibot.bot.suggest_help(missing_generator=True) | |
return False | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment