User:TLULbot/Source/CharmLogCreator.java

/* * tLULbot - Java bot for editing the RuneScape Wiki * Copyright (c) 2009-2010 The last username left and others. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;. * * This program was based on AmauriceBot, created by Maurice Abraham. * * Contributors: * Maurice Abraham * The last username left *--*/

package tLULbot_v3;

import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import jBot.*; import jBot.Core.*;

/** * Class for creating charm submissions pages. * @author The last username left */ public class CharmLogCreator extends Task {   public void setWikiSession(WikiSession wiki) {       w = wiki; }   protected int getPrinterVersion {       return 1; }   protected int getMainVersion {       return 2; }   //A privately defined Runnable is used so that two threads are not both trying to run the same task with the same parameters //while still allowing the task to be run asynchronously. Eventually, I plan to modify the code so that a task can not be   //run alongside the same task, by making the Runnable static. Currently, a second copy can be run because a new copy of the //Task is created whenever Main needs to access the Runnable. private Runnable r = new Runnable {       public void run {           try {               if (!w.isLoggedIn) {                   w.p.errString("Submission pages must be created while logged in."); return; }               create; }           catch (IOException e1) {               w.p.errString("IOException: " + e1.getMessage); }           catch (jBotException e1) {               w.p.errString("Error: " + e1.getMessage); }       }    };

public Runnable getRunnable {       return r;    } public String getTaskCommand {       return "charmsubmit"; }   public String getTaskName {       return "Create charm log submissions pages"; }   protected String getWiki {       return "runescape"; }   private WikiSession w = null;

private List&lt;String&gt; logNames = new ArrayList&lt;String&gt;; private List&lt;String&gt; submitNames = new ArrayList&lt;String&gt;; private Pattern whatLinksHerePattern = Pattern.compile("&lt;li&gt;&lt;a href=\"/wiki/([^\":/]+)/Charm_log(_?\\(?[0-9]*\\)?)\"");   private Pattern charmsPerDropPattern = Pattern.compile("charms *= *([0-9]*)", Pattern.CASE_INSENSITIVE);    private Pattern killsPattern = Pattern.compile("kills *= *([0-9]*)", Pattern.CASE_INSENSITIVE);    private Pattern goldPattern = Pattern.compile("gold *= *([0-9]*)", Pattern.CASE_INSENSITIVE);    private Pattern greenPattern = Pattern.compile("green *= *([0-9]*)", Pattern.CASE_INSENSITIVE);    private Pattern crimsonPattern = Pattern.compile("crimson *= *([0-9]*)", Pattern.CASE_INSENSITIVE);    private Pattern bluePattern = Pattern.compile("blue *= *([0-9]*)", Pattern.CASE_INSENSITIVE);    boolean debug = true;    private void create throws jBotException, IOException    {        w.setWiki("runescape");        w.p.setProgString("Getting list of charm logs..."); if (!getLogNames) throw new jBotException("Unable to get list of charm logs."); if (!w.isLoggedIn) throw new jBotException("Unable to continue while not logged in."); //Force quit - bot edits need to be properly identified w.p.setProgString("Getting charm data: ~s%"); int startpage = 0; int numpages = 1000; String charms = "1"; String kills = "0"; String gold = "0"; String green = "0"; String crimson = "0"; String blue = "0"; if (debug) w.p.outString("DEBUG MODE"); for (int i = startpage; i &lt; submitNames.size &amp;&amp; i &lt; (numpages + startpage); i++) {           w.p.setProgString("Creating submissions pages... " + submitNames.get(i)); charms = "1"; kills = "0"; gold = "0"; green = "0"; crimson = "0"; blue = "0"; logNames.set(i, logNames.get(i).replace("%27", "\'")); //tLULbot is apostrophe intolerant submitNames.set(i, submitNames.get(i).replace("%27", "\'")); String text = w.getText(logNames.get(i)); Matcher match = charmsPerDropPattern.matcher(text); if (match.find &amp;&amp; match.groupCount == 1) {               charms = match.group(1) == "0"? "1" : match.group(1); }           match = killsPattern.matcher(text); if (match.find &amp;&amp; match.groupCount == 1) {               kills = match.group(1); }           match = goldPattern.matcher(text); if (match.find &amp;&amp; match.groupCount == 1) {               gold = match.group(1); }           match = greenPattern.matcher(text); if (match.find &amp;&amp; match.groupCount == 1) {               green = match.group(1); }           match = crimsonPattern.matcher(text); if (match.find &amp;&amp; match.groupCount == 1) {               crimson = match.group(1); }           match = bluePattern.matcher(text); if (match.find &amp;&amp; match.groupCount == 1) {               blue = match.group(1); }           if (debug) {               w.p.outString("Create page " + submitNames.get(i) + "\ncharms=" + charms + "\nkills=" + kills + "\ngold=" + gold + "\ngreen=" + green + "\ncrimson=" + crimson + "\nblue=" + blue); }           else {               w.p.outString("Editing page " + logNames.get(i) + ", creating page " + submitNames.get(i) + "."); w.getText(logNames.get(i)); w.editText(logNames.get(i), "", "Task 2a" + (debug? " (debug)":"") + " - Report errors - Shutdown", false); w.getText(logNames.get(i)); w.protectPage(logNames.get(i), "Charm logs protected per Forum:Preventing charm log vandalism and User:TLULbot/Tasks/2a"); w.getText(submitNames.get(i)); w.editText(submitNames.get(i), "", "Task 2a" + (debug? " (debug)":"") + " - Report errors - Shutdown", true); }           w.p.setProg((i + 1 - startpage) * 100 / Math.min(submitNames.size, numpages)); }       Utils.log("Completed Task 2a" + (debug? " (simulation)" : "") + ": Creation of charm log submissions pages. Number of pages created: " + submitNames.size); w.updateLog; w.p.outString("Done. Number of pages created: " + submitNames.size); }   private boolean getLogNames throws IOException {       logNames.clear; submitNames.clear; //Text of what links to Template:Charm data String text = Utils.getInternetPage("http://runescape.wikia.com/index.php?title=Special:WhatLinksHere/Template:Charm_data&amp;limit=5000", null, null, w.p); addCharmLogs(text); //Add charm logs to listing w.p.outString("Total pages: " + logNames.size); //Report total pages to operator return true; }   private void addCharmLogs(String text) {       logNames.add("User:TLULbot/Aberrant_spectre/Charm_log"); submitNames.add("User:TLULbot/Aberrant_spectre/Charm_log_submission"); logNames.add("User:TLULbot/Abyssal_demon/Charm_log"); submitNames.add("User:TLULbot/Abyssal_demon/Charm_log_submission"); logNames.add("User:TLULbot/Abyssal_guardian/Charm_log"); submitNames.add("User:TLULbot/Abyssal_guardian/Charm_log_submission"); if (true) return; /*For debugging*/ Matcher match = whatLinksHerePattern.matcher(text); while(match.find) {           if (match.groupCount == 1) //If only the first group matched {               logNames.add(match.group(1) + "/Charm_log"); submitNames.add(match.group(1) + "/Charm_log_submission"); //Adds the name of the page to be created }           else if(match.groupCount == 2) //If second group matched - there is an extra part to the name {               logNames.add(match.group(1) + "/Charm_log" + match.group(2)); submitNames.add(match.group(1) + "/Charm_log_submission" + match.group(2)); //Adds the name of the page to be created }       }    } }