Usuário:SandroHcBot/Código Fonte/LogTask

De RuneScape Wiki
Ir para: navegação, pesquisa

Atualiza as entradas de ações efetuadas na wikia.

/*--------------------------------------------------------
 * AmauriceBot - RuneScape Wikia update task robot
 * Copyright (c) 2009-2012  Maurice Abraham.
 *
 * 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 <http://www.gnu.org/licenses/>.
 *
 * Contributors:
 *   None
 *------------------------------------------------------*/

package amauricebot;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

@Description(summary = "Atualiza as entradas de ações efetuadas na wikia.")
public class LogTask implements BotTask {
	private WikiSession m_wiki = null;

	private static final int MAX_LOG_HISTORY = 10;

	public LogTask(WikiSession wiki) {
		m_wiki = wiki;
	}

	public void perform() throws IOException {
		String wikiLog = "Utilizador:" + m_wiki.getLoginUser() + "/Log";

		if (Utils.getLogList().isEmpty())
			return; // nothing to log

		StringBuffer textBuf = new StringBuffer(m_wiki.getText(wikiLog));
		textBuf.append("\n");

		String[] sections = textBuf.toString().split("(?m)(?=^==)");

		textBuf = new StringBuffer();
		textBuf.append(sections.length > 0 ? sections[0] : "");

		Locale locale = new java.util.Locale("pt","PT");

		SimpleDateFormat date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", locale);
		date.setTimeZone(TimeZone.getTimeZone("UTC"));

		textBuf.append("==== ").append(date).append(" ====\n");
		for (String msg : Utils.getLogList()) {
			msg = msg
					.replaceFirst("^NOTA:", "[[Ficheiro:Missão.png]]")
					.replaceFirst("^AVISO:", "[[Ficheiro:D&D ícone.png]]")
					.replaceFirst("^ERRO:", "[[Ficheiro:Loja de magia ícone.png]]");
			textBuf.append("* ").append(msg).append("\n");
		}

		for (int i = 1; i < sections.length && i < MAX_LOG_HISTORY; i++) {
			textBuf.append(sections[i]);
		}

		if (!m_wiki.editText(wikiLog, textBuf.toString(), "Adicionando entradas", false))
			System.err.println("Falha ao adicionar a " + wikiLog);
	}
}