Search in sources :

Example 21 with Element

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

the class QuestPageParser method parseQuestPage.

/**
 * Parse the quest page at the given URL.
 * @param url URL of quest page.
 * @return A list of quests or <code>null</code> if an error occurred.
 */
public List<QuestDescription> parseQuestPage(String url) {
    List<QuestDescription> quests = null;
    try {
        DownloadService downloader = DownloadService.getInstance();
        String page = downloader.getPage(url);
        Source source = new Source(page);
        // <div id="lorebookNoedit">
        Element lorebook = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(source, HTMLElementName.DIV, "id", "lorebookNoedit");
        if (lorebook != null) {
            // identifier
            // <a id="ca-nstab-quest" class="lorebook_action_link" href="/wiki/Quest:A_Feminine_Curve_to_the_Steel">Article</a>
            _key = null;
            Element articleLink = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(source, HTMLElementName.A, "id", "ca-nstab-quest");
            if (articleLink != null) {
                String thisURL = articleLink.getAttributeValue("href");
                if ((thisURL != null) && (thisURL.startsWith(QUEST_URL_SEED))) {
                    _key = thisURL.substring(QUEST_URL_SEED.length()).trim();
                }
            }
            quests = new ArrayList<QuestDescription>();
            List<Element> questSections = JerichoHtmlUtils.findElementsByTagNameAndAttributeValue(lorebook, HTMLElementName.DIV, "class", "lorebookquest");
            if ((questSections != null) && (questSections.size() > 0)) {
                for (Element questSection : questSections) {
                    QuestDescription quest = parseQuestSection(questSection);
                    if (quest != null) {
                        // System.out.println(quest.dump());
                        quests.add(quest);
                        quest.setKey(_key);
                    }
                }
            }
            findIdentifiers(quests);
        }
    } catch (Exception e) {
        quests = null;
        _logger.error("Cannot parse quest page [" + url + "]", e);
    }
    return quests;
}
Also used : QuestDescription(delta.games.lotro.lore.quests.QuestDescription) Element(net.htmlparser.jericho.Element) DownloadService(delta.games.lotro.utils.DownloadService) InputSource(org.xml.sax.InputSource) Source(net.htmlparser.jericho.Source)

Example 22 with Element

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

the class CharacterLogPageParser method parseFirstPage.

private int parseFirstPage(String rootUrl) {
    int nbPages = 0;
    String firstPageURL = rootUrl + "=1";
    try {
        DownloadService downloader = DownloadService.getInstance();
        String page = downloader.getPage(firstPageURL);
        Source source = new Source(page);
        // <div id="widget_" class="widget ui-widget ui-corner-all" type="activitylog">
        Element activityLog = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(source, HTMLElementName.DIV, "type", "activitylog");
        if (activityLog != null) {
            // Fetch page numbers
            // <div class="pagination">
            Element pagination = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(source, HTMLElementName.DIV, "class", "pagination");
            if (pagination != null) {
                // <td class="current"> Page 1 of 121 </td>
                String pages = JerichoHtmlUtils.getTagContents(source, HTMLElementName.TD, "class", "current");
                if (pages != null) {
                    String[] items = pages.split(" ");
                    if ((items != null) && (items.length == 4)) {
                        nbPages = NumericTools.parseInt(items[3], 0);
                    }
                }
            }
        }
    } catch (Exception e) {
        _logger.error("Cannot parse character log page [" + firstPageURL + "]", e);
    }
    return nbPages;
}
Also used : Element(net.htmlparser.jericho.Element) DownloadService(delta.games.lotro.utils.DownloadService) Source(net.htmlparser.jericho.Source)

Example 23 with Element

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

the class CharacterLogPageParser method parseLogPage.

private boolean parseLogPage(CharacterLog log, String rootURL, int pageNumber, int retryNumber) {
    if (_logger.isInfoEnabled()) {
        _logger.info("Page #" + pageNumber + ((retryNumber > 0) ? " try #" + retryNumber : ""));
    }
    boolean ret;
    String url = rootURL + "=" + String.valueOf(pageNumber);
    try {
        DownloadService downloader = DownloadService.getInstance();
        String page = downloader.getPage(url);
        Source source = new Source(page);
        // <table class="gradient_table activitylog">
        Element logTable = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(source, HTMLElementName.TABLE, "class", "gradient_table activitylog");
        if (logTable != null) {
            List<Element> trs = logTable.getAllElements(HTMLElementName.TR);
            if ((trs != null) && (trs.size() >= 1)) {
                // ignore first (table headers)
                trs.remove(0);
                for (Element tr : trs) {
                    CharacterLogItem item = parseLogItem(tr);
                    if (item != null) {
                        boolean addIt = true;
                        long date = item.getDate();
                        if (_stopDate != null) {
                            if (date < _stopDate.longValue()) {
                                addIt = false;
                                _stop = true;
                                break;
                            }
                        }
                        if (addIt) {
                            log.addLogItem(item);
                        }
                    }
                }
            }
        }
        ret = true;
    } catch (Exception e) {
        _logger.warn("Cannot parse character page [" + url + "]", e);
        ret = false;
    }
    return ret;
}
Also used : Element(net.htmlparser.jericho.Element) CharacterLogItem(delta.games.lotro.character.log.CharacterLogItem) DownloadService(delta.games.lotro.utils.DownloadService) Source(net.htmlparser.jericho.Source)

Example 24 with Element

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

the class RewardsHTMLParser method parseReward.

private void parseReward(Element rewardDiv, Rewards rewards) {
    List<Element> strongs = rewardDiv.getAllElements(HTMLElementName.STRONG);
    if ((strongs != null) && (strongs.size() >= 1)) {
        Element strong = strongs.get(0);
        String key = CharacterReference.decodeCollapseWhiteSpace(strong.getContent());
        key = cleanupFieldName(key);
        if ("Money".equals(key)) {
            Money m = parseMoneyReward(rewardDiv);
            rewards.getMoney().add(m);
        } else if ("Reputation".equals(key)) {
            Reputation r = parseReputationReward(rewardDiv);
            rewards.getReputation().add(r);
        } else if ((RECEIVE_KEY.equals(key)) || (SELECT_ONE_OF_KEY.equals(key))) {
            parseItemReward(rewardDiv, rewards);
        } else if ("IXP".equals(key)) {
            boolean itemXP = parseItemXPReward(rewardDiv);
            rewards.setHasItemXP(itemXP);
        } else if ("Traits".equals(key)) {
            parseTraitReward(rewardDiv, rewards);
        } else if ("Titles".equals(key)) {
            parseTitleReward(rewardDiv, rewards);
        } else if ("Virtues".equals(key)) {
            parseVirtuesReward(rewardDiv, rewards);
        } else if ("Emotes".equals(key)) {
            parseEmoteReward(rewardDiv, rewards);
        } else if ("Destiny Points".equals(key)) {
            int nb = parseDestinyPoints(rewardDiv);
            rewards.setDestinyPoints(nb);
        } else {
            _logger.error(_objectId + ": unknown reward type: " + key);
        }
    }
}
Also used : Money(delta.games.lotro.common.Money) Element(net.htmlparser.jericho.Element) Reputation(delta.games.lotro.common.Reputation)

Example 25 with Element

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

the class RewardsHTMLParser method parseTitleReward.

private void parseTitleReward(Element rewardDiv, Rewards rewards) {
    List<Element> as = rewardDiv.getAllElements(HTMLElementName.A);
    int size = (as != null) ? as.size() : 0;
    if (size == 1) {
        String name = null;
        String titleIdentifier = null;
        // String iconURL=null;
        Element firstA = as.get(0);
        String url = firstA.getAttributeValue("href");
        if ((url != null) && (url.startsWith(TITLE_URL_SEED))) {
            titleIdentifier = url.substring(TITLE_URL_SEED.length()).trim();
        }
        name = CharacterReference.decodeCollapseWhiteSpace(firstA.getContent());
        if ((name != null) && (titleIdentifier != null)) {
            Title title = new Title(titleIdentifier, name);
            rewards.addTitle(title);
        }
    } else {
        _logger.warn(_objectId + ": title with " + size + " anchor tags!");
    }
/*
<div class="questReward">
<div>
<strong>Titles:</strong>
</div>
<div>
<a href="/wiki/Title:_Honorary_Shirriff"> Honorary Shirriff</a>
</div>
</div>
 */
}
Also used : Element(net.htmlparser.jericho.Element) Title(delta.games.lotro.common.Title)

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