Edukira joan

Lankide:EUmineralbot/kodea2

Wikipedia, Entziklopedia askea
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon May 13 23:48:29 2019

@author: jipsagak
"""

import pywikibot
from pywikibot import pagegenerators
import re

#Mineral_Mindat kategoriako  mineralen artikuluak hobetu formula eta sistema kristalinoa wikidatatik hartzen saiatuta
    
    
def formula (page1):
    item = pywikibot.ItemPage.fromPage(page1)  # this can be used for any page object, wikidatako elementua
    item.get()  # you need to call it to access any data.
    sitelinks = item.sitelinks
    
    if item.claims:
        if 'P274' in item.claims: #  formula kimikoa (P274) 
            formula1 =item.claims['P274'][0].getTarget()
            return (formula1)

def sist_kristalino (page1):
    item = pywikibot.ItemPage.fromPage(page1)  # this can be used for any page object, wikidatako elementua
    item.get()  # you need to call it to access any data.
    sitelinks = item.sitelinks
    if item.claims:
        if 'P556' in item.claims: #  sistema kristalinoa (P556) 
            kristal_esp = item.claims['P556'][0].getTarget() # Adibidez: [[wikidata:Q648961]]
            kristal_esp.get()  # you need to call it to access any data.
            sitelinks2 = kristal_esp.sitelinks
            if 'eu' in kristal_esp.labels:
                return ('[['+kristal_esp.labels['eu']+'|S'+kristal_esp.labels['eu'][9:]+'a]]')

    
def zuzendu_formula_edota_sitkristal (page):
    aldaketarik = False
#    site = pywikibot.Site('eu')
#    page = pywikibot.Page(site, u"Armalkolita")
    text = page.text
    print (text)
    if re.search('\s]]ren arabera sortzen ditu kristalak.',text):
        print ('aurkitua: ]]ren arabera sortzen ditu kristalak. ')
        SK = sist_kristalino (page)
        if SK != None:
            print (SK)
            text = re.sub(
                       r"\s]]ren arabera sortzen ditu kristalak.", 
                       ' '+SK+'ren arabera sortzen ditu kristalak.', 
                       text
                       )
            aldaketarik = True
        else:
            text = re.sub(
                       r"\s]]ren arabera sortzen ditu kristalak.", 
                       "", 
                       text
                       )
            aldaketarik = True
            
    if re.search(': \.',text):
        print ('aurkitua: :. arabera')
        F = formula(page)
        if F != None:
            print (F)
            text = re.sub(
                       r": \.", 
                       ': '+F+'.', 
                       text
                       )
            aldaketarik = True
    
    if aldaketarik:
        print (text)    
        page.text = text
        page.save(u"Formula edota kristal-sistema wikidatatik hartuta")

site = pywikibot.Site ('eu', 'wikipedia')
cat = pywikibot.Category(site,'Kategoria:Mineralak Mindat')
gen = pagegenerators.CategorizedPageGenerator(cat)
for page1 in gen:
    zuzendu_formula_edota_sitkristal (page1)