Difference between revisions of "User:Moussekateer/editcount.py"

From Team Fortress Wiki
Jump to: navigation, search
m
m (Updated)
Line 6: Line 6:
  
 
wikiAddress = r'http://wiki.teamfortress.com/w/api.php?action=query&list=allusers&auprop=editcount|registration&auwitheditsonly&aulimit=500&format=json'
 
wikiAddress = r'http://wiki.teamfortress.com/w/api.php?action=query&list=allusers&auprop=editcount|registration&auwitheditsonly&aulimit=500&format=json'
 +
usernameSubs = {'Ohyeahcrucz': 'Cructo',
 +
'I-ghost': 'i-ghost'
 +
}
 +
bots = [r'FreemBOT', r'MousseBOT', r'PilkBOT', r'SebBOT', r'WindBOT']
  
 
usersList = []
 
usersList = []
Line 22: Line 26:
 
else:
 
else:
 
return 1
 
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}+
 +
| {{{{Chart bar|{count}|max={max}}}}}
 +
| {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}
 +
| {{{{Chart bar|{count}|max={max}}}}}
 +
| {percentage}%""".format(nlower = nlower,
 +
nupper = nupper,
 +
count = userEditCount(nlower, nupper),
 +
max = len(usersList),
 +
percentage = str('%.2f' % (100 * float(userEditCount(nlower, nupper))/float(len(usersList))))
 +
)
  
 
populate_list()
 
populate_list()
Line 27: Line 63:
 
sortedList = sorted(usersList, key=itemgetter('editcount'), reverse=True)
 
sortedList = sorted(usersList, key=itemgetter('editcount'), reverse=True)
  
outputString = """List of '''users by edit count'''. Data as of %s (GMT).
+
outputString = """User edits statistics. Data accurate as of {0} (GMT)""".format(time.strftime(r'%H:%M, %d %B %Y', time.gmtime()))
 +
 
 +
outputString += """\n\n== Edit count distribution ==
 +
{| class="wikitable grid sortable plainlinks" style="text-align: center;width=50%;"
 +
|-
 +
! class="header" width="30%" | Number of edits
 +
! class="header" width="50%" | Count
 +
! class="header" width="20%" | Percentage of users"""
 +
 
 +
outputString += addTableRow(1, 10)
 +
outputString += addTableRow(11, 100)
 +
outputString += addTableRow(101, 1000)
 +
outputString += addTableRow(1001, 5000)
 +
outputString += addTableRow(5001)
 +
outputString += """\n|}"""
 +
 
  
== List ==
+
outputString += """\n\n== Top editors list ==
 
Limited to the top 100.
 
Limited to the top 100.
  
{| class="wikitable grid sortable"
+
{{| class="wikitable grid sortable"
 
|-
 
|-
 
! class="header" | #
 
! class="header" | #
Line 39: Line 90:
 
! class="header" | Edits per day
 
! class="header" | Edits per day
 
! class="header" | Registration date
 
! class="header" | Registration date
|-""" % time.strftime(r'%H:%M, %d %B %Y', time.gmtime())
+
|-""".format(time.strftime(r'%H:%M, %d %B %Y', time.gmtime()))
  
 
n = 1
 
n = 1
 
timenow = datetime.now()
 
timenow = datetime.now()
 
for user in sortedList[:100]:
 
for user in sortedList[:100]:
userstarttime = datetime.strptime(user['registration'], r'%Y-%m-%dT%H:%M:%SZ')
+
username = user['name']
timedelta = timenow-userstarttime
+
usereditcount = user['editcount']
editsperday = ('%.2f' % (float(user['editcount'])/float(timedelta.days)))
+
userregistration = user['registration']
 +
if username in usernameSubs:
 +
username = usernameSubs[username]
 +
userstarttime = datetime.strptime(userregistration, r'%Y-%m-%dT%H:%M:%SZ')
 +
timedelta = timenow - userstarttime
 +
editsperday = ('%.2f' % (float(usereditcount)/float(timedelta.days)))
  
outputString += '\n' + """| %s || [[User:%s|%s]] || %s || %s || <span style="display:none;">%s</span>%s
+
outputString += '\n' + """| {n} || [[User:{username}|{username}]] || {editcount} || {editday} || <span style="display:none;">{sortabledate}</span>{date}
|-""" % (str(n), user['name'], user['name'], locale.format('%d', user['editcount'], grouping=True), str(editsperday), time.strftime(r'%Y-%m-%d %H:%M:00', time.strptime(user['registration'], r'%Y-%m-%dT%H:%M:%SZ')), time.strftime(r'%H:%M, %d %B %Y', time.strptime(user['registration'], r'%Y-%m-%dT%H:%M:%SZ')))
+
|-""".format(n = str(n),
 +
username = username,
 +
editcount = locale.format('%d', usereditcount, grouping=True),
 +
editday = str(editsperday),
 +
sortabledate = time.strftime(r'%Y-%m-%d %H:%M:00', time.strptime(userregistration, r'%Y-%m-%dT%H:%M:%SZ')),
 +
date = time.strftime(r'%H:%M, %d %B %Y', time.strptime(userregistration, r'%Y-%m-%dT%H:%M:%SZ'))
 +
)
 
n += 1
 
n += 1
 
outputString += '\n|}'
 
outputString += '\n|}'
  
 
file = open(r'edit_count_table.txt', 'wb')
 
file = open(r'edit_count_table.txt', 'wb')
file.write(outputString)</pre>
+
file.write(outputString)
 +
print 'Article written to edit_count_table.txt'</pre>

Revision as of 06:13, 27 June 2012

import urllib2, json, time, locale
from datetime import datetime
from operator import itemgetter

locale.setlocale(locale.LC_ALL, '')

wikiAddress = r'http://wiki.teamfortress.com/w/api.php?action=query&list=allusers&auprop=editcount|registration&auwitheditsonly&aulimit=500&format=json'
usernameSubs = {'Ohyeahcrucz': 'Cructo',
				'I-ghost': 'i-ghost'
				}
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}+
| {{{{Chart bar|{count}|max={max}}}}}
| {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}
| {{{{Chart bar|{count}|max={max}}}}}
| {percentage}%""".format(nlower = nlower,
						 nupper = nupper,
						 count = userEditCount(nlower, nupper),
						 max = len(usersList),
						 percentage = str('%.2f' % (100 * float(userEditCount(nlower, nupper))/float(len(usersList))))
						 )

populate_list()

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

outputString = """User edits statistics. Data accurate as of {0} (GMT)""".format(time.strftime(r'%H:%M, %d %B %Y', time.gmtime()))

outputString += """\n\n== Edit count distribution ==
{| class="wikitable grid sortable plainlinks" style="text-align: center;width=50%;"
|-
! class="header" width="30%" | Number of edits
! class="header" width="50%" | Count
! class="header" width="20%" | Percentage of users"""

outputString += addTableRow(1, 10)
outputString += addTableRow(11, 100)
outputString += addTableRow(101, 1000)
outputString += addTableRow(1001, 5000)
outputString += addTableRow(5001)
outputString += """\n|}"""


outputString += """\n\n== Top editors list ==
Limited to the top 100.

{{| class="wikitable grid sortable"
|-
! class="header" | #
! class="header" | User
! class="header" | Edit count
! class="header" | Edits per day
! class="header" | Registration date
|-""".format(time.strftime(r'%H:%M, %d %B %Y', time.gmtime()))

n = 1
timenow = datetime.now()
for user in sortedList[:100]:
	username = user['name']
	usereditcount = user['editcount']
	userregistration = user['registration']
	if username in usernameSubs:
		username = usernameSubs[username]
	userstarttime = datetime.strptime(userregistration, r'%Y-%m-%dT%H:%M:%SZ')
	timedelta = timenow - userstarttime
	editsperday = ('%.2f' % (float(usereditcount)/float(timedelta.days)))

	outputString += '\n' + """| {n} || [[User:{username}|{username}]] || {editcount} || {editday} || <span style="display:none;">{sortabledate}</span>{date}
|-""".format(n = str(n),
			 username = username,
			 editcount = locale.format('%d', usereditcount, grouping=True),
			 editday = str(editsperday),
			 sortabledate = time.strftime(r'%Y-%m-%d %H:%M:00', time.strptime(userregistration, r'%Y-%m-%dT%H:%M:%SZ')),
			 date = time.strftime(r'%H:%M, %d %B %Y', time.strptime(userregistration, r'%Y-%m-%dT%H:%M:%SZ'))
			 )
	n += 1
outputString += '\n|}'

file = open(r'edit_count_table.txt', 'wb')
file.write(outputString)
print 'Article written to edit_count_table.txt'