forked from jbzdarkid/TFWiki-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit_stats.py
167 lines (152 loc) · 5.21 KB
/
edit_stats.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
from __future__ import division
from datetime import date, datetime
from operator import itemgetter
from time import strftime, strptime
from utils import time_and_date
from wikitools import wiki
verbose = False
NUMYEARS = date.today().year-2010 + 1 # 2014 - 2010 + 1 = 5 (years)
def userEditCount(sortedList, nlower, nupper=None):
count = 0
for user in sortedList:
if nlower <= user['editcount']:
if nupper is None or user['editcount'] <= nupper:
count += 1
return count
def addTableRow(sortedList, nlower, nupper=None):
if verbose:
print("Adding users with edit count", nlower, "-", nupper)
count = userEditCount(sortedList, nlower, nupper)
if nupper is None:
return """|-
| {nlower}+
| {{{{Chart bar|{count}|max={max}}}}}
| {percentage}%""".format(nlower = nlower,
count = count,
max = len(sortedList),
percentage = round(100 * float(count) / len(sortedList), 2)
)
else:
return """|-
| {nlower} - {nupper}
| {{{{Chart bar|{count}|max={max}}}}}
| {percentage}%""".format(nlower = nlower,
nupper = nupper,
count = count,
max = len(sortedList),
percentage = round(100 * float(count) / len(sortedList), 2)
)
def monthName(n):
return [
None,
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
][n]
def addTimeData(timeSortedList):
if verbose:
print("Adding user signups")
timeRange = [[0]*12 for i in range(NUMYEARS)] # timeRange[year][month]
for user in timeSortedList:
time = user['registration']
timeRange[int(time[:4])-2010][int(time[5:7])-1] += 1 # 2013-05 -> year 3, month 4
runningTotal = 0
output = ""
for year in range(2010, 2010+NUMYEARS):
for month in range(1, 13):
if year == date.today().year and month == date.today().month:
break # We've reached the present, so current data is incorrect and future data is blank.
numUsers = timeRange[year-2010][month-1]
if numUsers == 0:
continue # No data for given time period
runningTotal += numUsers
output += """|-
| data-sort-value="{year}-{month}" | {monthName} {year}
| {{{{Chart bar|{numUsers}|max=3500}}}}
| {total}\n""".format(numUsers = numUsers,
month = "%02d" % month,
monthName = monthName(month),
year = year,
total = runningTotal)
return output
def addTopUsers(w, sortedList, count):
if verbose:
print("Adding top", count, "users")
output = ""
i = 0
while i < count:
user = sortedList[i]
username = user['name']
usereditcount = user['editcount']
userregistration = user['registration']
userlink = 'User:'+username
place = i+1 # List is indexed 0-99, editors are indexed 1-100
if 'BOT' in username or username in ['Pillownet', 'PhoneWave']:
place = "<small>''BOT''</small>"
del sortedList[i]
i -= 1
userstarttime = strptime(userregistration, r'%Y-%m-%dT%H:%M:%SZ')
timedelta = (datetime.now() - datetime(*userstarttime[:6])).days
editsperday = round(float(usereditcount) / timedelta, 2)
output += u"""|-
| {place} || [[{userlink}|{username}]] || {editcount} || {editday}
| data-sort-value="{sortabledate}" | {date}\n""".format(
place = place, # List is indexed 0-99, editors are indexed 1-100
userlink = userlink,
username = username,
editcount = usereditcount,
editday = str(editsperday),
sortabledate = strftime(r'%Y-%m-%d %H:%M:00', userstarttime),
date = strftime(r'%H:%M, %d %B %Y', userstarttime),
)
i += 1
return output
def main(w):
usersList = list(w.get_all_users())
sortedList = sorted(usersList, key=itemgetter('editcount'), reverse=True)
timeSortedList = sorted(usersList, key=itemgetter('registration'))
output = """User edits statistics. Data accurate as of """ + time_and_date() + """.
;Note: All data excludes registered users with no edits.
== Edit count distribution ==
{| class="wikitable grid sortable plainlinks" style="text-align: center"
! class="header" width="30%" | Number of edits
! class="header" width="50%" | Users
! class="header" width="20%" | Percentage of users
""" + addTableRow(sortedList, 1, 10) + """
""" + addTableRow(sortedList, 11, 100) + """
""" + addTableRow(sortedList, 101, 1000) + """
""" + addTableRow(sortedList, 1001, 5000) + """
""" + addTableRow(sortedList, 5001) + """
|}
== User signups ==
{| class="wikitable grid sortable plainlinks" style="text-align:center"
! class="header" width="30%" | Date
! class="header" width="50%" | Signups
! class="header" width="20%" | Total number of users
""" + addTimeData(timeSortedList) + """
|}
== Top 100 editors ==
{| class="wikitable grid sortable"
! class="header" data-sort-type="number" | #
! class="header" | User
! class="header" | Edit count
! class="header" | Edits per day
! class="header" | Registration date
""" + addTopUsers(w, sortedList, 100) + """
|}"""
return output
if __name__ == '__main__':
verbose = True
w = wiki.Wiki('https://wiki.teamfortress.com/w/api.php')
with open('wiki_edit_stats.txt', 'w', encoding='utf-8') as f:
f.write(main(w))
print(f'Article written to {f.name}')