User:FluBot/Source

This is an example of how the FluBot works. This function moved all schematic pages from the old system (Schem:Blah) to new (Blah (Schematic)). I kinda forget how it all works... basically, getSchemPages visits Special:Allpages and parses that page to get links to all the old schem pages. There are up to three links on each line, which is why so much of the code is repeated. Function moveSkillPage makes an HTTP POST request to visit the wiki's Move Page form and fill in the required information automagically. Those are the basics; the rest is just driving the thing.

def getSchemPages(self): returned_html = wikipedia.getPage(self.site, "Special:Allpages&from=Schem", do_quote = False, get_edit_page=False)

start = "" end = "" n = 0

startpos = returned_html.index(start) + len(start) endpos = startpos + returned_html[startpos:].index(end) relevant = returned_html[startpos:endpos] lines = [line.strip for line in relevant.strip.split('\n')][::-1]

startpage = "a href=\"/swgwiki/index.php/"  endpage = "\" title =\""

for line in relevant.strip.split('\n'): if line.find(startpage) < 0: continue

pagestart = line.find(startpage) + len(startpage) pageend = pagestart + line[pagestart:].find(endpage)

skill = line[pagestart:pageend]

if skill.find("Schem:") == 0: self.moveSchemPage(skill.replace("_", " ").replace("%26", "&").replace("%27", "'"))

pagestart = line.find(startpage, line.find(startpage) + len(startpage)) + len(startpage) pageend = pagestart + line[pagestart:].find(endpage)

skill = line[pagestart:pageend] if skill.find("Schem:") == 0: self.moveSchemPage(skill.replace("_", " ").replace("%26", "&").replace("%27", "'"))

pagestart = line.find(startpage, line.find(startpage, line.find(startpage) + len(startpage)) + len(startpage)) + len(startpage) pageend = pagestart + line[pagestart:].find(endpage)

skill = line[pagestart:pageend] if skill.find("Schem:") == 0: self.moveSchemPage(skill.replace("_", " ").replace("%26", "&").replace("%27", "'"))

def moveSchemPage(self, skill): startskill = "Schem:" startpos = skill.index(startskill) + len(startskill) newname = (skill[startpos:] + " (Schematic)")

print "Moving \"" + skill + "\" to \"" + newname + "\"..." predata = [ ("wpNewTitle", newname), ("wpOldTitle", skill), ("wpMovetalk", "1"), ("wpMove", "1")] data = wikipedia.urlencode(tuple(predata)) conn = httplib.HTTPConnection(b.site.hostname) conn.putrequest("POST", "/swgwiki/index.php?title=Special:Movepage&action=submit") conn.putheader('Content-Length', str(len(data))) conn.putheader("Content-type", "application/x-www-form-urlencoded") conn.putheader("User-agent", "PythonWikipediaBot/1.0") if b.site.cookies: conn.putheader('Cookie',b.site.cookies) conn.endheaders conn.send(data)

# Prepare the return values response = conn.getresponse data = response.read conn.close

No, I'm not really a Python programmer, which is probably why a lot of this looks rather ugly. I just know enough to hack something like this. --Influenza 01:19, 29 Aug 2005 (CEST)