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

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

Obtém o último código fonte da wiki.

/*--------------------------------------------------------
 * 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.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Description(summary = "Obtém o último código fonte da wiki.")
public class GetsourceTask implements BotTask
{
    private WikiSession m_wiki = null;

    private final static String SOURCE_PATH = "src/amauricebot";
    private final static String BACKUP_PATH = "prevsrc";
    private final Pattern m_indexPattern = Pattern.compile("/Código Fonte/(\\w+)");
    private final Pattern m_srcPattern = Pattern.compile("<pre>\n(.*)</pre>",Pattern.DOTALL);

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

    public void perform()
            throws IOException
    {
        File sourceDir = new File(SOURCE_PATH);
        if (!sourceDir.exists() || !sourceDir.isDirectory())
        {
            Utils.log("ERRO: Nenhum diretório em "
                    + sourceDir.getCanonicalPath());
            return;
        }

        String getBase = "Utilizador:" + m_wiki.getLoginUser() + "/Código Fonte/";
        String indexPage = getBase + "Index";
        String indexText = m_wiki.getText(indexPage);
        Matcher indexMatcher = m_indexPattern.matcher(indexText);
        while (indexMatcher.find())
        {
            String className = indexMatcher.group(1);
            getSourceFile(getBase + className, className);
        }
    }

    private void getSourceFile(String wikiPage, String className)
            throws IOException
    {
        String pageSrc = m_wiki.getText(wikiPage);
        Matcher srcMatcher = m_srcPattern.matcher(pageSrc);
        if (srcMatcher.find())
            pageSrc = srcMatcher.group(1);
        
        pageSrc = pageSrc.replaceAll("\\&lt;", "<")
                         .replaceAll("\\&gt;", ">")
                         .replaceAll("\\&amp;", "\\&");
        
        File sourceFile = new File(SOURCE_PATH + "/" + className + ".java");
        if (sourceFile.exists())
        {
            StringBuilder sourceCode = new StringBuilder();
            BufferedReader in = new BufferedReader(new FileReader(sourceFile));

            int c;
            while ((c = in.read()) != -1)
            {
                sourceCode.append((char)c);
            }
            in.close();

            String fileSrc = sourceCode.toString();
            if (fileSrc.equals(pageSrc))
                return;
        }

        // create backup dir if does not exist
        File backupFile = new File(BACKUP_PATH);
        if (!backupFile.exists())
            backupFile.mkdir();

        // remove old backup file if exists
        backupFile = new File(BACKUP_PATH + "/" + className + ".java");
        if (backupFile.exists())
            backupFile.delete();

        // rename current source to backup
        sourceFile = new File(SOURCE_PATH + "/" + className + ".java");
        if (sourceFile.exists())
            sourceFile.renameTo(backupFile);

        // write new source file
        BufferedWriter out = new BufferedWriter(new FileWriter(sourceFile));
        out.write(pageSrc);
        out.close();
        
        Utils.log("Baixado o novo " + className);
    }
}