Search in sources :

Example 81 with Element

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

the class Set method extractStatsFromTitleToList.

private static List<String> extractStatsFromTitleToList(Language lg, Element elem) {
    List<String> values = new ArrayList<>();
    Elements lines = elem.getElementsByClass("ak-title");
    StringBuilder tmp = new StringBuilder();
    for (Element line : lines) {
        String value = EmojiManager.getEmojiForStat(lg, line.text()) + line.text() + "\n";
        if (value.length() + tmp.length() > EmbedBuilder.FIELD_CONTENT_LIMIT) {
            values.add(tmp.toString());
            tmp.setLength(0);
        } else
            tmp.append(value);
    }
    if (tmp.length() > 0)
        values.add(tmp.toString());
    return values;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Elements(org.jsoup.select.Elements)

Example 82 with Element

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

the class Set method getSet.

public static Set getSet(Language lg, String url) throws IOException {
    Document doc = JSoupManager.getDocument(url);
    String name = doc.getElementsByClass("ak-return-link").first().text();
    String level = doc.getElementsByClass("ak-encyclo-detail-level").first().text().replaceAll(Translator.getLabel(lg, "set.extract.level") + " ", "");
    Element element = doc.getElementsByClass("ak-encyclo-detail-illu").first().getElementsByTag("img").first();
    String skinURL = element.attr("src");
    List<String> bonusTotal = new ArrayList<>();
    String composition = null;
    String[] bonusPano = new String[doc.getElementsByClass("ak-item-list-preview").first().children().size() - 1];
    List<String> recipeTotal = new ArrayList<>();
    Elements titles = doc.getElementsByClass("ak-panel-title");
    for (Element title : titles) if (title.text().equals(Translator.getLabel(lg, "set.extract.bonus.total")))
        bonusTotal = extractStatsFromTitleToList(lg, title.parent());
    else if (title.text().equals(Translator.getLabel(lg, "set.extract.bonus"))) {
        for (int i = 0; i < bonusPano.length; i++) bonusPano[i] = extractStatsFromTitle(lg, title.parent().getElementsByClass("set-bonus-list set-bonus-" + (i + 2)).first());
    } else if (title.text().equals(Translator.getLabel(lg, "set.extract.composition"))) {
        StringBuilder st = new StringBuilder();
        for (Element tr : title.parent().getElementsByTag("tr")) {
            Element a = tr.getElementsByClass("ak-set-composition-name").first().getElementsByTag("a").first();
            st.append("[").append(a.text()).append("](").append(a.attr("abs:href")).append(") ").append(tr.getElementsByClass("ak-set-composition-level").first().text()).append("\n");
        }
        composition = st.toString();
    } else if (title.text().equals(Translator.getLabel(lg, "set.extract.recipe"))) {
        Elements lines = title.parent().getElementsByClass("ak-column");
        StringBuilder field = new StringBuilder();
        for (Element line : lines) {
            StringBuilder tmp = new StringBuilder(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");
            if (field.length() + tmp.length() > EmbedBuilder.FIELD_CONTENT_LIMIT) {
                recipeTotal.add(field.toString());
                field.setLength(0);
            } else
                field.append(tmp.toString());
        }
        if (field.length() > 0)
            recipeTotal.add(field.toString());
    }
    return new Set(name, level, bonusTotal, URLManager.abs(skinURL), url, composition, bonusPano, recipeTotal);
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 83 with Element

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

the class Set method extractStatsFromTitle.

private static String extractStatsFromTitle(Language lg, Element elem) {
    Elements lines = elem.getElementsByClass("ak-title");
    StringBuilder tmp = new StringBuilder();
    for (Element line : lines) tmp.append(EmojiManager.getEmojiForStat(lg, line.text())).append(line.text()).append("\n");
    return tmp.toString();
}
Also used : Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements)

Example 84 with Element

use of org.jsoup.nodes.Element in project ocreader by schaal.

the class ArticleWebView method prepareDocument.

private void prepareDocument(Document document) {
    // Some blog engines replace emojis with an image and place the emoji in the image tag.
    // Find images with the tag being a single character and check if they are emoji. Then
    // replace the img with the actual emoji in unicode.
    Elements imgs = document.select("img[alt~=^.$]");
    for (Element img : imgs) {
        final String possibleEmoji = img.attr("alt");
        if (EmojiManager.isEmoji(possibleEmoji))
            img.replaceWith(new TextNode(possibleEmoji));
    }
    Elements iframes = document.getElementsByTag("iframe");
    for (Element iframe : iframes) {
        if (iframe.hasAttr("src")) {
            String href = iframe.attr("src");
            String html = String.format(Locale.US, videoLink, href, href);
            // Check if url matches any known patterns
            for (IframePattern iframePattern : IframePattern.values()) {
                Matcher matcher = iframePattern.pattern.matcher(href);
                if (matcher.matches()) {
                    final String videoId = matcher.group(2);
                    String urlPrefix = matcher.group(1);
                    href = urlPrefix + iframePattern.baseUrl + videoId;
                    // use thumbnail if available
                    if (iframePattern.thumbUrl != null) {
                        String thumbUrl = String.format(iframePattern.thumbUrl, urlPrefix, videoId);
                        html = String.format(Locale.US, videoThumbLink, href, thumbUrl);
                    }
                    break;
                }
            }
            iframe.replaceWith(Jsoup.parse(html).body().child(0));
        } else {
            iframe.remove();
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) Element(org.jsoup.nodes.Element) TextNode(org.jsoup.nodes.TextNode) Elements(org.jsoup.select.Elements)

Example 85 with Element

use of org.jsoup.nodes.Element in project JsoupXpath by zhegexiaohuozi.

the class XpathProcessor method visitAbbreviatedStep.

@Override
public XValue visitAbbreviatedStep(XpathParser.AbbreviatedStepContext ctx) {
    if ("..".equals(ctx.getText())) {
        Set<Element> total = new HashSet<>();
        Elements newContext = new Elements();
        for (Element e : currentScope().context()) {
            total.add(e.parent());
        }
        newContext.addAll(total);
        return XValue.create(newContext);
    } else {
        return XValue.create(currentScope().context());
    }
}
Also used : Element(org.jsoup.nodes.Element) Elements(org.jsoup.select.Elements) HashSet(java.util.HashSet)

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