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;
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations