Usuário:SandroHcBot/Código Fonte/MembersTask
Ir para navegação
Ir para pesquisar
Reporta o número de membros nos recordes com o passar do tempo
/*-------------------------------------------------------- * 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.Locale; import java.util.TimeZone; import java.util.regex.Matcher; import java.util.regex.Pattern; @Description(summary = "Reporta o número de membros nos recordes com o passar do tempo") public class MembersTask implements BotTask { private WikiSession m_wiki = null; private String m_summaryPage = null; private String m_rankPage = "Predefinição:Nível Mínimo para Recordes"; private DateFormat m_dateFmt = null; private Pattern RANK_PATTERN = Pattern.compile("\\|(\\w+)\\.Rank=([0-9,]+)"); public MembersTask(WikiSession wiki) { m_wiki = wiki; String loginUser = m_wiki.getLoginUser(); if (loginUser != null) m_summaryPage = "Utilizador:" + loginUser + "/Número de Membros"; m_dateFmt = new SimpleDateFormat("d-MMM-yyyy", Locale.ENGLISH); m_dateFmt.setTimeZone(TimeZone.getTimeZone("UTC")); } public void perform() throws IOException { if (m_summaryPage == null) return; WikiSession.PageRevision[] revisions = m_wiki.getHistory(m_rankPage, 50); if (revisions.length < 1) { Utils.log("ERRO: Não foi possível obter o histórico de [[" + m_rankPage + "]]"); return; } StringBuilder overall = new StringBuilder(); StringBuilder constit = new StringBuilder(); for (int i = revisions.length-1; i >= 0; i --) { long numOverall = 1; long numConstit = 1; Matcher m = RANK_PATTERN.matcher(revisions[i].getContent()); while (m.find()) { if (m.group(1).equals("Todos")) numOverall = Utils.parseNum(m.group(2), null, 1); if (m.group(1).equals("Condição física")) numConstit = Utils.parseNum(m.group(2), null, 1); } overall.append(numOverall).append(" "); constit.append(numConstit).append(" "); } try { String oldText = m_wiki.getText(m_summaryPage); StringBuilder text = new StringBuilder(); text.append("{{#vardefine:overall|").append(overall).append("}}\n"); text.append("{{#vardefine:constit|").append(constit).append("}}\n"); text.append(oldText.replaceFirst("^(?:.|\n)*?==","==")); String newText = text.toString(); if (!newText.trim().equals(oldText.trim())) { if (m_wiki.editText(m_summaryPage, text.toString(), "Número de membros atualizado", true)) Utils.log("Contas atualizadas em [[" + m_summaryPage + "]]"); else Utils.log("AVISO: Falha ao atualizar o número de membros (possível conflicto)"); } } catch (Exception ex) { // ignore } } }