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

Content deleted Content added
Updating source) (bot
m Replaced deprecated <source> tags with <syntaxhighlight>
 
(23 intermediate revisions by 5 users not shown)
Line 1:
Source as of 0009:0137, 128 SeptemberDecember 20122014 (UTC).
 
== Sandbot1.py ==
Line 17:
If the template is present, it does nothing.
 
<sourcesyntaxhighlight lang="python">
#!/usrhome/sigma/.local/bin/python3
# -*- coding: utf-8 -*-
# LGPLv2+ license, look it up
 
import sys
import os
import datetimebuiltins
 
import ceterach
import passwords
 
import mwparserfromhell as mwparser
 
builtins.print = lambda *args, **kwargs: None
 
def main():
global api
api = ceterach.api.MediaWiki("httphttps://en.wikipedia.org/w/api.php")
api.login("Lowercase sigmabot II", ceterach.passwords.lcsb2)
api.set_token("edit")
bot = SandBot1(api)
bot.run()
Line 37 ⟶ 43:
class SandBot1:
REINSERT = "{{Please leave this line alone (SB)}}\n\n"
SANDBOXES = ({"Wikipedia:Sandbox",
"Wikipedia:Tutorial/Editing/sandbox",
"Wikipedia:Tutorial/Formatting/sandbox",
Line 43 ⟶ 49:
"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)
Line 57 ⟶ 64:
@property
def is_allowed(self):
return self.shutoff_page.content.lower() == "true" #or True
 
def get_templates_on(self, page):
tl = tuple(self.api.prop(500, "templates", tlnamespace=10, titles=page))
if not tl[0].get("templates", None):
return
for x in tl[0]["templates"]:
yield self.api.page(x["title"])
 
def check_if_heading_is_gone(self, box):
tl = tuple(self.api.propiterator(500, prop="templates", tlnamespace=10, tllimit=500, titles=page)box.title)
templates_in_box = self.get_templates_on(box)
returnres not= ("Template:Sandbox heading" in [{x.['title'] for x in templates_in_box]next(tl).get("templates", "")}
return not res & self.TEMPLATES
 
def run(self):
Line 74 ⟶ 75:
return
for sandbox in self.SANDBOXES:
print(sandbox)
box = self.api.page(sandbox)
if box.revision_user.name == "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!")
Line 84:
if __name__ == "__main__":
main()
api.logout()
 
</syntaxhighlight>
</source>
 
== Sandbot2.py ==
Line 99 ⟶ 98:
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■--></pre>
 
<sourcesyntaxhighlight lang="python">
#!/usrhome/sigma/.local/bin/python3
# -*- coding: utf-8 -*-
# LGPLv2+ license, look it up
 
import datetime
import time
import os
import sys
import pickle
import threading
 
import arrow
 
import ceterach
import passwords
 
reset_text = """{{Please leave this line alone (sandbox heading)}}<!--
reset_text = "{{subst:Sandbox reset}}"
* Welcome to the sandbox! *
tpl_reset_text = "{{subst:Template sandbox reset}}"
* Please leave this part alone *
* The page is cleared regularly *
* Feel free to try your editing skills below *
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■-->
"""
def main():
global api
api = ceterach.api.MediaWiki("httphttps://en.wikipedia.org/w/api.php")
with open("opener.pkl", 'rb') as fp:
api.login("Lowercase sigmabot II", ceterach.passwords.lcsb2)
api.opener = pickle.load(fp)
bot = SandBot2(api)
bot.run()
 
class SandBot2:
SANDBOXES = ("Wikipedia:Sandbox",
Line 134 ⟶ 133:
"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",
print( "Template:Template sandbox)",
}
 
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"
 
def wait(self, box):
for __ in range(3):
Line 153 ⟶ 167:
# will clear the box regardless.
print("Done with sleeping, clearing {!r}".format(box.title))
self.api.login("Lowercase sigmabot II", ceterach.passwords.lcsb2)
self.api.set_token("edit")
text = tpl_reset_text if box.namespace == 10 else reset_text
self.apibox.edit(box.title, reset_texttext, "Clearing sandbox) (bot", bot=True, force=True)
 
def parse_date(self, date):
return datetimearrow.datetimeArrow.strptime(date, '%Y-%m-%dT%H:%M:%SZ')
 
def box_needs_reset(self, box):
now = datetime.datetimearrow.utcnow()
three_min = datetimearrow.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 = res["query"]["pages"][tuplenext(iter(res["query"]["pages"].keysvalues())[0]])["revisions"][0]["timestamp"]
box_stamp = self.parse_date(str_stamp)
if box_stamp < (now - three_min):
return True
return False
 
def run(self):
if not self.is_allowed:
Line 176 ⟶ 192:
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))
self.apiprint(box.edit(sandbox, reset_text, "Clearing sandbox) (bot", bot=True, force=True))
else:
# The bot will fork, and continue on to the next sandbox
Line 188 ⟶ 205:
# os._exit(0) # Exit the child process
threading.Thread(target=self.wait, args=(box,)).start()
 
if __name__ == "__main__":
main()
 
</syntaxhighlight>
</source>