Open main menu

User:AkkakkBot/code/01-terminator-taxon

python code:

        site = pywikibot.Site("wikidata", "wikidata")
        repo = site.data_repository()
        editmax = 1500
        log = ""
        exit_reason = "end of data"

        #get items
        items = []
        #response = urllib2.urlopen('http://tools.wmflabs.org/wikidata-terminator/data/wikidata.en.t1000')
        #csv = response.read()
        csv = ""
        for lang in ['en', 'de', 'es', 'fr']:
            with open ("/data/project/wikidata-terminator/data/wikidata."+lang+".t1000.1000", "r") as file_handler:
                csv += file_handler.read()

        lines = csv.rstrip().split('\n')
        for line in lines:
            linelist = line.rstrip().split('\t')
            if linelist[0][0] == "Q":
                items.append(linelist[0])

        #iterate items
        editcnt = 0
        try:
            for i, q in enumerate(items):
                print("checking "+q);
                item = pywikibot.ItemPage(repo, q)
                try:
                    content = item.get()
                    claims = content['claims']
                    if 'P225' in claims:
                        if len(claims['P225']) > 1:
                            print("  multiple taxon names")
                        else:
                            taxonname = claims['P225'][0].getTarget()
                            print("  taxon name is "+taxonname)
                            changed=False
                            labels = content['labels']
                            for lang in ['en', 'de', 'fr', 'es']:
                                if not lang in labels:
                                    labels[lang] = taxonname
                                    changed=True
                            if(changed):
                                print("  saving changes...");
                                item.editLabels(summary=u"set label to taxon name "+taxonname+" (task 1)", labels=labels)
                                log += u"* [["+q+"]]: "+taxonname+"\n"
                            
                                editcnt += 1
                                if(editcnt >= editmax):
                                    print("maximum number of edits reached")
                                    exit_reason = "maximum number of edits reached"
                                    break
                            else:
                                print("  no changes");
                    else:
                        print("  no taxon name given")
                except pywikibot.exceptions.NoPage:
                    print("  item does not exist")
        except Exception as exc:
            print("exception")
            traceback.print_exc()
            exit_reason = "exception"
        
        if log != "":
            log += "exit reason:"+exit_reason
            pageobj = pywikibot.Page(site, u"User:AkkakkBot/log")
            pageobj.put(log, u"log for task 1: set taxonname as label", minorEdit = False)