User:MastCell/ContribCheckerCL


 * 1) !/usr/bin/python


 * 1) ContribChecker 1.0 command-line version
 * 2) Created 12/16/2009
 * 3) Requires mwclient library 0.63b or later from sourceforge
 * 4) Requires an active Internet connection
 * 5) Available for re-use and modification without restriction
 * 6) Description:
 * 7)    A command-line Python script which will pull all contributions
 * 8)    by a given editor of English Wikipedia to a specific article
 * 9)    and output them as sequential tab-delimited rows.
 * 10) Usage (assuming you name this file 'check'):
 * 11)    check
 * 12) Parameters:
 * 13)    : A Wikipedia user name (case-sensitive)
 * 14)    : A page name from English Wikipedia (en.wikipedia.org)
 * 15)    Usernames and page names are case-sensitive
 * 16)    On UNIX-type systems, multi-word page names must be passed in single quotes
 * 17) Example:
 * 18)    check MastCell 'Acute myeloid leukemia'
 * 19) Output:
 * 20)    Output will be in sequential tab-delimited rows, one edit per row, in the form:
 * 21)    TAB TAB (if any)
 * 1)    check MastCell 'Acute myeloid leukemia'
 * 2) Output:
 * 3)    Output will be in sequential tab-delimited rows, one edit per row, in the form:
 * 4)    TAB TAB (if any)
 * 1)    TAB TAB (if any)

import mwclient import sys
 * 1) Imports

def checkContribs(userName, pageName): wpHandle = mwclient.Site('en.wikipedia.org') wpPage = wpHandle.Pages[pageName] revs = wpPage.revisions(user=userName) numHits = listRevs(revs) # Finish up by printing number of edits found... if (numHits == 0): print "No edits to " + pageName + " by " + userName + " were found." else: print "Found " + str(numHits) + " edits to " + pageName + " by " + userName + "."
 * 1) Function: checkContribs
 * 2) Parameters: userName (string), pageName (string)
 * 3) Return value: None

def listRevs(revlist): numRevs = 0 for rev in revlist: timestamp = rev.get(u'timestamp') timeStr = '{0}-{1}-{2} {3}:{4}'.format(timestamp.tm_mon, timestamp.tm_mday, timestamp.tm_year, str(timestamp.tm_hour).zfill(2), str(timestamp.tm_min).zfill(2)) if (rev.get(u'comment') != None): edsumStr = rev.get(u'comment') else: edsumStr = "" userStr = rev.get(u'user') printOneRev(timeStr, userStr, edsumStr) numRevs += 1 return numRevs    # Return number of hits
 * 1) Function: listRevs
 * 2) Parameters: revList (list of revision objects)
 * 3) Return value: Number of edits in revList (int)

def printOneRev(timeString, userString, editsumString): print timeString + "\t" + userString + "\t" + editsumString
 * 1) Function: printOneRev
 * 2) Parameters: timeString (string), userString (string), editsumString (string)
 * 3) Return value: None
 * 4) Notes: Separated this out mostly so that it's easy to adjust the output format if desired

def printProperUsage: print "Welcome to ContribChecker." print "This utility will check a user's contributions to a given page on English Wikipedia." print "" print "Proper usage: ContribChecker "
 * 1) Function: printProperUsage
 * 2) Parameters: None
 * 3) Return value: None
 * 4) Notes: Bare-bones "help" message if an incorrect number of command-line arguments are supplied

if (len(sys.argv) != 3): printProperUsage else: checkContribs(sys.argv[1], sys.argv[2])
 * 1) MAIN PROGRAM
 * 1) MAIN PROGRAM