User:BinBot/wd-adminlangs.py

See at User:BinBot/Languages spoken by admins

# -*- coding: utf-8 -*-
"""
This bot tries to get the languages spoken by any of the administrators from
Wikidata:List of administrators, and list them.
This may be useful if a contributor does not speak English and wonders if
there is an admin speaking his/her language.
The list might be inserted into the header of admin-related pages.
Currently it is written to a temporary location.
"""
# (C)(D)(E)(F)(G)(A)(H)(C) Bináris, 2012

import re
import wikipedia as pywikibot

# Initial settings
site = pywikibot.getSite('wikidata','wikidata')
page = pywikibot.Page(site,u'Wikidata:List of administrators')
page2 = pywikibot.Page(site,u'user:BinBot/Languages spoken by admins')
adminregex = re.compile(u'(?i)# *\[\[user:.*:?\]\],?(.*)')
#The last choice currently discards level codes. We regard only the language:
rubbish = re.compile(u'(\'| |\<.*>|\-\d)')
comment = u'Updating the language list of administrators'
langs = set()

# Go on
text = page.get()
for match in adminregex.finditer(text):
    # WMDE admins don't use their bits
    match = match if u'(WMDE)' not in match.group() else None
    try:
        langlist = match.group(1)
        langlist = rubbish.sub('',langlist)
        pywikibot.output(langlist)
        langs.update(set(langlist.split(',')))
        # print len(langs) # debug only
    except AttributeError:
        # Either WMDE or admin without languages
        pass

# Dump the result to Wikidata
text = "'''Languages spoken by Wikidata admins:''' "
text += ", ".join(sorted(list(langs))) + ' (%d)' % len(langs)
page2.put(text, comment)

# Good-bye!