User:ZaBOTka/StatisticsCode

# -*- coding: utf-8 -*-
#20121111: Not fully corrected logging yet. No comments yet.
#changes in pywikipedia
#path=path.replace("ZaBOTka/","ZaBOTka%2f")
#changes in pywikidata 
#item.missing was added

import wikidata
import wikipedia

lang="ru"
b=951
e=b+50

no_label=""
no_description=""
no_wiki=""
log=""
log_add=""


items = wikidata.api.getItemsById(range(b,e))
for item in items:
    if item.missing == 0:
        lang_wiki = ""
        lang_label = ""
        lang_description = ""

        try: lang_wiki = item.sitelinks[lang+"wiki"]
        except: no_wiki = no_wiki + "# [[Q"+str(item.id)+"]]\n"

        try: lang_label = item.labels[lang]
        except: pass 

        try: lang_description = item.descriptions[lang]
        except:
            try: no_description = no_description + "# [[Q"+str(item.id)+"|"+lang_wiki+"]]\n"
            except: no_description = no_description + "# [[Q"+str(item.id)+"]]\n"

        if lang_wiki != "" and lang_label == "" and lang_wiki.find("(")<=0:
            item.labels[lang]=lang_wiki
#           wikidata.api.save(item, "Add ["+lang+"] label: "+lang_wiki)
            log = log + "# Label could be added for item "+str(item.id)+"\n"
        else:
            if lang_label != "":
                log = log + "# No changes for item "+str(item.id)+" (label exists)\n"
            elif lang_wiki == "":
                no_label = no_label + "# [[Q"+str(item.id)+"]]\n"
                log = log + "# No changes for item "+str(item.id)+" ("+lang+"wiki doesn't exist)\n"
            elif lang_wiki.find("(")>0:
                log = log + "# No changes for item "+str(item.id)+" (brackets)\n"
                log_add = log_add + "# [[Q"+str(item.id)+"|"+lang_wiki+"]]\n"
#               log_add = log_add + "# [[Q"+str(item.id)+"]]\n"
                no_label = no_label + "# [[Q"+str(item.id)+"]]\n"

section_title = "\n== "+str(b)+" - "+str(e)+" ==\n"
site = wikipedia.getSite()
no_wiki_page = wikipedia.Page(site, "User:ZaBOTka/No-Ru-Wiki")
no_label_page = wikipedia.Page(site, "User:ZaBOTka/No-Ru-Labels")
no_description_page = wikipedia.Page(site, "User:ZaBOTka/No-Ru-Descriptors")
char_page = wikipedia.Page(site, "User:ZaBOTka/No-Ru-Labels_char")
log_page = wikipedia.Page(site, "User:ZaBOTka/GenLog")

no_description = no_description.replace("|]]","]]")
no_wiki_page.put(no_wiki_page.get()+section_title+no_wiki, section_title)
no_label_page.put(no_label_page.get()+section_title+no_label, section_title)
no_description_page.put(no_description_page.get()+section_title+no_description, section_title)
char_page.put(char_page.get()+section_title+log_add, section_title)
log_page.put(log_page.get()+section_title+log, section_title)