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 69.160.28.160 (talk) at 19:32, 18 July 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Source as of 09:37, 28 December 2014 (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.

#!/home/sigma/.local/bin/python3
# -*- coding: utf-8 -*-
# LGPLv2+ license, look it up

import sys
import os
import builtins

import ceterach
import passwords

import mwparserfromhell as mwparser

builtins.print = lambda *args, **kwargs: None

def main():
    global api
    api = ceterach.api.MediaWiki("https://en.wikipedia.org/w/api.php")
    api.login("Lowercase sigmabot II", passwords.lcsb2)
    api.set_token("edit")
    bot = SandBot1(api)
    bot.run()

class SandBot1:
    REINSERT = "{{Please leave this line alone (SB)}}\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 heading)",
                 "Template:Sandbox heading/noedit",
                 "Template:Sandbox header (do not remove)",
                 "Template:PLTLA (SH)",
                 "Template:Please leave this line alone (sandbox heading)",
                 "Template:Sandbox heading"
    }

    def __init__(self, api, shutoff="User:Lowercase sigmabot II/Shutoff"):
        self.api = api
        self.shutoff_page = api.page(shutoff)

    @property
    def is_allowed(self):
        return self.shutoff_page.content.lower() == "true" #or True

    def check_if_heading_is_gone(self, box):
        tl = self.api.iterator(500, prop="templates", tlnamespace=10, tllimit=500, titles=box.title)
        res = {x['title'] for x in next(tl).get("templates", "")}
        return not res & self.TEMPLATES

    def run(self):
        if not self.is_allowed:
            return
        for sandbox in self.SANDBOXES:
            box = self.api.page(sandbox)
            if box.revision_user.name == "Lowercase sigmabot II":
                continue
            if self.check_if_heading_is_gone(box):
                box.prepend(self.REINSERT, summary="Reinserting sandbox header) (bot", bot=True)
                print("\thad a header reinserted!")

if __name__ == "__main__":
    main()

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     *
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-->