User:Moussekateer/editcount.py

From Team Fortress Wiki
< User:Moussekateer
Revision as of 06:40, 2 July 2014 by Darkid (talk | contribs) (Updated to include "user signups")
Jump to: navigation, search

import urllib2, json, time, locale from datetime import datetime from datetime import date from operator import itemgetter global NUMYEARS NUMYEARS = date.today().year-2010 + 1 # 2014 - 2010 + 1 = 5 (years)

locale.setlocale(locale.LC_ALL, )

wikiAddress = r'http://wiki.teamfortress.com/w/api.php?action=query&list=allusers&auprop=editcount%7Cregistration&auwitheditsonly&aulimit=500&format=json' usernameSubs = {'Ohyeahcrucz': 'Cructo', 'I-ghost': 'i-ghost', 'Darkid': 'darkid' } bots = [r'FreemBOT', r'MousseBOT', r'PilkBOT', r'SebBOT', r'WindBOT']

usersList = []

def populate_list(aufrom=None): global usersList if aufrom: result = json.loads(urllib2.urlopen(wikiAddress + r'&aufrom=' + aufrom).read()) else: result = json.loads(urllib2.urlopen(wikiAddress).read()) list = result['query']['allusers'] usersList += list print 'User count:', str(len(usersList)) if 'query-continue' in result: populate_list(aufrom=result['query-continue']['allusers']['aufrom']) else: return 1

def userEditCount(nlower, nupper=None): count = 0 for user in sortedList: if nupper is None: if user['editcount'] >= nlower: count += 1 else: if nlower <= user['editcount'] and user['editcount'] <= nupper: count += 1 return count

def addTableRow(nlower, nupper=None): if nupper is None: return """\n|- | {nlower}+ | {{count}}} | {percentage}%""".format(nlower = nlower, count = userEditCount(nlower), max = len(usersList), percentage = str('%.2f' % (100 * float(userEditCount(nlower))/float(len(usersList)))) ) else: return """\n|- | {nlower} - {nupper} | {{count}}} | {percentage}%""".format(nlower = nlower, nupper = nupper, count = userEditCount(nlower, nupper), max = len(usersList), percentage = str('%.2f' % (100 * float(userEditCount(nlower, nupper))/float(len(usersList)))) )

def monthName(n): return {1: "January", 2: "February", 3: "March", 4: "April", 5: "May", 6: "June", 7: "July", 8: "August", 9: "September", 10: "October", 11: "November", 12: "December", }[n]

def addTimeData(timeSortedList): timeRange = [[0]*12 for i in range(NUMYEARS)] #[year][month] for user in timeSortedList: time = user['registration'] timeRange[int(time[:4])-2010][int(time[5:7])-1] += 1 # 2013-05 -> 3, 4 runningTotal = 0 output = "" for year in range(NUMYEARS): for month in range(1,12): numUsers = timeRange[year][month-1] if numUsers == 0: continue # Time segment has no data, just skip. runningTotal += numUsers output += """\n|- | {year}{month}{monthName} {year} | {{numUsers}} | {total}""".format(numUsers = numUsers, month = month, monthName = monthName(month), year = year+2010, total = runningTotal) return output

populate_list()

sortedList = sorted(usersList, key=itemgetter('editcount'), reverse=True) timeSortedList = sorted(usersList, key=itemgetter('registration'))

outputString = """User edits statistics. Data accurate as of {0} (GMT). Further stats available at stats.wiki.tf

Note: All data excludes registered users with no edits.""".format(time.strftime(r'%H
%M, %d %B %Y', time.gmtime()))

outputString += """\n\n== Edit count distribution ==