Jump to content

User:Lowercase sigmabot II/Source.py

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lowercase sigmabot II (talk | contribs) at 02:22, 1 May 2012 (Updating source) (bot). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Source as of 02:22, 1 May 2012 (UTC).

Sandbot1.py

This task runs every minute. It checks if the following sandboxes have the \{\{tl|Sandbox heading\}\} template;

If the template is not present on the sandbox, it prepends \{\{xt|\{\{tl|Sandbox heading\}\} <!-- Please leave this line alone! -->

\}\} to the page.

If the template is present, it does nothing.

&(sandbot1)s

Sandbot2.py

This task runs once per hour. It clears the sandboxes by replacing the content with the sandbox header, which looks like this:

\{\{subst:X1/sandbox/sandbox header\}\}

#!/usr/bin/python
# -*- coding: utf-8 -*-

from wikibot import WikiBot
from datetime import datetime, timedelta
import time
import sys
import os

user = "Lowercase sigmabot II"
pwfile = open(os.path.expanduser("~/wikibot/password.txt"), "r")
pw = pwfile.read().replace("\n", ""); pwfile.close(); del pwfile;
api = WikiBot("http://en.wikipedia.org/w/api.php")
api.login(user, pw)
api.set_all()
reset_text = """{{Please leave this line alone (sandbox heading)}}<!--
*               Welcome to the sandbox!              *
*            Please leave this part alone            *
*           The page is cleared regularly            *
*     Feel free to try your editing skills below     *
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-->
"""

sandboxes = ("Wikipedia:Sandbox", "Wikipedia:Tutorial/Editing/sandbox", "Wikipedia:Tutorial/Formatting/sandbox", "Wikipedia:Tutorial/Wikipedia links/sandbox", "Wikipedia:Tutorial/Citing sources/sandbox", "Wikipedia:Tutorial/Keep in mind/sandbox")

def should_be_reset(box):
    curtime = datetime.utcnow()
    three_days = timedelta(hours=72)
    query = api.call({"action":"query", "prop":"revisions", "titles":box, "rvprop":"timestamp"})["query"]["pages"]
    pageid = query.keys()[0]
    box_stamp = api.parse_date(query[pageid]["revisions"][0]["timestamp"])
    if box_stamp < curtime - three_days:
        return True
    return False

for sandbox in sandboxes:
    if should_be_reset(sandbox):
        api.edit(sandbox, reset_text, "Clearing sandbox", True, True)
    else:
        pid = os.fork() # You didn't see this....
        if pid == 0:
            for blah in xrange(3):
                if should_be_reset(sandbox):
                    api.edit(sandbox, reset_text, "Clearing sandbox", True, True)
                    break
                else:
                    time.sleep(60*3)
            # After it sleeps for 9 minutes without clearing the sandbox, it will be cleared regardless.
            api.edit(sandbox, reset_text, "Clearing sandbox", True, True)
            os._exit(0) # Exit the child process, (I think)

api.logout()
sys.exit(0)

wikibot.py

The framework is located at user:lowercase sigmabot/Source.py. It requires the 'BeautifulSoup', 'kitchen' and 'simplemediawiki' python modules to run.