User:Lowercase sigmabot II/Source.py: Difference between revisions

Content deleted Content added
Updating source) (bot
m Replaced deprecated <source> tags with <syntaxhighlight>
 
(33 intermediate revisions by 5 users not shown)
Line 1:
Source as of 09:37, 28 December 2014 (UTC).
 
Source as of 00:00, 1 June 2012 (UTC).
 
== Sandbot1.py ==
Line 18 ⟶ 17:
If the template is present, it does nothing.
 
<sourcesyntaxhighlight lang="python">
#!/usrhome/sigma/.local/bin/pythonpython3
# -*- coding: utf-8 -*-
# LGPLv2+ license, look it up
 
"""
This sandbot reinserts the sandbox header every 60 seconds.
"""
 
from wikibot import WikiBot
from passwords import lcsb2 as pw
import sys
import os
import builtins
 
import ceterach
def revision_user(page):
import passwords
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"]
 
import mwparserfromhell as mwparser
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"]
 
builtins.print = lambda *args, **kwargs: None
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 runmain():
global api
for sandbox in sandboxes:
api = ceterach.api.MediaWiki("https://en.wikipedia.org/w/api.php")
if revision_user(sandbox) == "Lowercase sigmabot II":
api.login("Lowercase sigmabot II", passwords.lcsb2)
continue
api.set_token("edit")
elif heading_is_gone(sandbox):
bot = SandBot1(api)
api.prepend(sandbox, reinsert, summary="Reinserting sandbox header", minor=False, bot=True)
bot.run()
print sandbox, "had a heading reinserted"
 
class SandBot1:
def shut_off_check(page="Coal ball"):
REINSERT = "{{Please leave this line alone (SB)}}\n\n"
if api.get(page).lower() == "true":
SANDBOXES = {"Wikipedia:Sandbox",
return True
"Wikipedia:Tutorial/Editing/sandbox",
return False
"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
user = "Lowercase sigmabot II"
def is_allowed(self):
#pw = lcsb2.replace("\n", "")
return self.shutoff_page.content.lower() == "true" #or True
api = WikiBot("http://en.wikipedia.org/w/api.php")
if shut_off_check("User:Lowercase sigmabot II/Shutoff") is False:
print "!!! Emergency shut off page is not true"
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()
 
def check_if_heading_is_gone(self, box):
</source>
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()
 
</syntaxhighlight>
 
== Sandbot2.py ==
Line 96 ⟶ 98:
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■--></pre>
 
<sourcesyntaxhighlight lang="python">
#!/usrhome/sigma/.local/bin/pythonpython3
# -*- coding: utf-8 -*-
# LGPLv2+ license, look it up
 
from wikibot import WikiBot
from datetime import datetime, timedelta
from threading import Thread
from passwords import lcsb2
import time
import os
import sys
import ospickle
import threading
 
import arrow
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 *
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-->
"""
 
import ceterach
threads_exist = 0
import passwords
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")
 
reset_text = "{{subst:Sandbox reset}}"
def should_be_reset(box):
tpl_reset_text = "{{subst:Template sandbox reset}}"
curtime = datetime.utcnow()
three_min = timedelta(seconds=180)
def main():
query = api.call({"action":"query", "prop":"revisions", "titles":box, "rvprop":"timestamp"})["query"]["pages"]
global api
pageid = query.keys()[0]
api = ceterach.api.MediaWiki("https://en.wikipedia.org/w/api.php")
box_stamp = api.parse_date(query[pageid]["revisions"][0]["timestamp"])
with open("opener.pkl", 'rb') as fp:
if box_stamp < curtime - three_min:
returnapi.opener True= pickle.load(fp)
bot = SandBot2(api)
return False
bot.run()
 
class SandBot2:
def revision_user(page):
SANDBOXES = ("Wikipedia:Sandbox",
query = {"action":"query", "prop":"revisions", "titles":page, "rvprop":"user"}
"Wikipedia:Tutorial/Editing/sandbox",
res = api.call(query)["query"]["pages"]
"Wikipedia:Tutorial/Formatting/sandbox",
pageid = res.keys()[0]
"Wikipedia:Tutorial/Wikipedia links/sandbox",
return res[pageid]["revisions"][0]["user"]
"Wikipedia:Tutorial/Citing sources/sandbox",
"Wikipedia:Tutorial/Keep in mind/sandbox"
)
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",
"Wikipedia talk:Sandbox",
"Wikipedia talk:Tutorial/Editing/sandbox",
"Wikipedia talk:Tutorial/Formatting/sandbox",
"Wikipedia talk:Tutorial/Wikipedia links/sandbox",
"Wikipedia talk:Tutorial/Citing sources/sandbox",
"Wikipedia talk:Tutorial/Keep in mind/sandbox",
"User:Sandbox",
"Template:Template sandbox",
}
 
def __init__(self, api, shutoff="User:Lowercase sigmabot II/Shutoff"):
def sandbox_wait(box):
for blah in xrange(3): self.api = api
timeself.sleepshutoff_page = api.page(60*3shutoff)
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
 
@property
for sandbox in sandboxes:
def is_allowed(self):
if revision_user(sandbox) == "Lowercase sigmabot II":
return self.shutoff_page.content.lower() == "true"
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()
 
def wait(self, box):
if threads_exist == 1:
for __ in range(3):
print "Threads exist"
# Sleep for 3 minutes
else:
print("3 minute sleep on {!r}".format(box.title))
api.logout()
time.sleep(60 * 3)
if self.box_needs_reset(box):
break
# After the bot sleeps on this box for 9 minutes, it
# will clear the box regardless.
print("Done with sleeping, clearing {!r}".format(box.title))
self.api.login("Lowercase sigmabot II", passwords.lcsb2)
self.api.set_token("edit")
text = tpl_reset_text if box.namespace == 10 else reset_text
box.edit(text, "Clearing sandbox) (bot", bot=True, force=True)
 
def parse_date(self, date):
return arrow.Arrow.strptime(date, '%Y-%m-%dT%H:%M:%SZ')
 
def box_needs_reset(self, box):
now = arrow.utcnow()
three_min = arrow.util.timedelta(seconds=60 * 3)
# there's probably a way to use MediaWiki.iterator(), but too lazy
res = self.api.call(action="query", prop="revisions", titles=box.title, rvprop="timestamp", limit="2")
str_stamp = next(iter(res["query"]["pages"].values()))["revisions"][0]["timestamp"]
box_stamp = self.parse_date(str_stamp)
if box_stamp < (now - three_min):
return True
return False
 
def run(self):
</source>
if not self.is_allowed:
print("Check the shutoff page")
return
for sandbox in self.SANDBOXES:
box = self.api.page(sandbox)
if box.revision_user.name in ("Lowercase sigmabot II", "Hazard-Bot") or\
box.content == reset_text:
continue
if self.box_needs_reset(box):
print("Clearing {!r}".format(sandbox))
print(box.edit(reset_text, "Clearing sandbox) (bot", bot=True, force=True))
else:
# The bot will fork, and continue on to the next sandbox
# while the child process waits 3 to 9 minutes
# if os.fork() == 0: # Child process
# self.wait(box) # Wait takes place in the child process
# os._exit(0) # Exit the child process
threading.Thread(target=self.wait, args=(box,)).start()
 
if __name__ == "__main__":
== wikibot.py ==
main()
 
</syntaxhighlight>
The framework is located at [[user:lowercase sigmabot/Source.py]]. It requires the 'BeautifulSoup' python module to run.