User:PotatoBot/Code/7
- !/usr/bin/python
- -*- coding: utf-8 -*-
import pywikibot as w
import scripts.noreferences, re, mysave
from datetime import date
- PotatoBot Task 7: Adds Glottolog codes to language infoboxes
def main():
datapage = 'Wikipedia:WikiProject Languages/Glottolog 2.2 language names'
errNobox, errIsSection, errMultiISO, errWrongGlotto, errMultiISOGlotto, errWrongISO, errNoISO = [], [], [], [], [], [], []
errIO, refAdded = ,
refbot = scripts.noreferences.NoReferencesBot(None, True)
for glottoline in w.Page(w.getSite(), datapage).get().splitlines():
# glottoline == ' aari1239 [aiw] Aari'
i = glottoline.find('[[ISO 639:')
if i > 1:
w.output('')
if '|' in glottoline[i+24:]:
pipe = glottoline.find('|', i+24)
langpage = mysave.resolveredir(w.Page(w.getSite(), glottoline[i+24 : pipe]))
glottoname = glottoline[pipe+1 : glottoline.find(']', pipe)]
else:
glottoname = glottoline[i+24 : glottoline.find(']', i+24)]
langpage = mysave.resolveredir(w.Page(w.getSite(), glottoname))
glottocode = glottoline[1 : glottoline.find(' ', 1)]
isocode = glottoline[i+10 : i+13]
w.output(langpage.title() + ' (' + isocode + '/' + glottocode + '):')
langbox = False
paramISO3, paramLC, paramLD, paramGlotto, paramGlottoref = , [], [], [],
if langpage.exists() and '#' not in langpage.title():
for template in langpage.templatesWithParams():
if template[0].title() == 'Template:Infobox language' or template[0].title() == 'Template:Infobox Language':
langbox = True
for param in template[1]:
pp = param.partition('=')
pname = pp[0].strip()
pval = pp[2].strip()
if pname == 'iso3':
paramISO3 = pval
paramLC.append(pval)
elif pname[:2] == 'lc' and pname[2:].isdigit():
paramLC.append(pval)
elif pname[:2] == 'ld' and pname[2:].isdigit():
paramLD.append('"' + pval + '"')
elif pname[:6] == 'glotto' and (len(pname) == 6 or pname[6:].isdigit()):
paramGlotto.append(pval)
elif pname == 'glottorefname':
paramGlottoref = pval
if len(paramLD) > 0:
if len(paramLD) == 1: paramLC += paramLD
else: paramLC.append('several dialect names')
entry = '#' + langpage.title() + ' (' + isocode + '/' + glottocode + ')'
if not langbox:
if '#' in langpage.title():
w.output(' \03{yellow}redirect to section\03{default}')
errIsSection.append(entry)
else:
w.output(' \03{yellow}no language infobox\03{default}')
errNobox.append(entry)
elif len(paramLC) > 1:
if len(paramGlotto) == 0:
w.output(' \03{yellow}multiple ISO codes, but no glottocode\03{default}')
errMultiISO.append(entry + ': ' + ', '.join(paramLC))
else:
w.output(' \03{yellow}multiple ISO codes and glottocode(s)\03{default}')
errMultiISOGlotto.append(entry + ': ' + ', '.join(paramLC) + ' / ' + ', '.join(paramGlotto))
else:
if len(paramGlotto) == 0:
langtext = langpage.get()
isoparam = re.search(r'\|\s*iso3\s*=[^\|\}]*', langtext)
if isoparam and paramISO3 != '':
if paramISO3 != isocode:
w.output(' \03{yellow}mismatching ISO code\03{default}')
errWrongISO.append(entry + ': ' + paramISO3)
else:
langtext = langtext[:isoparam.end()] + '|glotto=' + glottocode + '\n' +\
('|glottorefname=' + glottoname + '\n') * (paramGlottoref == '') + langtext[isoparam.end():]
summary = 'Add Glottolog code ' + glottocode
w.output(' NoReferencesBot says: ', newline = False)
if refbot.lacksReferences(langtext + ''):
langtext = refbot.addReferences(langtext)
summary += ' and <references />'
refAdded += entry + '\n'
errIO += mysave.savepage(langpage, langtext, '7', summary)
else:
w.output(' \03{yellow}no ISO code\03{default}')
errNoISO.append(entry)
elif len(paramGlotto) > 1 or paramGlotto[0] != glottocode:
w.output(' \03{yellow}mismatching glottocode(s)\03{default}')
errWrongGlotto.append(entry + ': ' + ', '.join(paramGlotto))
errNobox.sort()
errIsSection.sort()
errWrongGlotto.sort()
errMultiISO.sort()
errMultiISOGlotto.sort()
errWrongISO.sort()
errNoISO.sort()
mysave.savepage(w.Page(w.getSite(), 'User:PotatoBot/Lists/Glottolog log'),
'Log for the addition of Glottolog codes to language infoboxes (Task 7). Date: ' +\
mysave.fmtdate(date.today()) + '.\n\n' + "Syntax:" + '\n' + '#Some language (ISO 639-3 code/glottocode, from [[' +\
datapage + '|database]]): ISO codes and dialect names / glottocodes, from {{tl|Infobox language}} (if applicable)\n\n{{TOC right}}\n' +\
'=== No language infobox ===\n' + '\n'.join(errNobox) + '\n\n' +\
'=== Redirect to section ===\n' + '\n'.join(errIsSection) + '\n\n' +\
'=== One ISO code and mismatching glottocode(s) ===\n' + '\n'.join(errWrongGlotto) + '\n\n' +\
'=== Multiple ISO codes, but no glottocode ===\n' + '\n'.join(errMultiISO) + '\n\n' +\
'=== Multiple ISO codes and mismatching, missing or surplus glottocode(s) ===\n' + '\n'.join(errMultiISOGlotto) + '\n\n' +\
'=== Mismatching ISO code ===\n' + '\n'.join(errWrongISO) + '\n\n' +\
'=== No ISO code ===\n' + '\n'.join(errNoISO) + '\n\n' +\
'=== <references /> added ===\n' + refAdded +\
'=== I/O error ===\n' + errIO, '7', 'Creating Glottolog log')
if __name__ == "__main__":
try:
main()
finally:
w.stopme()
{{User:PotatoBot/Code/Save}}