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

From Team Fortress Wiki
Jump to: navigation, search
(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&...")
 
m
Line 1: Line 1:
 
<pre>import urllib2, json, time, locale
 
<pre>import urllib2, json, time, locale
 +
from datetime import datetime
 
from operator import itemgetter
 
from operator import itemgetter
  
Line 36: Line 37:
 
! class="header" | User
 
! class="header" | User
 
! class="header" | Edit count
 
! class="header" | Edit count
 +
! class="header" | Edits per day
 
! class="header" | Registration date
 
! class="header" | Registration date
 
|-""" % time.strftime(r'%H:%M, %d %B %Y', time.gmtime())
 
|-""" % time.strftime(r'%H:%M, %d %B %Y', time.gmtime())
  
 
n = 1
 
n = 1
 +
timenow = datetime.now()
 
for user in sortedList[:100]:
 
for user in sortedList[:100]:
outputString += '\n' + """| %s || [[User:%s|%s]] || %s || <span style="display:none;">%s</span>%s
+
userstarttime = datetime.strptime(user['registration'], r'%Y-%m-%dT%H:%M:%SZ')
|-""" % (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')))
+
timedelta = timenow-userstarttime
 +
editsperday = ('%.2f' % (float(user['editcount'])/float(timedelta.days)))
 +
 
 +
outputString += '\n' + """| %s || [[User:%s|%s]] || %s || %s || <span style="display:none;">%s</span>%s
 +
|-""" % (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')))
 
n += 1
 
n += 1
 
outputString += '\n|}'
 
outputString += '\n|}'

Revision as of 01:26, 6 April 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'

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" | Edits per day
! class="header" | Registration date
|-""" % time.strftime(r'%H:%M, %d %B %Y', time.gmtime())

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

	outputString += '\n' + """| %s || [[User:%s|%s]] || %s || %s || <span style="display:none;">%s</span>%s
|-""" % (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')))
	n += 1
outputString += '\n|}'

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