summaryrefslogtreecommitdiffstats
path: root/wiki.py
blob: 6fd3695858242753c816c1ded55e67638fb8ae42 (plain)
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
#!/usr/bin/python -tt
# coding: utf-8
#
# Copyright (C) 2008 Red Hat Inc.
# Author: Luke Macken <lmacken@redhat.com>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
# the full text of the license.

"""
Fedora Weekly Wiki Metrics
"""

import simplejson
from urllib import urlencode
from urllib2 import urlopen
from datetime import datetime, timedelta

now = datetime.utcnow()
then = now - timedelta(days=7)

query = urlencode({
    'list'    : 'recentchanges',
    'format'  : 'json',
    'action'  : 'query',
    'rcprop'  : 'user|title',
    'rcend'   : then.isoformat().split('.')[0] + 'Z',
    'rclimit' : 500,
})
data = simplejson.load(urlopen('http://fedoraproject.org/w/api.php', query))

print "From %s to %s" % (then, now)
num_changes = len(data['query']['recentchanges'])
print "%d wiki changes in the past week" % num_changes
if num_changes == 500:
    print "Warning: Number of changes exceeds the API return limit."
    print "These results will not be accurate unless run from bot account."

users = {} # {username: [change,]}
pages = {} # {pagename: # of edits}
for change in data['query']['recentchanges']:
    if not change['user'] in users:
        users[change['user']] = []
    users[change['user']].append(change['title'])
    if not change['title'] in pages:
        pages[change['title']] = 0
    pages[change['title']] += 1

print '\n== Most active wiki users =='
for user, changes in sorted(users.items(),
                            cmp=lambda x, y: cmp(len(x[1]), len(y[1])),
                            reverse=True)[:10]:
    print ' %-50s %d' % (('[[:User:%s]]' % user).ljust(50, '.'), len(changes))

print '\n== Most edited pages =='
for page, num in sorted(pages.items(),
                        cmp=lambda x, y: cmp(x[1], y[1]),
                        reverse=True)[:10]:
    print ' %-50s %d' % (('[[%s]]' % page).ljust(50, '.'), num)