Source code for scripts.illustrate_wikidata

# -*- coding: utf-8 -*-
Bot to add images to Wikidata items.

The image is extracted from the page_props. For this to be available the
PageImages extension (
needs to be installed


    python illustrate_wikidata <some generator>

# (C) Pywikibot team, 2013-2020
# Distributed under the terms of MIT License.
import pywikibot

from pywikibot import pagegenerators, WikidataBot

docuReplacements = {'&params;': pagegenerators.parameterHelp}  # noqa: N816

[docs]class IllustrateRobot(WikidataBot): """A bot to add Wikidata image claims."""
[docs] def __init__(self, generator, wdproperty='P18') -> None: """ Initializer. @param generator: A generator that yields Page objects @type generator: generator @param wdproperty: The property to add. Should be of type commonsMedia @type wdproperty: str """ super().__init__() self.generator = generator self.wdproperty = wdproperty self.cacheSources() claim = pywikibot.Claim(self.repo, self.wdproperty) if claim.type != 'commonsMedia': raise ValueError('{} is of type {}, should be commonsMedia' .format(self.wdproperty, claim.type))
[docs] def treat_page_and_item(self, page, item) -> None: """Treat a page / item.""" pywikibot.output('Found ' + item.title()) imagename ='page_image_free') if not imagename: return claims = item.get().get('claims') if self.wdproperty in claims: pywikibot.output('Item {} already contains image ({})' .format(item.title(), self.wdproperty)) return newclaim = pywikibot.Claim(self.repo, self.wdproperty) commonssite = pywikibot.Site('commons', 'commons') imagelink = pywikibot.Link(imagename, source=commonssite, default_namespace=6) image = pywikibot.FilePage(imagelink) if image.isRedirectPage(): image = pywikibot.FilePage(image.getRedirectTarget()) if not image.exists(): pywikibot.output("{} doesn't exist so I can't link to it" .format(image.title(as_link=True))) return newclaim.setTarget(image) # A generator might yield pages from multiple sites self.user_add_claim(item, newclaim,
[docs]def main(*args) -> None: """ Process command line arguments and invoke bot. If args is an empty list, sys.argv is used. @param args: command line arguments @type args: str """ # Process global args and prepare generator args parser local_args = pywikibot.handle_args(args) generator_factory = pagegenerators.GeneratorFactory() wdproperty = 'P18' for arg in local_args: if arg.startswith('-property'): if len(arg) == 9: wdproperty = pywikibot.input( 'Please enter the property you want to add:') else: wdproperty = arg[10:] else: generator_factory.handleArg(arg) generator = generator_factory.getCombinedGenerator(preload=True) if not generator: return bot = IllustrateRobot(generator, wdproperty)
if __name__ == '__main__': main()