User:GrabberBot/poster.py

From The Battle for Wesnoth Wiki
#!/usr/bin/python

import sys, re, os
import mediawiki

url = "UnsortedContrib"

# read data to post
try:
    post = file("post.txt", "r").read().strip()
except IOError:
    sys.exit(0)

if post == "":
    sys.exit(0)

# open wiki page for editing, read current contents
mw = mediawiki.MediaWiki()
data = mw.fetch(url)

# find part of page to replace
mob = re.compile("== Recently posted images ==(.*)== See Also ==",
    re.S).search(data)
if mob:
    images = mob.group(1).strip()
    data = data[:mob.start(1)] + "\n\n" + images + "\n" + post + "\n\n" + data[mob.end(1):]
else:
    data = """\
This list is generated by a bot, and contains links to plain image attachements
which were posted to the forums recently. The bot is here: [[User:GrabberBot]]
The idea is to make it easier to keep the [[GraphicLibrary]] updated with new graphics, if not, I'll just
remove it from my crontab again :)

You are encouraged to remove images which don't fit (e.g. screenshots, concept
art, off-topic images), and move proper unit graphics over to the
[[GraphicLibrary]]. To move an image, click on the '''Move''' link. To delete an
image, click the '''X'''. The effect won't be immediate, it merely will schedule
the command on a site I'm mis-using for this (so if you have a machine where you
can run the python script once a day or so, by all means, let me know). For now, when my computer
is switched on the next time, the script will be run and it will read all the
scheduled commands and update the links.

== Recently posted images ==

%s

== See Also ==

* [[GraphicLibrary]]
""" % post

# post to the wiki
n = len(post.splitlines())
s = "s"
if n == 1: s = ""
mw.post(url, data, "added %d new forum image%s" % (n, s))

os.remove("post.txt")
This page was last edited on 1 November 2005, at 13:22.