<commit_before>#!/usr/bin/python
# -*- 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 (https://www.mediawiki.org/wiki/Extension:PageImages)
needs to be installed

Usage:

    python pwb.py illustrate_wikidata <some generator>

&params;
"""
#
# (C) Multichill, 2014
# (C) Pywikibot team, 2013-2017
#
# Distributed under the terms of MIT License.
#
from __future__ import absolute_import, unicode_literals

__version__ = '$Id$'
#

import pywikibot

from pywikibot import pagegenerators, WikidataBot

docuReplacements = {'&params;': pywikibot.pagegenerators.parameterHelp}


class IllustrateRobot(WikidataBot):

    """A bot to add Wikidata image claims."""

    def __init__(self, generator, wdproperty=u'P18'):
        """
        Constructor.

        @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(IllustrateRobot, self).__init__()
        self.generator = generator
        self.wdproperty = wdproperty
        self.cacheSources()

        claim = pywikibot.Claim(self.repo, self.wdproperty)
        if claim.type != 'commonsMedia':
            raise ValueError(u'%s is of type %s, should be commonsMedia'
                             % (self.wdproperty, claim.type))

    def treat(self, page, item):
        """Treat a page / item."""
        self.current_page = page

        pywikibot.output(u'Found %s' % item.title())
        imagename = page.properties().get('page_image')

        if not imagename:
            return

        claims = item.get().get('claims')
        if self.wdproperty in claims:
            pywikibot.output('Item %s already contains image (%s)'
                             % (item.title(), self.wdproperty))
            return

        newclaim = pywikibot.Claim(self.repo, self.wdproperty)
        commonssite = pywikibot.Site("commons", "commons")
        imagelink = pywikibot.Link(imagename, source=commonssite,
                                   defaultNamespace=6)
        image = pywikibot.FilePage(imagelink)
        if image.isRedirectPage():
            image = pywikibot.FilePage(image.getRedirectTarget())

        if not image.exists():
            pywikibot.output('[[%s]] doesn\'t exist so I can\'t link to it'
                             % (image.title(),))
            return

        newclaim.setTarget(image)
        # A generator might yield pages from multiple sites
        self.user_add_claim(item, newclaim, page.site)


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
    """
    # Process global args and prepare generator args parser
    local_args = pywikibot.handle_args(args)
    generator_factory = pagegenerators.GeneratorFactory()

    wdproperty = u'P18'

    for arg in local_args:
        if arg.startswith('-property'):
            if len(arg) == 9:
                wdproperty = pywikibot.input(
                    u'Please enter the property you want to add:')
            else:
                wdproperty = arg[10:]<commit_msg>[bugfix] illustratewikidata: rename page_image key in page_image_free<commit_after>#!/usr/bin/python
# -*- 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 (https://www.mediawiki.org/wiki/Extension:PageImages)
needs to be installed

Usage:

    python pwb.py illustrate_wikidata <some generator>

&params;
"""
#
# (C) Multichill, 2014
# (C) Pywikibot team, 2013-2017
#
# Distributed under the terms of MIT License.
#
from __future__ import absolute_import, unicode_literals

__version__ = '$Id$'
#

import pywikibot

from pywikibot import pagegenerators, WikidataBot

docuReplacements = {'&params;': pywikibot.pagegenerators.parameterHelp}


class IllustrateRobot(WikidataBot):

    """A bot to add Wikidata image claims."""

    def __init__(self, generator, wdproperty=u'P18'):
        """
        Constructor.

        @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(IllustrateRobot, self).__init__()
        self.generator = generator
        self.wdproperty = wdproperty
        self.cacheSources()

        claim = pywikibot.Claim(self.repo, self.wdproperty)
        if claim.type != 'commonsMedia':
            raise ValueError(u'%s is of type %s, should be commonsMedia'
                             % (self.wdproperty, claim.type))

    def treat(self, page, item):
        """Treat a page / item."""
        self.current_page = page

        pywikibot.output(u'Found %s' % item.title())
        imagename = page.properties().get('page_image_free')

        if not imagename:
            return

        claims = item.get().get('claims')
        if self.wdproperty in claims:
            pywikibot.output('Item %s already contains image (%s)'
                             % (item.title(), self.wdproperty))
            return

        newclaim = pywikibot.Claim(self.repo, self.wdproperty)
        commonssite = pywikibot.Site("commons", "commons")
        imagelink = pywikibot.Link(imagename, source=commonssite,
                                   defaultNamespace=6)
        image = pywikibot.FilePage(imagelink)
        if image.isRedirectPage():
            image = pywikibot.FilePage(image.getRedirectTarget())

        if not image.exists():
            pywikibot.output("%s doesn't exist so I can't link to it"
                             % image.title(asLink=True))
            return

        newclaim.setTarget(image)
        # A generator might yield pages from multiple sites
        self.user_add_claim(item, newclaim, page.site)


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
    """
    # Process global args and prepare generator args parser
    local_args = pywikibot.handle_args(args)
    generator_factory = pagegenerators.GeneratorFactory()

    wdproperty = u'P18'

    for arg in local_args:
        if arg.startswith('-property'):
            if len(arg) == 9:
                wdproperty = pywikibot.input(
                    u'Please enter the property you want to add:')
            else:
                wdproperty = arg[10:]
{'fraction_non_alphanumeric': 0.09965839893064013, 'fraction_numerical': 0.0062379325709193524, 'mean_word_length': 2.37882589061716, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4127285', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1896, 'n_words': 547}