Open main menu

User:Ivanhercaz/labels-indonesian-villages.py

import pywikibot
from pywikibot import pagegenerators as pg


query='select ?item where {?item wdt:P31 wd:Q2225692 . ?item wdt:P17 wd:Q252}'
lang='es'
src='id'

def wd_sparql_query(spq):
  wikidatasite=pywikibot.Site('wikidata','wikidata') 
  generator=pg.WikidataSPARQLPageGenerator(spq,site=wikidatasite)
  for wd in generator:
    if (wd.exists()):
      wd.get(get_redirect=True)
      yield wd

es_blank = 0
es_filled= 0
for village in wd_sparql_query(query):
  es=''
  id=''
  if (src) in village.labels:
    id=village.labels['id']
    if (lang) in village.labels:
      es=village.labels[lang]
      es_filled += 1
    else:
      es_blank += 1  
      data={}
      data.update({'labels':{lang:id}})
      try:
        village.editEntity(data,summary=u'from label id-wiki')
      except:
        pass
  else:    
    print('%s-%d-%d-%d-[%s]-<%s>' % (village.title(),100*es_filled / (es_blank+es_filled+1),es_filled,es_blank,es,id))