<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2013 Legoktm
Copyright (C) 2013 Pywikipediabot team

Distributed under the MIT License

Usage:

python claimit.py [pagegenerators] P1 Q2 P123 Q456

You can use any typical pagegenerator to provide with a list of pages

Then list the property-->target pairs to add.
"""
import pywikibot
from pywikibot import pagegenerators

class ClaimRobot:
    """
    A bot to add Wikidata claims
    """
    def __init__(self, generator, claims):
        """
        Arguments:
            * generator    - A generator that yields Page objects.
            * claims       - A list of wikidata claims

        """
        self.generator = generator
        self.claims = claims
        self.repo = pywikibot.Site().data_repository()
        self.source = None
        self.setSource(pywikibot.Site().language())
    
    def setSource(self, lang):
        '''
        Get the source
        '''
        source_values = {'en': pywikibot.ItemPage(self.repo, 'Q328'),
                         'sv': pywikibot.ItemPage(self.repo, 'Q169514'),
                         'de': pywikibot.ItemPage(self.repo, 'Q48183'),
                         'it': pywikibot.ItemPage(self.repo, 'Q11920'),
                         'no': pywikibot.ItemPage(self.repo, 'Q191769'),
                         'ar': pywikibot.ItemPage(self.repo, 'Q199700'),
                         'es': pywikibot.ItemPage(self.repo, 'Q8449'),
                         'pl': pywikibot.ItemPage(self.repo, 'Q1551807'),
                         'ca': pywikibot.ItemPage(self.repo, 'Q199693'),
                         'fr': pywikibot.ItemPage(self.repo, 'Q8447'),
                         'nl': pywikibot.ItemPage(self.repo, 'Q10000'),
                         'pt': pywikibot.ItemPage(self.repo, 'Q11921'),
                         'ru': pywikibot.ItemPage(self.repo, 'Q206855'),
                         'vi': pywikibot.ItemPage(self.repo, 'Q200180'),
                         'be': pywikibot.ItemPage(self.repo, 'Q877583'),
                         'uk': pywikibot.ItemPage(self.repo, 'Q199698'),
                         'tr': pywikibot.ItemPage(self.repo, 'Q58255'),
                 }  # TODO: This should include all projects
        
        if lang in source_values:
            self.source = pywikibot.Claim(self.repo, 'p143')
            self.source.setTarget(source_values.get(lang))

    def run(self):
        """
        Starts the robot.
        """
        for page in self.generator:
            item = pywikibot.ItemPage.fromPage(page)
            pywikibot.output('Processing %s' % page)
            if not item.exists():
                pywikibot.output('%s doesn\'t have a wikidata item :(' % page)
                #TODO FIXME: We should provide an option to create the page
            else:
                for claim in self.claims:
                    if claim.getID() in item.get().get('claims'):
                        pywikibot.output(u'A claim for %s already exists. Skipping' % (claim.getID(),))
                        #TODO FIXME: This is a very crude way of dupe checking
                    else:
                        pywikibot.output('Adding %s --> %s' % (claim.getID(), claim.getTarget().getID()))
                        item.addClaim(claim)
                        if self.source:
                            claim.addSource(self.source, bot=True)
                        #TODO FIXME: We need to check that we aren't adding a duplicate           


def main():<commit_msg>Fetch the list of sources from a central list at [[d:Wikidata:List of wikis/python]]<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2013 Legoktm
Copyright (C) 2013 Pywikipediabot team

Distributed under the MIT License

Usage:

python claimit.py [pagegenerators] P1 Q2 P123 Q456

You can use any typical pagegenerator to provide with a list of pages

Then list the property-->target pairs to add.
"""
import simplejson
import pywikibot
from pywikibot import pagegenerators

class ClaimRobot:
    """
    A bot to add Wikidata claims
    """
    def __init__(self, generator, claims):
        """
        Arguments:
            * generator    - A generator that yields Page objects.
            * claims       - A list of wikidata claims

        """
        self.generator = generator
        self.claims = claims
        self.repo = pywikibot.Site().data_repository()
        self.source = None
        self.setSource(pywikibot.Site().language())

    def setSource(self, lang):
        '''
        Get the source
        '''
        page = pywikibot.Page(self.repo, 'Wikidata:List of wikis/python')
        source_values = simplejson.loads(page.get())
        source_values = source_values['wikipedia']
        for lang in source_values:
            source_values[lang] = pywikibot.ItemPage(self.repo, source_values[lang])

        if lang in source_values:
            self.source = pywikibot.Claim(self.repo, 'p143')
            self.source.setTarget(source_values.get(lang))

    def run(self):
        """
        Starts the robot.
        """
        for page in self.generator:
            item = pywikibot.ItemPage.fromPage(page)
            pywikibot.output('Processing %s' % page)
            if not item.exists():
                pywikibot.output('%s doesn\'t have a wikidata item :(' % page)
                #TODO FIXME: We should provide an option to create the page
            else:
                for claim in self.claims:
                    if claim.getID() in item.get().get('claims'):
                        pywikibot.output(u'A claim for %s already exists. Skipping' % (claim.getID(),))
                        #TODO FIXME: This is a very crude way of dupe checking
                    else:
                        pywikibot.output('Adding %s --> %s' % (claim.getID(), claim.getTarget().getID()))
                        item.addClaim(claim)
                        if self.source:
                            claim.addSource(self.source, bot=True)
                        #TODO FIXME: We need to check that we aren't adding a duplicate           


def main():
{'fraction_non_alphanumeric': 0.11009771986970684, 'fraction_numerical': 0.021335504885993485, 'mean_word_length': 1.7390722569134702, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4695390', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1724, 'n_words': 463}