Search in sources :

Example 56 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class CharacterPageParser method parseMainPage.

/**
 * Parse the character page at the given URL.
 * @param name Toon name.
 * @param url URL of character page.
 * @param retryNumber Number of previous tries.
 * @return A character or <code>null</code> if an error occurred..
 */
private CharacterData parseMainPage(String name, String url, int retryNumber) {
    if (_logger.isInfoEnabled()) {
        _logger.info("Character description page parsing for toon [" + name + "] " + ((retryNumber > 0) ? " try #" + retryNumber : ""));
    }
    CharacterData ret = null;
    try {
        DownloadService downloader = DownloadService.getInstance();
        String page = downloader.getPage(url);
        Source source = new Source(page);
        _character = null;
        // <table class="char_panel freep">
        Element charPanel = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(source, HTMLElementName.TABLE, "class", "char_panel freep");
        if (charPanel != null) {
            _character = new CharacterData();
            parseCharacterDescription(charPanel);
            parseStats(charPanel);
        }
        ret = _character;
        _character = null;
    } catch (Exception e) {
        if (_logger.isInfoEnabled()) {
            _logger.info("Cannot parse character page [" + url + "]", e);
        }
    }
    return ret;
}
Also used : CharacterData(delta.games.lotro.character.CharacterData) Element(net.htmlparser.jericho.Element) DownloadService(delta.games.lotro.utils.DownloadService) Source(net.htmlparser.jericho.Source)

Example 57 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class RewardsHTMLParser method parseReputationReward.

private Reputation parseReputationReward(Element rewardDiv) {
    // System.out.println("Reputation reward!");
    Reputation r = new Reputation();
    List<Element> elements = rewardDiv.getChildElements();
    if ((elements != null) && (elements.size() == 2)) {
        Element reputationNode = elements.get(1);
        List<Segment> nodes = JerichoHtmlUtils.getChildNodes(reputationNode);
        int nbNodes = nodes.size();
        int nbItems = nbNodes / 4;
        for (int i = 0; i < nbItems; i++) {
            Segment valueNode = nodes.get(i * 4 + 1);
            Segment factionNode = nodes.get(i * 4 + 3);
            if ((valueNode.getClass() == Segment.class) && (factionNode.getClass() == Segment.class)) {
                String valueStr = valueNode.toString();
                valueStr = valueStr.replace("with", "").trim();
                valueStr = valueStr.replace("+", "").trim();
                int reputation = NumericTools.parseInt(valueStr, 0);
                String factionName = factionNode.toString().trim();
                Faction faction = FactionsRegistry.getInstance().getByName(factionName);
                if (faction != null) {
                    ReputationItem item = new ReputationItem(faction);
                    item.setAmount(reputation);
                    r.add(item);
                } else {
                    _logger.error("Cannot get faction [" + factionName + "]!");
                }
            }
        }
    }
    return r;
/*
<div class="questReward">
<div>
<strong>Reputation:</strong>
</div>
<div>
<img class="icon" src="http://content.turbine.com/sites/lorebook.lotro.com/images/icons/reputation_increase.gif">
+700 with
<a href="/wiki/Faction:Malledhrim">Malledhrim</a>
</div>
</div>
     */
}
Also used : Element(net.htmlparser.jericho.Element) Reputation(delta.games.lotro.common.Reputation) ReputationItem(delta.games.lotro.common.ReputationItem) Segment(net.htmlparser.jericho.Segment) Faction(delta.games.lotro.lore.reputation.Faction)

Example 58 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class RewardsHTMLParser method parseMoneyReward.

private Money parseMoneyReward(Element rewardDiv) {
    // System.out.println("Money reward!");
    Money m = new Money();
    List<Element> elements = rewardDiv.getChildElements();
    if ((elements != null) && (elements.size() == 2)) {
        Element moneyNode = elements.get(1);
        List<Segment> nodes = JerichoHtmlUtils.getChildNodes(moneyNode);
        int nb = nodes.size();
        for (int i = 0; i < nb; i++) {
            Segment s = nodes.get(i);
            if (s.getClass() == Segment.class) {
                int nbCoins = NumericTools.parseInt(s.toString(), 0);
                if ((nbCoins > 0) && (i < nb - 1)) {
                    Segment tmp = nodes.get(i + 1);
                    if (tmp instanceof StartTag) {
                        String className = ((StartTag) tmp).getAttributeValue("class");
                        if ("coin".equals(className)) {
                            String type = ((StartTag) tmp).getAttributeValue("src");
                            if (type != null) {
                                if (type.contains("silver")) {
                                    m.setSilverCoins(nbCoins);
                                } else if (type.contains("copper")) {
                                    m.setCopperCoins(nbCoins);
                                } else if (type.contains("gold")) {
                                    m.setGoldCoins(nbCoins);
                                } else {
                                    _logger.warn(_objectId + ": unknown coin type [" + type + "]");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return m;
/*
    <div class="questReward">
    <div>
    <strong>Money:</strong>
    </div>
    <div>
    29
    <img class="coin" src="http://content.turbine.com/sites/lorebook.lotro.com/images/icons/currency/silver.gif">
    &nbsp;5
    <img class="coin" src="http://content.turbine.com/sites/lorebook.lotro.com/images/icons/currency/copper.gif">
    </div>
    </div>
    */
}
Also used : Money(delta.games.lotro.common.Money) Element(net.htmlparser.jericho.Element) Segment(net.htmlparser.jericho.Segment) StartTag(net.htmlparser.jericho.StartTag)

Example 59 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class RewardsHTMLParser method parseItemReward.

private void parseItemReward(Element rewardDiv, Rewards rewards) {
    ObjectsSet objects = null;
    // System.out.println("Item reward!");
    String key = "";
    List<Element> divs = rewardDiv.getAllElements(HTMLElementName.DIV);
    // remove reward div
    divs.remove(0);
    for (Element div : divs) {
        List<Element> strongs = div.getAllElements(HTMLElementName.STRONG);
        if (strongs.size() > 0) {
            Element strong = strongs.get(0);
            key = CharacterReference.decodeCollapseWhiteSpace(strong.getContent());
            key = cleanupFieldName(key);
            if (RECEIVE_KEY.equals(key)) {
                objects = rewards.getObjects();
            } else if (SELECT_ONE_OF_KEY.equals(key)) {
                objects = rewards.getSelectObjects();
            } else {
                _logger.warn(_objectId + ": unmanaged object selection key [" + key + "]");
            }
        } else {
            List<Element> as = div.getAllElements(HTMLElementName.A);
            if ((as != null) && (as.size() == 2)) {
                Element iconItem = as.get(0);
                String iconURL = null;
                List<Element> imgs = iconItem.getAllElements(HTMLElementName.IMG);
                if ((imgs != null) && (imgs.size() == 1)) {
                    Element img = imgs.get(0);
                    iconURL = img.getAttributeValue("src");
                // System.out.println("Icon: "+iconURL);
                }
                Element textItem = as.get(1);
                String itemName = CharacterReference.decodeCollapseWhiteSpace(textItem.getContent());
                String url = textItem.getAttributeValue("href");
                ObjectItem item = new ObjectItem(itemName);
                item.setObjectURL(url);
                item.setIconURL(iconURL);
                int quantity = 1;
                TextExtractor extractor = div.getTextExtractor();
                extractor.excludeElement(iconItem.getStartTag());
                extractor.excludeElement(textItem.getStartTag());
                String text = extractor.toString();
                int factorIndex = text.indexOf("(x");
                if (factorIndex != -1) {
                    int parenthesisIndex = text.indexOf(')', factorIndex + 2);
                    if (parenthesisIndex != -1) {
                        String factorStr = text.substring(factorIndex + 2, parenthesisIndex);
                        quantity = NumericTools.parseInt(factorStr, 1);
                    }
                }
                if (objects != null) {
                    objects.addObject(item, quantity);
                } else {
                    _logger.warn(_objectId + ": ignored object [" + item + "], quantity=" + quantity);
                }
            // System.out.println("Item: "+itemName+", URL: "+url);
            }
        }
    }
/*

<div class="questReward">
<div>
<strong>Receive:</strong>
</div>
<div>
<a href="/wiki/Item:Drownholt_Compass">
<img class="icon" rel="" src="http://content.turbine.com/sites/lorebook.lotro.com/images/icons/item/device/it_tracking_mom_book9_chapter5.png">
</a>
<a href="/wiki/Item:Drownholt_Compass">Drownholt Compass</a>
&nbsp;(x5)
</div>
</div>
     */
}
Also used : ObjectItem(delta.games.lotro.common.objects.ObjectItem) Element(net.htmlparser.jericho.Element) ObjectsSet(delta.games.lotro.common.objects.ObjectsSet) TextExtractor(net.htmlparser.jericho.TextExtractor)

Example 60 with Element

use of net.htmlparser.jericho.Element in project lotro-tools by dmorcellet.

the class RewardsHTMLParser method parseDestinyPoints.

private int parseDestinyPoints(Element rewardDiv) {
    int nbDestinyPoints = 0;
    List<Element> elements = rewardDiv.getChildElements();
    if ((elements != null) && (elements.size() == 2)) {
        Element moneyNode = elements.get(1);
        List<Segment> nodes = JerichoHtmlUtils.getChildNodes(moneyNode);
        int nb = nodes.size();
        for (int i = 0; i < nb; i++) {
            Segment s = nodes.get(i);
            if (s.getClass() == Segment.class) {
                nbDestinyPoints = NumericTools.parseInt(s.toString(), 0);
            }
        }
    }
    return nbDestinyPoints;
/*
<div class="questReward">
<div>
<strong>Destiny Points:</strong>
</div>
<div>
250
<img class="icon" src="http://content.turbine.com/sites/lorebook.lotro.com/images/icons/icon_destiny_points_15.png">
</div>
</div>
 */
}
Also used : Element(net.htmlparser.jericho.Element) Segment(net.htmlparser.jericho.Segment)

Aggregations

Element (net.htmlparser.jericho.Element)70 Source (net.htmlparser.jericho.Source)17 DownloadService (delta.games.lotro.utils.DownloadService)11 ArrayList (java.util.ArrayList)11 Segment (net.htmlparser.jericho.Segment)6 InputSource (org.xml.sax.InputSource)6 Context (com.cflint.plugins.Context)4 Matcher (java.util.regex.Matcher)4 StartTag (net.htmlparser.jericho.StartTag)4 BasicStatsSet (delta.games.lotro.character.stats.BasicStatsSet)3 Item (delta.games.lotro.lore.items.Item)3 List (java.util.List)3 Attribute (net.htmlparser.jericho.Attribute)3 CFScriptStatement (cfml.parsing.cfscript.script.CFScriptStatement)2 ParseException (cfml.parsing.reporting.ParseException)2 CFLintScanException (com.cflint.exception.CFLintScanException)2 ContextMessage (com.cflint.plugins.Context.ContextMessage)2 Money (delta.games.lotro.common.Money)2 CraftingResult (delta.games.lotro.lore.crafting.recipes.CraftingResult)2 Ingredient (delta.games.lotro.lore.crafting.recipes.Ingredient)2