Search in sources :

Example 1 with Recipe

use of delta.games.lotro.lore.crafting.recipes.Recipe in project lotro-tools by dmorcellet.

the class RecipesLoader method loadRecipeDefinition.

/**
 * Load recipe definition.
 * @param key Recipe key.
 * @return Number of loaded recipes.
 */
private int loadRecipeDefinition(String key) {
    int nbRecipes = 0;
    RecipePageParser parser = new RecipePageParser();
    String escapedKey = Escapes.escapeIdentifier(key);
    String url = "http://lorebook.lotro.com/wiki/Recipe:" + escapedKey;
    List<Recipe> recipes = parser.parseRecipePage(url);
    if ((recipes != null) && (recipes.size() > 0)) {
        RecipeXMLWriter writer = new RecipeXMLWriter();
        for (Recipe recipe : recipes) {
            // Write recipe
            int id = recipe.getIdentifier();
            String fileName = String.valueOf(id) + ".xml";
            File recipeFile = new File(_recipesDir, fileName);
            if (!recipeFile.getParentFile().exists()) {
                recipeFile.getParentFile().mkdirs();
            }
            String name = recipe.getName();
            boolean ok = writer.write(recipeFile, recipe, EncodingNames.UTF_8);
            if (ok) {
                System.out.println("Wrote recipe [" + name + "]");
                nbRecipes++;
            } else {
                _logger.error("Write failed for recipe [" + name + "] (id=" + id + ")!");
            }
        }
    } else {
        _logger.error("No recipe for URL [" + url + "]");
    }
    return nbRecipes;
}
Also used : Recipe(delta.games.lotro.lore.crafting.recipes.Recipe) RecipeXMLWriter(delta.games.lotro.lore.crafting.recipes.io.xml.RecipeXMLWriter) File(java.io.File)

Example 2 with Recipe

use of delta.games.lotro.lore.crafting.recipes.Recipe in project lotro-tools by dmorcellet.

the class RecipesIndexBuilder method doIt.

/**
 * Do build recipes index.
 * @return <code>true</code> if it was done, <code>false</code> otherwise.
 */
public boolean doIt() {
    boolean ret = false;
    if (_recipesDir.exists()) {
        RecipesIndex index = new RecipesIndex();
        ExtensionPredicate extFilter = new ExtensionPredicate(".xml");
        File[] recipeFiles = _recipesDir.listFiles(extFilter);
        if (recipeFiles != null) {
            RecipeXMLParser parser = new RecipeXMLParser();
            for (File recipeFile : recipeFiles) {
                Recipe recipe = parser.parseXML(recipeFile);
                String profession = recipe.getProfession();
                String key = recipe.getKey();
                String name = recipe.getName();
                // int id=recipe.getIdentifier();
                int tier = recipe.getTier();
                index.addRecipe(key, name, profession, tier);
            }
            RecipesIndexXMLWriter writer = new RecipesIndexXMLWriter();
            ret = writer.write(_indexFile, index, EncodingNames.UTF_8);
        }
    }
    return ret;
}
Also used : RecipesIndexXMLWriter(delta.games.lotro.lore.crafting.recipes.index.io.xml.RecipesIndexXMLWriter) RecipesIndex(delta.games.lotro.lore.crafting.recipes.index.RecipesIndex) Recipe(delta.games.lotro.lore.crafting.recipes.Recipe) ExtensionPredicate(delta.common.utils.files.filter.ExtensionPredicate) RecipeXMLParser(delta.games.lotro.lore.crafting.recipes.io.xml.RecipeXMLParser) File(java.io.File)

Example 3 with Recipe

use of delta.games.lotro.lore.crafting.recipes.Recipe in project lotro-tools by dmorcellet.

the class ResolveItemLinksInRecipes method handleRecipes.

private void handleRecipes(ItemsResolver resolver) {
    // Load recipes
    RecipesManager rMgr = RecipesManager.getInstance();
    File recipesDir = LotroCoreConfig.getInstance().getRecipesDir();
    FileFilter fileFilter = new ExtensionPredicate("xml");
    File[] recipeFiles = recipesDir.listFiles(fileFilter);
    if (recipeFiles != null) {
        Set<String> missingKeys = new HashSet<String>();
        for (File recipeFile : recipeFiles) {
            String idStr = recipeFile.getName();
            idStr = idStr.substring(0, idStr.length() - 4);
            int id = NumericTools.parseInt(idStr, -1);
            if (id != -1) {
                Recipe recipe = rMgr.getRecipe(Integer.valueOf(id));
                List<Ingredient> ingredients = recipe.getIngredients();
                for (Ingredient ingredient : ingredients) {
                    ItemReference itemRef = ingredient.getItem();
                    handleItemRef(resolver, missingKeys, itemRef);
                }
                /*
          ItemReference scroll=recipe.getRecipeScroll();
          if (scroll!=null)
          {
            handleItemRef(ids,missingKeys,scroll);
          }
          */
                List<RecipeVersion> versions = recipe.getVersions();
                for (RecipeVersion version : versions) {
                    CraftingResult regular = version.getRegular();
                    if (regular != null) {
                        ItemReference ref = regular.getItem();
                        handleItemRef(resolver, missingKeys, ref);
                    }
                    CraftingResult critical = version.getCritical();
                    if (critical != null) {
                        ItemReference ref = critical.getItem();
                        handleItemRef(resolver, missingKeys, ref);
                    }
                }
            }
        }
        List<String> sortedKeys = new ArrayList<String>(missingKeys);
        Collections.sort(sortedKeys);
        for (String missingKey : sortedKeys) {
            System.out.println("Missing : " + missingKey);
        }
        System.out.println("Missing : " + sortedKeys.size());
    }
}
Also used : Recipe(delta.games.lotro.lore.crafting.recipes.Recipe) ArrayList(java.util.ArrayList) ExtensionPredicate(delta.common.utils.files.filter.ExtensionPredicate) RecipesManager(delta.games.lotro.lore.crafting.recipes.RecipesManager) ItemReference(delta.games.lotro.lore.crafting.recipes.ItemReference) Ingredient(delta.games.lotro.lore.crafting.recipes.Ingredient) RecipeVersion(delta.games.lotro.lore.crafting.recipes.RecipeVersion) FileFilter(java.io.FileFilter) File(java.io.File) CraftingResult(delta.games.lotro.lore.crafting.recipes.CraftingResult) HashSet(java.util.HashSet)

Example 4 with Recipe

use of delta.games.lotro.lore.crafting.recipes.Recipe in project lotro-tools by dmorcellet.

the class RecipePageParser method parseRecipePage.

/**
 * Parse the recipe page at the given URL.
 * @param url URL of recipe page.
 * @return A list of recipes or <code>null</code> if an error occurred.
 */
public List<Recipe> parseRecipePage(String url) {
    List<Recipe> recipes = null;
    try {
        // url="http://lorebook.lotro.com/wiki/Recipe:Ancient_Steel_Scholar%27s_Glass";
        // url="http://lorebook.lotro.com/wiki/Recipe:Heavy_Cotton_Armour"; // Multiple output
        DownloadService downloader = DownloadService.getInstance();
        String page = downloader.getPage(url);
        // File f=new File("D:\\dam\\dev\\perso\\lotro\\docs\\recipes\\Glass.html");
        // File f=new File("D:\\dam\\dev\\perso\\lotro\\docs\\recipes\\Chisel.html");
        // String page=TextUtils.loadTextFile(f,EncodingNames.ISO8859_1);
        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-recipe" class="lorebook_action_link" href="/wiki/Recipe:Sage%5C%27s_Sharp_Chisel">Article</a></div>
            _key = null;
            Element articleLink = JerichoHtmlUtils.findElementByTagNameAndAttributeValue(source, HTMLElementName.A, "id", "ca-nstab-recipe");
            if (articleLink != null) {
                String thisURL = articleLink.getAttributeValue("href");
                if ((thisURL != null) && (thisURL.startsWith(RECIPE_URL_SEED))) {
                    _key = thisURL.substring(RECIPE_URL_SEED.length()).trim();
                }
            }
            int tier = findTier(source);
            recipes = new ArrayList<Recipe>();
            List<Element> recipeSections = JerichoHtmlUtils.findElementsByTagNameAndAttributeValue(lorebook, HTMLElementName.DIV, "class", "lorebookclass");
            if ((recipeSections != null) && (recipeSections.size() > 0)) {
                for (Element recipeSection : recipeSections) {
                    Recipe recipe = parseRecipeSection(recipeSection);
                    if (recipe != null) {
                        recipes.add(recipe);
                        recipe.setKey(_key);
                        if (tier != 0) {
                            recipe.setTier(tier);
                        }
                    }
                }
            }
            findIdentifiers(recipes);
            for (Recipe recipe : recipes) {
                System.out.println("Recipe: ");
                System.out.println(recipe.dump());
            }
        }
    } catch (Exception e) {
        recipes = null;
        _logger.error("Cannot parse quest page [" + url + "]", e);
    }
    return recipes;
}
Also used : Recipe(delta.games.lotro.lore.crafting.recipes.Recipe) Element(net.htmlparser.jericho.Element) DownloadService(delta.games.lotro.utils.DownloadService) InputSource(org.xml.sax.InputSource) Source(net.htmlparser.jericho.Source)

Example 5 with Recipe

use of delta.games.lotro.lore.crafting.recipes.Recipe 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)

Aggregations

Recipe (delta.games.lotro.lore.crafting.recipes.Recipe)5 File (java.io.File)3 ExtensionPredicate (delta.common.utils.files.filter.ExtensionPredicate)2 Ingredient (delta.games.lotro.lore.crafting.recipes.Ingredient)2 RecipeVersion (delta.games.lotro.lore.crafting.recipes.RecipeVersion)2 Element (net.htmlparser.jericho.Element)2 CraftingResult (delta.games.lotro.lore.crafting.recipes.CraftingResult)1 ItemReference (delta.games.lotro.lore.crafting.recipes.ItemReference)1 RecipesManager (delta.games.lotro.lore.crafting.recipes.RecipesManager)1 RecipesIndex (delta.games.lotro.lore.crafting.recipes.index.RecipesIndex)1 RecipesIndexXMLWriter (delta.games.lotro.lore.crafting.recipes.index.io.xml.RecipesIndexXMLWriter)1 RecipeXMLParser (delta.games.lotro.lore.crafting.recipes.io.xml.RecipeXMLParser)1 RecipeXMLWriter (delta.games.lotro.lore.crafting.recipes.io.xml.RecipeXMLWriter)1 DownloadService (delta.games.lotro.utils.DownloadService)1 FileFilter (java.io.FileFilter)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Source (net.htmlparser.jericho.Source)1 InputSource (org.xml.sax.InputSource)1