Search in sources :

Example 56 with Elements

use of org.jsoup.select.Elements in project CodeUtils by boredream.

the class Main method parseApi.

private static ArrayList<RequestInfo> parseApi(String typeUrl) {
    ArrayList<RequestInfo> requestInfos = new ArrayList<RequestInfo>();
    try {
        String response = BmobHttpUtils.getString(typeUrl);
        Document parse = Jsoup.parse(response);
        Elements refentryElements = parse.getElementsByClass("refentry");
        for (Element element : refentryElements) {
            Elements hrefElement = element.getElementsByAttribute("href");
            if (hrefElement.size() > 0) {
                Element e = hrefElement.get(0);
                String link = e.attr("href");
                RequestInfo info = getApiInfo(link);
                requestInfos.add(info);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return requestInfos;
}
Also used : Json2JavaElement(entity.Json2JavaElement) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) RequestInfo(entity.RequestInfo) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 57 with Elements

use of org.jsoup.select.Elements in project LeMondeRssReader by MBach.

the class ArticleActivity method extractComments.

private List<Model> extractComments(Element doc, boolean loadMoreComments) {
    List<Model> commentList = new ArrayList<>();
    // Extract header
    if (!loadMoreComments) {
        Elements header = doc.select("[itemprop='InteractionCount']");
        if (atLeastOneChild(header)) {
            TextView commentHeader = new TextView(getBaseContext());
            commentHeader.setText(String.format("Commentaires %s", header.text()));
            commentHeader.setTypeface(null, Typeface.BOLD);
            commentHeader.setTextColor(Color.WHITE);
            commentHeader.setPadding(0, 0, 0, Constants.PADDING_COMMENT_ANSWER);
            commentList.add(new Model(commentHeader, 0));
        }
    }
    // Extract comments
    Elements comments = doc.select("[itemprop='commentText']");
    for (Element comment : comments) {
        Elements refs = comment.select("p.references");
        if (atLeastOneChild(refs)) {
            // Clear date
            refs.select("span").remove();
            TextView author = new TextView(getBaseContext());
            author.setTypeface(null, Typeface.BOLD);
            author.setText(refs.text());
            author.setTextColor(Color.WHITE);
            Elements commentComment = refs.next();
            if (atLeastOneChild(commentComment)) {
                TextView content = new TextView(getBaseContext());
                content.setText(commentComment.first().text());
                content.setTextColor(Color.WHITE);
                if (comment.hasClass("reponse")) {
                    author.setPadding(Constants.PADDING_COMMENT_ANSWER, 0, 0, 12);
                    content.setPadding(Constants.PADDING_COMMENT_ANSWER, 0, 0, 16);
                } else {
                    author.setPadding(0, 0, 0, 12);
                    content.setPadding(0, 0, 0, 16);
                }
                Integer commentId = Integer.valueOf(comment.attr("data-reaction_id"));
                commentList.add(new Model(author, commentId));
                commentList.add(new Model(content, commentId));
            }
        }
    }
    // Extract full comments page URI
    Elements div = doc.select("div.reactions");
    if (atLeastOneChild(div)) {
        Element fullComments = div.first().nextElementSibling();
        Elements next = fullComments.select("a");
        if (atLeastOneChild(next)) {
            commentsURI = next.first().attr("href");
        }
    }
    return commentList;
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) TextView(android.widget.TextView) Elements(org.jsoup.select.Elements)

Example 58 with Elements

use of org.jsoup.select.Elements in project LeMondeRssReader by MBach.

the class ArticleActivity method extractDates.

@NonNull
private String extractDates(@NonNull Element article) {
    StringBuilder builder = new StringBuilder("");
    Elements datePublished = article.select("[itemprop='datePublished']");
    if (!datePublished.isEmpty()) {
        builder.append("Publié le ").append(datePublished.first().text());
    }
    Elements dateModified = article.select("[itemprop='dateModified']");
    if (!dateModified.isEmpty()) {
        builder.append(", modifié le ").append(dateModified.first().text());
    }
    return builder.toString();
}
Also used : Elements(org.jsoup.select.Elements) NonNull(android.support.annotation.NonNull)

Example 59 with Elements

use of org.jsoup.select.Elements in project opennms by OpenNMS.

the class HttpCollectionHandler method getTimeStamp.

/**
 * Gets the time stamp.
 *
 * @param document the JSoup document
 * @param group the group
 * @return the time stamp
 */
protected Date getTimeStamp(Document doc, XmlGroup group) {
    if (group.getTimestampXpath() == null) {
        return null;
    }
    String pattern = group.getTimestampFormat() == null ? "yyyy-MM-dd HH:mm:ss" : group.getTimestampFormat();
    LOG.debug("getTimeStamp: retrieving custom timestamp to be used when updating RRDs using selector {} and pattern {}", group.getTimestampXpath(), pattern);
    Elements el = doc.select(group.getTimestampXpath());
    if (el == null) {
        return null;
    }
    String value = el.html();
    Date date = null;
    try {
        DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern);
        DateTime dateTime = dtf.parseDateTime(value);
        date = dateTime.toDate();
    } catch (Exception e) {
        LOG.warn("getTimeStamp: can't convert custom timetime {} using pattern {}", value, pattern);
    }
    return date;
}
Also used : Elements(org.jsoup.select.Elements) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Date(java.util.Date) DateTime(org.joda.time.DateTime)

Example 60 with Elements

use of org.jsoup.select.Elements in project anton-pavlovich-bot by wyvie.

the class SlovnikCommand method parseHtml.

private List<List<String>> parseHtml(String source) {
    List<List<String>> blocks = new ArrayList<>();
    Document document = Jsoup.parse(source);
    Elements results = document.select("div#results");
    // #results > div > h1 - subject itself or nothing have been found message
    List<String> headFastDefLine = new ArrayList<>();
    headFastDefLine.add(String.format("<b>%s</b>", results.select("h1").text()));
    results.select("#fastMeanings a").forEach(element -> {
        String hrefNew = BASE_SLOVNIK_URL + element.attr("href");
        element = element.attr("href", hrefNew);
        headFastDefLine.add(element.toString());
    });
    // extended grammatics
    List<String> extDefs = new ArrayList<>();
    results.select("ol li dl").forEach(dl -> {
        Element dt = dl.selectFirst("dt");
        String def = dt.text();
        String aTagsDefs = dt.select("a").stream().map(e -> {
            String hrefNew = BASE_SLOVNIK_URL + e.attr("href");
            return e.attr("href", hrefNew).toString();
        }).collect(Collectors.joining(","));
        extDefs.add(String.format("%s %s", aTagsDefs, def));
        dl.select("dd").forEach(dd -> {
            extDefs.add(dd.wholeText());
        });
    });
    // additional definitions links
    List<String> moreDefs = new ArrayList<>();
    results.select("ul.moreResults li").forEach(li -> {
        Element aTag = li.selectFirst("a");
        li.selectFirst("a").remove();
        String hrefNew = BASE_SLOVNIK_URL + aTag.attr("href");
        aTag = aTag.attr("href", hrefNew);
        String liText = li.text();
        moreDefs.add(String.format("%s <i>%s</i>", aTag, liText));
    });
    // synonyms/antonyms
    List<String> synDefs = new ArrayList<>();
    results.select("div.other-meaning a").forEach(a -> {
        String hrefNew = BASE_SLOVNIK_URL + a.attr("href");
        synDefs.add(a.attr("href", hrefNew).toString());
    });
    // additional definitions links
    List<String> addDefs = new ArrayList<>();
    results.select("#fulltext li").forEach(li -> {
        Element aTag = li.selectFirst("a");
        li.selectFirst("a").remove();
        String hrefNew = BASE_SLOVNIK_URL + aTag.attr("href");
        aTag = aTag.attr("href", hrefNew);
        String liText = li.text();
        addDefs.add(String.format("%s <i>%s</i>", aTag, liText));
    });
    blocks.add(headFastDefLine);
    blocks.add(extDefs);
    blocks.add(synDefs);
    blocks.add(moreDefs);
    blocks.add(addDefs);
    return blocks;
}
Also used : Arrays(java.util.Arrays) UrlHelper(org.wyvie.chehov.bot.commands.helper.UrlHelper) Logger(org.slf4j.Logger) ParseMode(com.pengrad.telegrambot.model.request.ParseMode) User(com.pengrad.telegrambot.model.User) SendMessage(com.pengrad.telegrambot.request.SendMessage) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) SendResponse(com.pengrad.telegrambot.response.SendResponse) List(java.util.List) Message(com.pengrad.telegrambot.model.Message) Service(org.springframework.stereotype.Service) Document(org.jsoup.nodes.Document) Element(org.jsoup.nodes.Element) Qualifier(org.springframework.beans.factory.annotation.Qualifier) CommandHandler(org.wyvie.chehov.bot.commands.CommandHandler) Jsoup(org.jsoup.Jsoup) Elements(org.jsoup.select.Elements) TelegramBot(com.pengrad.telegrambot.TelegramBot) StringUtils(org.springframework.util.StringUtils) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Aggregations

Elements (org.jsoup.select.Elements)709 Element (org.jsoup.nodes.Element)490 Document (org.jsoup.nodes.Document)362 ArrayList (java.util.ArrayList)213 IOException (java.io.IOException)151 Test (org.junit.Test)110 URL (java.net.URL)58 List (java.util.List)47 Matcher (java.util.regex.Matcher)42 Pattern (java.util.regex.Pattern)34 HashMap (java.util.HashMap)30 InputStream (java.io.InputStream)29 Jsoup (org.jsoup.Jsoup)28 Configuration (com.vaadin.addon.charts.model.Configuration)27 File (java.io.File)26 JSONObject (org.json.JSONObject)26 JSONException (org.json.JSONException)25 Collectors (java.util.stream.Collectors)23 URISyntaxException (java.net.URISyntaxException)22 BootstrapContext (com.vaadin.flow.server.BootstrapHandler.BootstrapContext)20