Jump to content

User:MastCell/ContribCheckerCL

From Wikipedia, the free encyclopedia
#!/usr/bin/python

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


# Imports
import mwclient
import sys


# Function: checkContribs
# Parameters: userName (string), pageName (string)
# Return value: None
# ------------------------------------------------
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 + "."


# Function: listRevs
# Parameters: revList (list of revision objects)
# Return value: Number of edits in revList (int)
# --------------------------------------------------
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


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


# Function: printProperUsage
# Parameters: None
# Return value: None
# Notes: Bare-bones "help" message if an incorrect number of command-line arguments are supplied
# ----------------------------------------------------------------------------------------------
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 <username> <page>"

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