User:Moussekateer/editcount.py

From Team Fortress Wiki
< User:Moussekateer
Revision as of 01:01, 6 April 2012 by Moussekateer (talk | contribs) (Created page with "<pre>import urllib2, json, time, locale from operator import itemgetter locale.setlocale(locale.LC_ALL, '') wikiAddress = r'http://wiki.teamfortress.com/w/api.php?action=query&...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
import urllib2, json, time, locale
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'

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

populate_list()

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

outputString = """List of '''users by edit count'''. Data as of %s (GMT).

== List ==
Limited to the top 100.

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

n = 1
for user in sortedList[:100]:
	outputString += '\n' + """| %s || [[User:%s|%s]] || %s || <span style="display:none;">%s</span>%s
|-""" % (str(n), user['name'], user['name'], locale.format('%d', user['editcount'], grouping=True), 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')))
	n += 1
outputString += '\n|}'

file = open(r'edit_count_table.txt', 'wb')
file.write(outputString)