User:Lowercase sigmabot II/Source.py
Appearance
Source as of 00:54, 3 May 2012 (UTC).
Sandbot1.py
This task runs every minute. It checks if the following sandboxes have the {{Sandbox heading}} template;
- 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
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
import sys
import os
def shut_off_check(page="Coal ball"):
if api.getpage(page).lower() == "true":
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 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(21):
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" not in templates_on_sandbox:
return True
return False
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) (bot", minor=False, bot=True)
print "{!s} had a sandbox header reinserted.".format(sandbox)
user = "Lowercase sigmabot II"
pw_file = open(os.path.expanduser("~/wikibot/password.txt"), "r")
pw = pw_file.read().replace("\n", ""); pw_file.close(); del pw_file;
api = WikiBot("http://en.wikipedia.org/w/api.php")
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")
if shut_off_check(page="User:Lowercase sigmabot II/Shutoff"):
run()
else:
print "Bot disabled."
api.logout()
sys.exit(0)
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
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.query({"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
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)
return None
for sandbox in sandboxes:
if should_be_reset(sandbox):
api.edit(sandbox, reset_text, "Clearing sandbox) (bot", True, True)
else:
Thread(target=sandbox_wait, args=(sandbox)).start()
api.logout()
wikibot.py
The framework is located at user:lowercase sigmabot/Source.py. It requires the 'pycountry', 'BeautifulSoup', 'kitchen' and 'simplemediawiki' python modules to run.