#!/usr/bin/python -tt # coding: utf-8 # # Copyright (C) 2008 Red Hat Inc. # Author: Luke Macken # # 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 Wiki Client """ import os from datetime import datetime, timedelta from pysqlite2 import dbapi2 as sqlite from collections import defaultdict from ConfigParser import ConfigParser from fedora.client import BaseClient class FedoraWiki(BaseClient): def __init__(self): cookies = self._get_firefox_cookies('fedoraproject.org') print cookies super(FedoraWiki, self).__init__('http://fedoraproject.org/w/', session_name='fpo-mediawiki_en_Token', session_id=cookies['fpo-mediawiki_en_Token'], username=cookies['fpo-mediawiki_en_UserName'], debug=True) def get_recent_changes(self, now, then, limit=600): """ Get recent wiki changes from ``now`` until ``then`` """ data = self.send_request('api.php', req_params={ 'list' : 'recentchanges', 'action' : 'query', 'format' : 'json', 'rcprop' : 'user|title', 'rcend' : then.isoformat().split('.')[0] + 'Z', 'rclimit' : limit, }, auth=True) if 'error' in data: raise Exception(data['error']['info']) return data def print_recent_changes(self, days=7, show=10): now = datetime.utcnow() then = now - timedelta(days=days) print "From %s to %s" % (then, now) data = self.get_recent_changes(now=now, then=then) 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 = defaultdict(list) # {username: [change,]} pages = defaultdict(int) # {pagename: # of edits} for change in data['query']['recentchanges']: users[change['user']].append(change['title']) 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)[:show]: 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)[:show]: print ' %-50s %d' % (('[[%s]]' % page).ljust(50, '.'), num) def _get_firefox_cookies(self, domain='fedoraproject.org'): """ Returns all firefox 3 cookies for a given domain """ cookies = {} profiles = os.path.expanduser('~/.mozilla/firefox/profiles.ini') config = ConfigParser() config.read(profiles) profile = config.get('Profile0', 'Path') cookiedb = os.path.expanduser('~/.mozilla/firefox/%s/cookies.sqlite' % profile) conn = sqlite.connect(cookiedb) c = conn.cursor() c.execute("SELECT name, value FROM moz_cookies WHERE host = '%s'" % domain) for row in c.fetchall(): cookies[row[0].encode('utf-8')] = row[1].encode('utf-8') return cookies if __name__ == '__main__': wiki = FedoraWiki() wiki.print_recent_changes()