User:Ritchie333/rfatally.py

From Wikipedia, the free encyclopedia
# A pywikibot script to dump out support/oppose/neutral totals of any RfAs currently in progress
import re
import pywikibot
from pywikibot import pagegenerators

def getTotals( text ):
  lines = text.splitlines();
  support = False
  oppose = False
  neutral = False
  nSupport = 0
  nOppose = 0
  nNeutral = 0
  for line in lines:
    if( re.match( '^\s*=====Support=====\s*$', line ) is not None):
      support = True
      oppose = False
      neutral = False
    if( re.match( '^\s*=====Oppose=====\s*$', line ) is not None):
      support = False
      oppose = True
      neutral = False
    if( re.match( '^\s*=====Neutral=====\s*$', line ) is not None):
      support = False
      oppose = False
      neutral = True
    if( re.match( '^\s*=====General comments=====\s*$', line ) is not None):
      support = False
      oppose = False
      neutral = False
    if( re.search( '^\s*#[^:]', line ) is not None):
      if( support ):
        nSupport += 1
      if( oppose ):
        nOppose += 1
      if( neutral ):
        nNeutral += 1
  return( nSupport, nOppose, nNeutral )

site = pywikibot.Site()
rfaPage = list( site.allpages( prefix='Requests for adminship', namespace=4, total=1 ) )[ 0 ]
for rfa in rfaPage.linkedPages(namespaces=4, content=True):
  result = re.match( 'Wikipedia:Requests for adminship/(.*)', rfa.title() )
  if result is not None:
    rfaName = result.group(1)
    text = rfa.text
    if( re.search( '{{RfA tally\|' + rfaName + '}}', text ) is not None):
      totals = getTotals( text )
      print( rfaName + '=(' + str( totals[ 0 ] ) + '/' + str( totals[ 1 ] ) + '/' + str( totals[ 2 ] ) + ')' )