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

Content deleted Content added
Updating source) (bot
Updating source) (bot
Line 1:
Source as of 05:04, 26 August 2012 (UTC).
 
Source as of 20:18, 5 August 2012 (UTC).
 
== Sandbot1.py ==
Line 19 ⟶ 18:
 
<source lang="python">
#!/usr/bin/pythonpython3
# -*- coding: utf-8 -*-
 
"""
This sandbot reinserts the sandbox header every 120 seconds.
"""
 
from wikibot import WikiBot
from passwords import lcsb2 as pw
import sys
import os
 
import ceterach
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"]
 
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"]
 
def heading_is_gonemain(box):
global api
templates_on_sandbox = templates_on_page(box)
api = ceterach.api.MediaWiki("http://en.wikipedia.org/w/api.php")
if "Template:Sandbox heading" in templates_on_sandbox:
api.login("Lowercase sigmabot II", ceterach.passwords.lcsb2)
return False
bot = SandBot1(api)
return True
bot.run()
 
class SandBot1:
def run():
REINSERT = "{{Please leave this line alone (SB)}}\n\n"
for sandbox in sandboxes:
SANDBOXES = ("Wikipedia:Sandbox",
if revision_user(sandbox) == "Lowercase sigmabot II":
continue "Wikipedia:Tutorial/Editing/sandbox",
"Wikipedia:Tutorial/Formatting/sandbox",
elif heading_is_gone(sandbox):
"Wikipedia:Tutorial/Wikipedia links/sandbox",
api.prepend(sandbox, reinsert, summary="Reinserting sandbox header", minor=False, bot=True)
"Wikipedia:Tutorial/Citing sources/sandbox",
print sandbox, "had a heading reinserted"
"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)"
)
 
def __init__(self, api, shutoff="User:Lowercase sigmabot/Shutoff"):
def shut_off_check(page="Coal ball"):
if api self.get(page).lower()api == "true":api
self.shutoff_page = api.page(shutoff)
return True
return False
 
@property
def is_allowed(self):
return self.shutoff_page.content.lower() == "true"
 
def get_templates_on(self, page):
user = "Lowercase sigmabot II"
tl = tuple(self.api.prop(500, "templates", tlnamespace=10, titles=page))
#pw = lcsb2.replace("\n", "")
if not tl[0].get("templates", None):
api = WikiBot("http://en.wikipedia.org/w/api.php")
return
if shut_off_check("User:Lowercase sigmabot II/Shutoff") is False:
for x in tl[0]["templates"]:
print "!!! Emergency shut off page is not true"
yield self.api.page(x["title"])
sys.exit()
 
api.login(user, pw)
def check_if_heading_is_gone(self, box):
api.set_all()
templates_in_box = self.get_templates_on(box)
reinsert = "{{Please leave this line alone (SB)}}\n\n"
return not ("Template:Sandbox heading" in [x.title for x in templates_in_box])
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")
def run(self):
if not self.is_allowed:
api.logout()
return
for sandbox in self.SANDBOXES:
print(sandbox)
box = self.api.page(sandbox)
if box.revision_user == "Lowercase sigmabot II":
continue
if self.check_if_heading_is_gone(box.title):
box.prepend(self.REINSERT, summary="Reinserting sandbox header) (bot", bot=True)
print("\thad a header reinserted!")
 
if __name__ == "__main__":
main()
api.logout()
 
</source>
Line 97 ⟶ 100:
 
<source lang="python">
#!/usr/bin/pythonpython3
# -*- coding: utf-8 -*-
 
from wikibot import WikiBotdatetime
from datetime import datetime, timedelta
from threading import Thread
from passwords import lcsb2
import time
import sys
import os
import sys
 
import ceterach
 
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! *
Line 121 ⟶ 118:
"""
 
def main():
threads_exist = 0
global api
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")
api = ceterach.api.MediaWiki("http://en.wikipedia.org/w/api.php")
api.login("Lowercase sigmabot II", ceterach.passwords.lcsb2)
bot = SandBot2(api)
bot.run()
 
class SandBot2:
def should_be_reset(box):
SANDBOXES = ("Wikipedia:Sandbox",
curtime = datetime.utcnow()
"Wikipedia:Tutorial/Editing/sandbox",
three_min = timedelta(seconds=180)
"Wikipedia:Tutorial/Formatting/sandbox",
query = api.call({"action":"query", "prop":"revisions", "titles":box, "rvprop":"timestamp"})["query"]["pages"]
"Wikipedia:Tutorial/Wikipedia links/sandbox",
pageid = query.keys()[0]
"Wikipedia:Tutorial/Citing sources/sandbox",
box_stamp = api.parse_date(query[pageid]["revisions"][0]["timestamp"])
"Wikipedia:Tutorial/Keep in mind/sandbox"
if box_stamp < curtime - three_min:
return True)
return False
 
def __init__(self, api, shutoff="User:Lowercase sigmabot II/Shutoff"):
def revision_user(page):
self.api = api
query = {"action":"query", "prop":"revisions", "titles":page, "rvprop":"user"}
self.shutoff_page = shutoff
res = api.call(query)["query"]["pages"]
pageid = res.keys()[0]
return res[pageid]["revisions"][0]["user"]
 
@property
def sandbox_wait(box):
fordef blah in xrangeis_allowed(3self):
return self.shutoff_page.content.lower() == "true"
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
 
def wait(self, box):
for sandbox in sandboxes:
for __ in range(3):
if revision_user(sandbox) == "Lowercase sigmabot II":
continue time.sleep(60 * 3)
if self.box_needs_reset(box):
elif should_be_reset(sandbox):
break
api.edit(sandbox, reset_text, "Clearing sandbox) (bot", True, True, force=True)
# After the bot sleeps on this box for 9 minutes, it
else:
# will clear the box regardless.
threads_exist = 1
Threadbox.edit(target=sandbox_waitreset_text, args=["Clearing sandbox]).start (bot", bot=True, force=True)
 
def parse_date(self, date):
if threads_exist == 1:
return datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%SZ')
print "Threads exist"
else:
api.logout()
 
def box_needs_reset(self, box):
</source>
now = datetime.datetime.utcnow()
three_min = datetime.timedelta(seconds=60 * 3)
res = tuple(api.prop(2, "revisions", titles=box.title, rvprop="timestamp"))
box_stamp = self.parse_date(res[0]["revisions"][0]["timestamp"])
return (box_stamp < now - three_min)
 
def run(self):
== wikibot.py ==
for sandbox in self.SANDBOXES:
box = self.api.page(sandbox)
if box.revision_user == "Lowercase sigmabot II":
continue
if self.box_needs_reset(box):
print("Edited {!r}".format(sandbox))
box.edit(reset_text, "Clearing sandbox) (bot", bot=True, force=True)
if os.fork() == 0: # Child process
self.wait(box)
os._exit(0)
else:
threads += 1
 
</source>
The framework is located at [[user:lowercase sigmabot/Source.py]]. It requires the 'BeautifulSoup' python module to run.