Search in sources :

Example 76 with Element

use of org.jsoup.nodes.Element in project KaellyBot by Kaysoro.

the class Item method getItem.

public static Item getItem(Language lg, String url) throws IOException {
    Document doc = JSoupManager.getDocument(url);
    doc.setBaseUri(url);
    String name = doc.getElementsByClass("ak-return-link").first().text();
    String level = doc.getElementsByClass("ak-encyclo-detail-level").first().text().replaceAll(Translator.getLabel(lg, "item.extract.level") + " ", "");
    String type = doc.getElementsByClass("ak-encyclo-detail-type").last().children().last().text();
    String skinURL = doc.getElementsByClass("ak-encyclo-detail-illu").first().getElementsByTag("img").first().attr("src");
    String description = null;
    String effects = null;
    String caracteristics = null;
    String conditions = null;
    String panoplie = null;
    String panoplieURL = null;
    String recipe = null;
    Elements titles = doc.getElementsByClass("ak-panel-title");
    Elements lines;
    StringBuilder tmp;
    for (Element title : titles) if (title.text().equals(Translator.getLabel(lg, "item.extract.description")))
        description = title.parent().getElementsByClass("ak-panel-content").first().text();
    else if (title.text().equals(Translator.getLabel(lg, "item.extract.effets")))
        effects = extractStatsFromTitle(lg, title);
    else if (title.text().equals(Translator.getLabel(lg, "item.extract.caracteristiques")))
        caracteristics = extractLinesFromTitle(title);
    else if (title.text().equals(Translator.getLabel(lg, "item.extract.conditions")))
        conditions = extractLinesFromTitle(title);
    else if (title.text().contains(Translator.getLabel(lg, "item.extract.panoplie"))) {
        panoplie = title.getElementsByTag("a").first().text();
        panoplieURL = title.getElementsByTag("a").first().attr("abs:href");
    } else if (title.text().equals(Translator.getLabel(lg, "item.extract.recette"))) {
        lines = title.parent().getElementsByClass("ak-column");
        tmp = new StringBuilder();
        for (Element line : lines) tmp.append(line.getElementsByClass("ak-front").text()).append(" [").append(line.getElementsByClass("ak-title").first().text()).append("](").append(line.getElementsByClass("ak-title").first().children().first().attr("abs:href")).append(")\n");
        recipe = tmp.toString();
    }
    return new Item(name, type, level, description, effects, URLManager.abs(skinURL), url, caracteristics, conditions, panoplie, panoplieURL, recipe);
}
Also used : Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 77 with Element

use of org.jsoup.nodes.Element in project KaellyBot by Kaysoro.

the class Resource method getResource.

public static Resource getResource(Language lg, String url) throws IOException {
    Document doc = JSoupManager.getDocument(url);
    doc.setBaseUri(url);
    String name = doc.getElementsByClass("ak-return-link").first().text();
    String level = null;
    if (!doc.getElementsByClass("ak-encyclo-detail-level").isEmpty())
        level = doc.getElementsByClass("ak-encyclo-detail-level").first().text().replaceAll(Translator.getLabel(lg, "resource.extract.level") + " ", "");
    String type = doc.getElementsByClass("ak-encyclo-detail-type").last().children().last().text();
    String skinURL = doc.getElementsByClass("ak-encyclo-detail-illu").first().getElementsByTag("img").first().attr("src");
    String description = null;
    String effects = null;
    String bonus = null;
    String sorts = null;
    String recipe = null;
    Elements titles = doc.getElementsByClass("ak-panel-title");
    Elements lines;
    StringBuilder tmp;
    for (Element title : titles) if (title.text().equals(Translator.getLabel(lg, "resource.extract.description")))
        description = title.parent().getElementsByClass("ak-panel-content").first().text();
    else if (title.text().equals(Translator.getLabel(lg, "resource.extract.effets")))
        effects = extractStatsFromTitle(lg, title);
    else if (title.text().equals(Translator.getLabel(lg, "resource.extract.bonus")))
        bonus = extractLinesFromTitle(title);
    else if (title.text().equals(Translator.getLabel(lg, "resource.extract.sorts")))
        sorts = title.parent().getElementsByClass("ak-panel-content").first().text();
    else if (title.text().equals(Translator.getLabel(lg, "resource.extract.recette"))) {
        lines = title.parent().getElementsByClass("ak-column");
        tmp = new StringBuilder();
        for (Element line : lines) tmp.append(line.getElementsByClass("ak-front").text()).append(" [").append(line.getElementsByClass("ak-title").first().text()).append("](").append(line.getElementsByClass("ak-title").first().children().first().attr("abs:href")).append(")\n");
        recipe = tmp.toString();
    }
    return new Resource(name, type, level, description, effects, URLManager.abs(skinURL), url, bonus, sorts, recipe);
}
Also used : Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 78 with Element

use of org.jsoup.nodes.Element in project KaellyBot by Kaysoro.

the class DofusRequestCommand method getListRequestableFrom.

protected List<Requestable> getListRequestableFrom(String url, IMessage message, DiscordException notFound) {
    List<Requestable> result = new ArrayList<>();
    Language lg = Translator.getLanguageFrom(message.getChannel());
    try {
        Document doc = JSoupManager.getDocument(url);
        Elements elems = doc.getElementsByClass("ak-bg-odd");
        elems.addAll(doc.getElementsByClass("ak-bg-even"));
        for (Element element : elems) result.add(new Requestable(element.child(1).text(), element.child(1).select("a").attr("href")));
    } catch (IOException e) {
        ExceptionManager.manageIOException(e, message, this, lg, notFound);
        return new ArrayList<>();
    } catch (Exception e) {
        ExceptionManager.manageException(e, message, this, lg);
        return new ArrayList<>();
    }
    return result;
}
Also used : Language(enums.Language) Requestable(util.Requestable) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) IOException(java.io.IOException) DiscordException(exceptions.DiscordException)

Example 79 with Element

use of org.jsoup.nodes.Element in project KaellyBot by Kaysoro.

the class GuildCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Matcher m = getMatcher(message);
        m.find();
        Language lg = Translator.getLanguageFrom(message.getChannel());
        String pseudo = m.group(1).trim().toLowerCase();
        StringBuilder url;
        try {
            url = new StringBuilder(Translator.getLabel(lg, "game.url")).append(Translator.getLabel(lg, "guild.url")).append("?").append(forPseudo).append(URLEncoder.encode(pseudo, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            ExceptionManager.manageException(e, message, this, lg);
            return false;
        }
        if (m.group(2) != null) {
            String serverName = m.group(2).trim().toLowerCase();
            List<ServerDofus> result = new ArrayList<>();
            for (ServerDofus server : ServerDofus.getServersDofus()) if (server.getName().toLowerCase().startsWith(serverName))
                result.add(server);
            if (result.size() == 1)
                url.append("&").append(forServer).append(result.get(0).getId());
            else {
                if (!result.isEmpty())
                    tooMuchServers.throwException(message, this, lg);
                else
                    notFoundServer.throwException(message, this, lg);
                return false;
            }
        }
        try {
            Document doc = JSoupManager.getDocument(url.toString());
            Elements elems = doc.getElementsByClass("ak-bg-odd");
            elems.addAll(doc.getElementsByClass("ak-bg-even"));
            if (!elems.isEmpty()) {
                // on boucle jusqu'à temps de trouver la bonne guilde (ie la plus proche du nom donnée)
                List<String> result = new ArrayList<>();
                List<String> servers = new ArrayList<>();
                for (Element element : elems) if (pseudo.equals(element.child(1).text().trim().toLowerCase())) {
                    result.add(element.child(1).select("a").attr("href"));
                    servers.add(element.child(3).text());
                }
                if (result.size() == 1) {
                    DofusGuild guildPage = DofusGuild.getDofusGuild(Translator.getLabel(lg, "game.url") + result.get(0), lg);
                    Message.sendEmbed(message.getChannel(), guildPage.getEmbedObject(lg));
                } else if (result.size() > 1)
                    tooMuchGuilds.throwException(message, this, lg, servers);
                else
                    notFoundGuild.throwException(message, this, lg);
            } else
                notFoundGuild.throwException(message, this, lg);
        } catch (IOException e) {
            ExceptionManager.manageIOException(e, message, this, lg, guildPageInaccessible);
        } catch (Exception e) {
            ExceptionManager.manageException(e, message, this, lg);
        }
    }
    return false;
}
Also used : DofusGuild(data.DofusGuild) Matcher(java.util.regex.Matcher) ServerDofus(data.ServerDofus) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Language(enums.Language)

Example 80 with Element

use of org.jsoup.nodes.Element in project KaellyBot by Kaysoro.

the class WhoisCommand method request.

@Override
public boolean request(IMessage message) {
    if (super.request(message)) {
        Matcher m = getMatcher(message);
        m.find();
        Language lg = Translator.getLanguageFrom(message.getChannel());
        String pseudo = m.group(1).trim().toLowerCase();
        StringBuilder url;
        try {
            url = new StringBuilder(Translator.getLabel(lg, "game.url")).append(Translator.getLabel(lg, "character.url")).append("?").append(forPseudo).append(URLEncoder.encode(pseudo, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            ExceptionManager.manageException(e, message, this, lg);
            return false;
        }
        if (m.group(2) != null) {
            String serverName = m.group(2).trim().toLowerCase();
            List<ServerDofus> result = new ArrayList<>();
            for (ServerDofus server : ServerDofus.getServersDofus()) if (server.getName().toLowerCase().startsWith(serverName))
                result.add(server);
            if (result.size() == 1)
                url.append("&").append(forServer).append(result.get(0).getId());
            else {
                if (!result.isEmpty())
                    tooMuchServers.throwException(message, this, lg);
                else
                    notFoundServer.throwException(message, this, lg);
                return false;
            }
        }
        try {
            Document doc = JSoupManager.getDocument(url.toString());
            Elements elems = doc.getElementsByClass("ak-bg-odd");
            elems.addAll(doc.getElementsByClass("ak-bg-even"));
            if (!elems.isEmpty()) {
                // on boucle jusqu'à temps de trouver le bon personnage (ie le plus proche du nom donnée)
                List<String> result = new ArrayList<>();
                List<String> servers = new ArrayList<>();
                for (Element element : elems) if (pseudo.equals(element.child(1).text().trim().toLowerCase())) {
                    result.add(element.child(1).select("a").attr("href"));
                    servers.add(element.child(element.children().size() - 2).text());
                }
                if (result.size() == 1) {
                    Connection.Response response = JSoupManager.getResponse(Translator.getLabel(lg, "game.url") + result.get(0));
                    if (!response.url().getPath().endsWith(Translator.getLabel(lg, "whois.request"))) {
                        Character characPage = Character.getCharacter(Translator.getLabel(lg, "game.url") + result.get(0), lg);
                        Message.sendEmbed(message.getChannel(), characPage.getEmbedObject(lg));
                    } else
                        characterTooOld.throwException(message, this, lg);
                } else if (result.size() > 1)
                    tooMuchCharacters.throwException(message, this, lg, servers);
                else
                    notFoundCharacter.throwException(message, this, lg);
            } else
                notFoundCharacter.throwException(message, this, lg);
        } catch (IOException e) {
            ExceptionManager.manageIOException(e, message, this, lg, characterPageInaccessible);
        } catch (Exception e) {
            ExceptionManager.manageException(e, message, this, lg);
        }
    }
    return false;
}
Also used : Character(data.Character) Matcher(java.util.regex.Matcher) ServerDofus(data.ServerDofus) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Connection(org.jsoup.Connection) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) IOException(java.io.IOException) Exception(java.lang.Exception) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Language(enums.Language)

Aggregations

Element (org.jsoup.nodes.Element)1237 Document (org.jsoup.nodes.Document)559 Elements (org.jsoup.select.Elements)529 ArrayList (java.util.ArrayList)316 IOException (java.io.IOException)220 Test (org.junit.Test)144 ElementHandlerImpl (org.asqatasun.ruleimplementation.ElementHandlerImpl)90 File (java.io.File)87 URL (java.net.URL)82 Matcher (java.util.regex.Matcher)73 List (java.util.List)60 HashMap (java.util.HashMap)57 Pattern (java.util.regex.Pattern)54 Node (org.jsoup.nodes.Node)50 TextNode (org.jsoup.nodes.TextNode)48 InputStream (java.io.InputStream)38 JSONException (org.json.JSONException)36 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)35 Map (java.util.Map)34 JSONObject (org.json.JSONObject)34