Package net.traviangui.hostInterface

Source Code of net.traviangui.hostInterface.IOAlly

package net.traviangui.hostInterface;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Scanner;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import net.traviangui.Util;
import net.traviangui.model.Ally;
import net.traviangui.model.Player;
import net.traviangui.model.Village;
import net.traviangui.model.Villages;

public class IOAlly
{

    public static void updatePlayers( Ally ally) throws Exception
    {
        // 1. get ally
      String page = HttpServer.getHttpServer().httpGetPage( "http://s3.travian.net/allianz.php?aid=" + ally.getId());
       
        HtmlCleaner cleaner = new HtmlCleaner();
        CleanerProperties props = cleaner.getProperties();
        props.setRecognizeUnicodeChars( true);
//        TagNode node = cleaner.clean( new File("tests/ally.html"));
        TagNode node = cleaner.clean( page);

        updatePlayers( ally, node);
      
    }

    private static void updatePlayers( Ally ally, TagNode node) throws Exception
    {
        Object[] domNames  = node.evaluateXPath( "//td[@class='pla']/a/text()");
        Object[] domUrlProfiles = node.evaluateXPath( "//td[@class='pla']/a/@href");

        for( int i=0 ; i<domNames.length ; i++)
        {
            int id = Integer.parseInt( Util.patternExtract( "\\?uid=(\\d+)", domUrlProfiles[i].toString(), 1)[0]);
   
            if( !ally.exists( id)) {
                Player p = new Player( id);
                p.setName( domNames[i].toString());
                p.setUrl( new URL( "http://s3.travian.net/" + domUrlProfiles[i].toString()));

                ally.add( p);
            }
        }

    }

}
TOP

Related Classes of net.traviangui.hostInterface.IOAlly

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.