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 23:44, 16 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 23:44, 16 May 2012 (UTC).

Sandbot1.py

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

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

to the page.

If the template is present, it does nothing.

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

"""
This sandbot reinserts the sandbox header every 60 seconds.
"""

from wikibot import WikiBot
from passwords import lcsb2
import sys
import os

def revision_user(page):
    query = {"action":"query", "prop":"revisions", "titles":page, "rvprop":"user"}
    res = api.call(query)["query"]["pages"]
    pageid = res.keys()[0]
    return res[pageid]["revisions"][0]["user"]

def templates_on_page(page):
    query = {"action":"query", "prop":"templates", "tllimit":500, "tlnamespace":10, "titles":page}
    res = api.call(query)["query"]["pages"]
    pageid = res.keys()[0]
    # You didn't see this...
    if not "templates" in res[pageid]:
        for x in xrange(10):
            yield str(x)
    else:
        for template in res[pageid]["templates"]:
            yield template["title"]

def heading_is_gone(box):
    templates_on_sandbox = templates_on_page(box)
    if "Template:Sandbox heading" in templates_on_sandbox:
        return False
    return True

def run():
    for sandbox in sandboxes:
        if revision_user(sandbox) == "Lowercase sigmabot II":
            continue
        elif heading_is_gone(sandbox):
            api.prepend(sandbox, reinsert, summary="Reinserting sandbox header", minor=False, bot=True)
            print sandbox, "had a heading reinserted"

def shut_off_check(page="Coal ball"):
    if api.get(page).lower() == "true":
        return True
    return False


user = "Lowercase sigmabot II"
pw = lcsb2.replace("\n", "")
api = WikiBot("http://en.wikipedia.org/w/api.php")
if shut_off_check("User:Lowercase sigmabot II/Shutoff") is False:
    sys.exit()
api.login(user, pw)
api.set_all()
reinsert = "{{Sandbox heading}} <!-- Please leave this line alone! -->\n\n"
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")
templates = ("Template:Please leave this line alone (sandbox talk heading)", "Template:Please leave this line alone (Sandbox heading)", "Template:Sandbox heading/noedit", "Template:Please leave this line alone (sandbox talk heading)/noedit", "Template:Sandbox header (do not remove)", "Template:PLTLA (SH)", "Template:Please leave this line alone (sandbox heading)", "Template:Please leave this line alone (sandbox heading)/noedit")
run()
api.logout()

Sandbot2.py

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

{{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     *
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-->
#!/usr/bin/python
# -*- coding: utf-8 -*-

from wikibot import WikiBot
from datetime import datetime, timedelta
from threading import Thread
from passwords import lcsb2
import time
import sys
import os

user = "Lowercase sigmabot II"
pw = lcsb2.replace("\n", "")
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     *
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-->
"""

threads_exist = 0
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_min = timedelta(seconds=180)
    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_min:
        return True
    return False

def revision_user(page):
    query = {"action":"query", "prop":"revisions", "titles":page, "rvprop":"user"}
    res = api.call(query)["query"]["pages"]
    pageid = res.keys()[0]
    return res[pageid]["revisions"][0]["user"]

def sandbox_wait(box):
    for blah in xrange(3):
        time.sleep(60*3)
        if should_be_reset(box):
            break
    # After it sleeps for 9 minutes without clearing the sandbox, it will be cleared regardless.
    api.edit(box, reset_text, "Clearing sandbox) (bot", True, True, force=True)
    return None

for sandbox in sandboxes:
    if revision_user(sandbox) == "Lowercase sigmabot II":
        continue
    elif should_be_reset(sandbox):
        api.edit(sandbox, reset_text, "Clearing sandbox) (bot", True, True, force=True)
    else:
        threads_exist = 1
        Thread(target=sandbox_wait, args=[sandbox]).start()

if threads_exist == 1:
    print "Threads exist"
else:
    api.logout()

wikibot.py

The framework is located at user:lowercase sigmabot/Source.py. It requires the 'BeautifulSoup' python module to run.