User:Edoderoobot/StratenInWoerden.py

import time
import json
import pywikibot
from pywikibot import pagegenerators
import urllib
import re
import pywikibot.data.wikidataquery as wdquery
from pywikibot.data import api

streets_query =  u'claim[31:79007]' # and claim[131:889423]'
#CheckTheseLabels = ['fr','de','en','es','it', 'hr']
#UseTheseDescriptions=['rue en ','Strasse in ','street in ','calle en ','strada a ','ulicu u ']
#UseTheseSuffix=[', NL',', NL',', NL',', NL',', NL','u, NL']
CheckTheseLabels = ['nl']
UseTheseDescriptions=['straat in ']
UseTheseSuffix=[', ',]
source_country = 'en'

def waitsometime(howmanyseconds):
 endtime = time.time() + howmanyseconds
 while time.time()< endtime :
   i = 0


class StreetBot:
    """
    A bot to add streets on Wikidata
    """
    def __init__(self, generator):
        """
        Arguments:
            * generator    - A generator that yields itempage objects.
        """
        self.generator = generator
        self.repo = pywikibot.Site().data_repository()

    def run(self):
        """
        Starts the robot.
        """
 
        itemcount = 0
        changed = 0
        site = pywikibot.getSite('nl')
        repo = site.data_repository()
        for wikidataitem in self.generator:
          wikidataitem.get()
          if (changed<49):
            itemcount += 1
            if wikidataitem.exists() :
                wikidataitem.get()
                if source_country in wikidataitem.labels :    # if original language is in the labels
                  data = {}
                  newlabel = wikidataitem.labels[source_country]
                  for thislabel in CheckTheseLabels :         # loop through all labels we like to fill
                    if not thislabel in wikidataitem.labels : # if not yet filled in
                      changed += 1
                      if ('P17' in wikidataitem.claims) :
                        wdicountry = wikidataitem.claims.get('P17')[0].getTarget()   # get wd-entry for country
                        wdicountryrec = pywikibot.ItemPage(repo,wdicountry.title())
                        wdicountryrec.get()
                        wdicountryISO = wdicountryrec.claims.get('P297')[0].getTarget()  # use ISO-2-code
                        wdicountryname= wdicountryrec.labels[thislabel] #country name in source language
                      else :
                        wdicountryISO = ''
                      townlabel = ''
                      if ('P131' in wikidataitem.claims) :
                        thislocation1 = wikidataitem.claims.get('P131')[0].getTarget()
                        wditown = pywikibot.ItemPage(repo,thislocation1.title())  # this can be used for any page object
                        wditown.get()
                        if (thislabel in wditown.labels) :
                          townlabel = wditown.labels[thislabel]
                      if ('P276' in wikidataitem.claims) :
                        thislocation2 = wikidataitem.claims.get('P276')[0].getTarget()
                        wdineighbourhood = pywikibot.ItemPage(repo,thislocation2.title())
                        wdineighbourhood.get()
                        neighbourhoodlabel = wdineighbourhood.labels[thislabel]
                      else :
                        neighbourhoodlabel = ''
                      if (neighbourhoodlabel=='') :
                        thislocation = townlabel
                      else :
                        thislocation = neighbourhoodlabel+", "+townlabel
                      myindex = CheckTheseLabels.index(thislabel)
                      if (thislocation=='') :
                        mydescription = UseTheseDescriptions[myindex]+thislocation+wdicountryname
                      else :
                        mydescription = UseTheseDescriptions[myindex]+thislocation+UseTheseSuffix[myindex]+wdicountryISO
                      data.update( {'labels': { thislabel: newlabel}, 'descriptions': {thislabel:mydescription} } )
                      print ("%s: [%s]: %s | %s" % (wikidataitem.title(),thislabel,newlabel,mydescription))

                      waitsometime(1)
                      wikidataitem.editEntity(data,summary=u'Ed0 edited with python')

          else :
            break

              
def WikidataQueryItemPageGenerator(query, site=None):
    """Generate pages that result from the given WikidataQuery.
    @param query: the WikidataQuery query string.
    """
    if site is None:
        site = pywikibot.Site()
    repo = site.data_repository()

    wd_queryset = wdquery.QuerySet(query)

    wd_query = wdquery.WikidataQuery(cacheMaxAge=0)
    data = wd_query.query(wd_queryset)

    pywikibot.output(u'retrieved %d items' % data[u'status'][u'items'])
    for item in data[u'items']:
        yield pywikibot.ItemPage(repo, u'Q' + unicode(item))


def main():
    print "main"
    pigenerator = pagegenerators.PreloadingItemGenerator(pagegenerators.WikidataItemGenerator(WikidataQueryItemPageGenerator(streets_query)))
   
    streetBot = StreetBot(pigenerator)
    streetBot.run()

main()