Difference between revisions of "User:GrabberBot/poster.py"

From The Battle for Wesnoth Wiki
(pasted code)
 
 
Line 29: Line 29:
 
     data = """\
 
     data = """\
 
This list is generated by a bot, and contains links to plain image attachements
 
This list is generated by a bot, and contains links to plain image attachements
which were posted to the forums recently. The bot can be found here:
+
which were posted to the forums recently. The bot is here: [[User:GrabberBot]]
http://www.wesnoth.org/forum/viewtopic.php?t=6078 The idea is to make it easier
+
The idea is to make it easier to keep the [[GraphicLibrary]] updated with new graphics, if not, I'll just
to keep the [[GraphicLibrary]] updated with new graphics, if not, I'll just
 
 
remove it from my crontab again :)
 
remove it from my crontab again :)
  
Line 39: Line 38:
 
image, click the '''X'''. The effect won't be immediate, it merely will schedule
 
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
 
the command on a site I'm mis-using for this (so if you have a machine where you
can run the python script, by all means, let me know). For now, when my computer
+
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
 
is switched on the next time, the script will be run and it will read all the
 
scheduled commands and update the links.
 
scheduled commands and update the links.

Latest revision as of 13:22, 1 November 2005

#!/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.