Difference between revisions of "User:Moussekateer/editcount.py"
Moussekateer (talk | contribs) m (Updated) |
(Hopefully none of this needs escaping.) |
||
Line 63: | Line 63: | ||
sortedList = sorted(usersList, key=itemgetter('editcount'), reverse=True) | 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 = """User edits statistics. Data accurate as of {0} (GMT). Further stats available at [http://stats.wiki.tf/wiki/tf] |
+ | ;Note<nowiki>:</nowiki> 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 == | outputString += """\n\n== Edit count distribution == | ||
Line 69: | Line 70: | ||
|- | |- | ||
! class="header" width="30%" | Number of edits | ! class="header" width="30%" | Number of edits | ||
− | ! class="header" width="50%" | | + | ! class="header" width="50%" | Users |
! class="header" width="20%" | Percentage of users""" | ! class="header" width="20%" | Percentage of users""" | ||
Revision as of 05:34, 1 July 2014
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). Further stats available at [http://stats.wiki.tf/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 == {| class="wikitable grid sortable plainlinks" style="text-align: center;width=50%;" |- ! class="header" width="30%" | Number of edits ! class="header" width="50%" | Users ! 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'