Search in sources :

Example 41 with Element

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

the class RecipePageParser method parseRecipeSection.

private Recipe parseRecipeSection(Element recipeSection) {
    Recipe ret = null;
    try {
        _recipe = new Recipe();
        // Element mainIconElement=JerichoHtmlUtils.findElementByTagNameAndAttributeValue(recipeSection,HTMLElementName.DIV,"class","mainicon");
        List<Element> officialSections = JerichoHtmlUtils.findElementsByTagNameAndAttributeValue(recipeSection, HTMLElementName.DIV, "class", "officialsection");
        // 1 - summary
        // 2 - ingredients
        // 3 - results
        int nbSections = officialSections.size();
        if (nbSections != 3) {
            System.out.println("Warning: found " + nbSections + " sections!");
        }
        Element summarySection = officialSections.get(0);
        parseRecipeSummary(summarySection);
        // System.out.println("Ingredients:");
        Element ingredientsSection = officialSections.get(1);
        List<Ingredient> ingredients = parseIngredients(ingredientsSection);
        _recipe.setIngredients(ingredients);
        // System.out.println("Results:");
        Element resultsSection = officialSections.get(2);
        List<RecipeVersion> results = parseResults(resultsSection);
        _recipe.setVersions(results);
        ret = _recipe;
        _recipe = null;
    } catch (Exception e) {
        ret = null;
        _logger.error("Recipe [" + _key + "]. Cannot parse recipe section!", e);
    }
    return ret;
}
Also used : Recipe(delta.games.lotro.lore.crafting.recipes.Recipe) Ingredient(delta.games.lotro.lore.crafting.recipes.Ingredient) Element(net.htmlparser.jericho.Element) RecipeVersion(delta.games.lotro.lore.crafting.recipes.RecipeVersion)

Example 42 with Element

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

the class RecipePageParser method parseResultItem.

private CraftingResult parseResultItem(Element resultRow, boolean critical) {
    CraftingResult result = new CraftingResult();
    result.setCriticalResult(critical);
    ItemReference itemRef = new ItemReference();
    result.setItem(itemRef);
    Element ingredientIcon = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(resultRow, HTMLElementName.TD, "class", "resicon");
    if (ingredientIcon != null) {
        Element img = JerichoHtmlUtils.findElementByTagName(ingredientIcon, HTMLElementName.IMG);
        if (img != null) {
            String itemIcon = img.getAttributeValue("src");
            // System.out.println("Icon: "+itemIcon);
            itemRef.setIcon(itemIcon);
        }
    }
    Element ingredientQuantity = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(resultRow, HTMLElementName.TD, "class", "resquan");
    if (ingredientQuantity != null) {
        String quantityStr = CharacterReference.decodeCollapseWhiteSpace(ingredientQuantity.getContent());
        int quantity = parseQuantityString(quantityStr);
        // System.out.println("Quantity: "+quantity);
        result.setQuantity(quantity);
    }
    Element ingredientName = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(resultRow, HTMLElementName.TD, "class", "resname");
    if (ingredientName != null) {
        Element a = JerichoHtmlUtils.findElementByTagName(ingredientName, HTMLElementName.A);
        String itemURL = a.getAttributeValue("href");
        String itemId = extractItemIdentifier(itemURL);
        itemRef.setItemKey(itemId);
        String itemName = CharacterReference.decodeCollapseWhiteSpace(a.getContent());
        itemRef.setName(itemName);
    }
    // System.out.println(critical?"Critical result":"Regular result");
    return result;
}
Also used : ItemReference(delta.games.lotro.lore.crafting.recipes.ItemReference) Element(net.htmlparser.jericho.Element) CraftingResult(delta.games.lotro.lore.crafting.recipes.CraftingResult)

Example 43 with Element

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

the class RecipePageParser method parseResults.

private List<RecipeVersion> parseResults(Element resultsSection) {
    /*
<tr class="resultrow"> // or "resultrow critical"
<td class="resicon"><a href="/wiki/Tool:Ancient_Steel_Scholar's_Glass"><img class="icon" rel="" src="http://content.turbine.com/sites/lorebook.lotro.com/images/icons/item/tool/eq_craft_tool_rare_scholars_glass_tier5.png"></img></a></td>
<td class="restype">regular success</td>  // or critical success
<td class="resquan">x1</td>
<td class="resname"><a href="/wiki/Tool:Ancient_Steel_Scholar's_Glass"></a></td></tr>
     */
    List<RecipeVersion> versions = new ArrayList<RecipeVersion>();
    RecipeVersion version = new RecipeVersion();
    versions.add(version);
    List<Element> rows = resultsSection.getAllElements(HTMLElementName.TR);
    for (Element row : rows) {
        StartTag tag = row.getStartTag();
        String value = tag.getAttributeValue("class");
        if ("resultrow".equals(value)) {
            Element header = JerichoHtmlUtils.findElementByTagName(row, HTMLElementName.TH);
            if (header != null) {
                version = new RecipeVersion();
                versions.add(version);
            // System.out.println("OR");
            } else {
                CraftingResult regular = parseResultItem(row, false);
                version.setRegular(regular);
            }
        } else if ("resultrow critical".equals(value)) {
            CraftingResult critical = parseResultItem(row, true);
            version.setCritical(critical);
        }
    }
    return versions;
}
Also used : Element(net.htmlparser.jericho.Element) ArrayList(java.util.ArrayList) RecipeVersion(delta.games.lotro.lore.crafting.recipes.RecipeVersion) CraftingResult(delta.games.lotro.lore.crafting.recipes.CraftingResult) StartTag(net.htmlparser.jericho.StartTag)

Example 44 with Element

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

the class RecipePageParser method findIdentifiers.

private void findIdentifiers(List<Recipe> recipes) {
    String url = "http://lorebook.lotro.com/index.php?title=Recipe:" + _key + "&action=edit";
    DownloadService downloader = DownloadService.getInstance();
    try {
        String page = downloader.getPage(url);
        Source s = new Source(page);
        // <textarea id="wpTextbox1"
        Element pageSource = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(s, HTMLElementName.TEXTAREA, "id", "wpTextbox1");
        if (pageSource != null) {
            String text = JerichoHtmlUtils.getTextFromTag(pageSource);
            parsePageSource(text, recipes);
        } else {
            _logger.warn("Cannot find identifiers!");
        }
    } catch (Exception e) {
        _logger.error("Parsing error", e);
    }
}
Also used : Element(net.htmlparser.jericho.Element) DownloadService(delta.games.lotro.utils.DownloadService) InputSource(org.xml.sax.InputSource) Source(net.htmlparser.jericho.Source)

Example 45 with Element

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

the class RecipePageParser method findTier.

private int findTier(Segment root) {
    int tier = 0;
    Element links = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(root, HTMLElementName.DIV, "id", "mw-normal-catlinks");
    if (links != null) {
        List<Element> as = JerichoHtmlUtils.findElementsByTagName(links, HTMLElementName.A);
        for (Element a : as) {
            String title = a.getAttributeValue("title");
            if (title != null) {
                int index = title.indexOf(CATEGORY_TIER);
                if (index != -1) {
                    int startIndex = index + CATEGORY_TIER.length();
                    String tierStr = title.substring(startIndex, startIndex + 2).trim();
                    tier = NumericTools.parseInt(tierStr, 0);
                    break;
                }
            }
        }
    }
    return tier;
}
Also used : Element(net.htmlparser.jericho.Element)

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