User:GrabberBot/update counts.py

From The Battle for Wesnoth Wiki
import re, urlgrabber.grabber, sys
import mediawiki

mw = mediawiki.MediaWiki()

allpages = urlgrabber.grabber.urlread("http://www.wesnoth.org/wiki/Special:Allpages&namespace=102")
allpages = re.compile('title="(.*?)"').findall(allpages)
counts = {}

allpages.remove("Special:Allpages")
print allpages
for page in allpages:
    print page,
    c = mw.fetch(page)
    count = 0
    if c:
        for line in c.splitlines():
            if re.compile(".*?://.*?\\.(gif|jpg|png|bmp|tga|jpeg)", re.I).match(line):
                count += 1
    counts[page] = count
    print count

allpages.sort()
newlist = ["* [[%s]] (%d)" % (x, counts[x]) for x in allpages]
newlist = "\n".join(newlist)
newlist = """
== Contributors ==

%s

== See Also ==""" % newlist

glib = mw.fetch("GraphicLibrary")
if glib and glib != newlist:
    glib = re.compile(r"\n== Contributors ==\s*(.*?)\s*\n== See Also ==", re.S).sub(newlist, glib)
    mw.post("GraphicLibrary", glib, "automated update of contributor stats")
    print "Successfully updated GraphicLibrary counts!"
This page was last edited on 18 February 2006, at 18:31.