import pywikibot, re
from pywikibot import pagegenerators as pg
def uncapitalize(s):
if len(s) == 0:
return s
else:
return s[0].lower() + s[1:]
casite = pywikibot.Site("ca", "wikipedia")
allpages = pg.AllpagesPageGenerator(site=casite, start="!", namespace=0, includeredirects = False)
pages = pg.PreloadingGenerator(allpages, pageNumber = 100)
for page in pages:
try:
item = pywikibot.ItemPage.fromPage(page)
item.get()
except:
print("Error 1")
continue
if 'ca' not in item.labels:
print("Error 2")
continue
print()
print('Element: ' + item.title())
if 'en' in item.labels:
print('Label (en): ' + item.labels['en'])
print('Label (ca): ' + item.labels['ca'])
if(item.labels['ca'][0].isupper()):
if re.search( r"'''\s*"+re.escape(uncapitalize(item.labels['ca']))+"\s*'''", page.text):
if not re.search( r"'''\s*"+re.escape(item.labels['ca'])+"\s*'''", page.text):
item.editLabels(labels={'ca': uncapitalize(item.labels['ca'])}, summary=u'Changed Catalan label to lower case')