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

Content deleted Content added
Updating source) (bot
m Replaced deprecated <source> tags with <syntaxhighlight>
 
(45 intermediate revisions by 5 users not shown)
Line 1:
Source as of 09:37, 28 December 2014 (UTC).
 
Source as of 02:24, 1 May 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
import sys
import os
import builtins
 
import ceterach
def shut_off_check(page="Coal ball"):
import passwords
if api.getpage(page).lower() == "true":
return True
return False
 
import mwparserfromhell as mwparser
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"]
 
builtins.print = lambda *args, **kwargs: None
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_gonemain(box):
global api
templates_on_sandbox = templates_on_page(box)
api = ceterach.api.MediaWiki("https://en.wikipedia.org/w/api.php")
if "Template:Sandbox heading" not in templates_on_sandbox:
api.login("Lowercase sigmabot II", passwords.lcsb2)
return True
api.set_token("edit")
return False
bot = SandBot1(api)
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) (bot", minor=False, bot=True)
"Wikipedia:Tutorial/Citing sources/sandbox",
print "{!s} had a sandbox header reinserted.".format(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"
}
 
user = def __init__(self, api, shutoff="User:Lowercase sigmabot II/Shutoff"):
self.api = api
pw_file = open(os.path.expanduser("~/wikibot/password.txt"), "r")
self.shutoff_page = api.page(shutoff)
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"
 
@property
sandboxes = ("Wikipedia:Sandbox", "Wikipedia:Tutorial/Editing/sandbox", "Wikipedia:Tutorial/Formatting/sandbox",
def is_allowed(self):
"Wikipedia:Tutorial/Wikipedia links/sandbox", "Wikipedia:Tutorial/Citing sources/sandbox", "Wikipedia:Tutorial/Keep in mind/sandbox")
return self.shutoff_page.content.lower() == "true" #or True
 
def check_if_heading_is_gone(self, box):
templates = ("Template:Please leave this line alone (sandbox talk heading)",
tl = self.api.iterator(500, prop="templates", tlnamespace=10, tllimit=500, titles=box.title)
"Template:Please leave this line alone (Sandbox heading)",
res = {x['title'] for x in next(tl).get("templates", "")}
"Template:Sandbox heading/noedit",
return not res & self.TEMPLATES
"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 shut_off_check(page="User:Lowercase sigmabot II/Shutoff"):
if not self.is_allowed:
run()
return
else:
for sandbox in self.SANDBOXES:
print "Bot disabled."
box = self.api.page(sandbox)
api.logout()
if box.revision_user.name == "Lowercase sigmabot II":
sys.exit(0)
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__":
</source>
main()
 
</syntaxhighlight>
 
== Sandbot2.py ==
Line 107 ⟶ 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
import time
import os
import sys
import ospickle
import threading
 
import arrow
user = "Lowercase sigmabot II"
 
pwfile = open(os.path.expanduser("~/wikibot/password.txt"), "r")
import ceterach
pw = pwfile.read().replace("\n", ""); pwfile.close(); del pwfile;
import passwords
api = WikiBot("http://en.wikipedia.org/w/api.php")
 
api.login(user, pw)
reset_text = "{{subst:Sandbox reset}}"
api.set_all()
reset_texttpl_reset_text = """{{Pleasesubst:Template leave this line alone (sandbox heading)reset}}<!--"
* Welcome to the sandbox! *
def main():
* Please leave this part alone *
global api
* The page is cleared regularly *
api = ceterach.api.MediaWiki("https://en.wikipedia.org/w/api.php")
* Feel free to try your editing skills below *
with open("opener.pkl", 'rb') as fp:
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-->
api.opener = pickle.load(fp)
"""
bot = SandBot2(api)
bot.run()
 
class SandBot2:
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"
)
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"):
self.api = api
self.shutoff_page = api.page(shutoff)
 
@property
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 is_allowed(self):
return self.shutoff_page.content.lower() == "true"
 
def should_be_resetwait(self, box):
curtime = datetime.utcnow for __ in range(3):
# Sleep for 3 minutes
three_days = timedelta(hours=72)
print("3 minute sleep on {!r}".format(box.title))
query = api.call({"action":"query", "prop":"revisions", "titles":box, "rvprop":"timestamp"})["query"]["pages"]
pageid = query time.keyssleep(60 * 3)[0]
if self.box_needs_reset(box):
box_stamp = api.parse_date(query[pageid]["revisions"][0]["timestamp"])
if box_stamp < curtime - three_days: break
# After the bot sleeps on this box for 9 minutes, it
return True
# will clear the box regardless.
return False
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):
for sandbox in sandboxes:
return arrow.Arrow.strptime(date, '%Y-%m-%dT%H:%M:%SZ')
if should_be_reset(sandbox):
api.edit(sandbox, reset_text, "Clearing sandbox", True, True)
else:
pid = os.fork() # You didn't see this....
if pid == 0:
for blah in xrange(3):
if should_be_reset(sandbox):
api.edit(sandbox, reset_text, "Clearing sandbox", True, True)
break
else:
time.sleep(60*3)
# After it sleeps for 9 minutes without clearing the sandbox, it will be cleared regardless.
api.edit(sandbox, reset_text, "Clearing sandbox", True, True)
os._exit(0) # Exit the child process, (I think)
 
def box_needs_reset(self, box):
api.logout()
now = arrow.utcnow()
sys.exit(0)
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', 'kitchen' and 'simplemediawiki' python modules to run.