Search in sources :

Example 1 with Node

use of forpdateam.ru.forpda.api.regex.parser.Node in project ForPDA by RadiationX.

the class NewsApi method recurseComments.

private Comment recurseComments(final SparseArray<Comment.Karma> karmaMap, Node root, Comment parentComment, int level) {
    Node rootComments = Parser.findNode(root, "ul", "class", "comment-list");
    ArrayList<Node> commentNodes = Parser.findChildNodes(rootComments, "li", null, null);
    /*if (commentNodes.size() == 0) {
            return null;
        }*/
    for (Node commentNode : commentNodes) {
        Comment comment = new Comment();
        String id = null, userId = null, userNick = null, date = null, content = null;
        Matcher matcher;
        Node anchorNode = Parser.findNode(commentNode, "div", "id", "comment-");
        if (anchorNode == null) {
            continue;
        }
        id = anchorNode.getAttribute("id");
        if (id != null) {
            matcher = idPattern.matcher(id);
            if (matcher.find()) {
                id = matcher.group(1);
                comment.setId(Integer.parseInt(id));
            }
        }
        String deletedString = anchorNode.getAttribute("class");
        boolean isDeleted = deletedString != null && deletedString.contains("deleted");
        comment.setDeleted(isDeleted);
        if (!isDeleted) {
            Node nickNode = Parser.findNode(commentNode, "a", "class", "nickname");
            Node metaNode = Parser.findNode(commentNode, "span", "class", "h-meta");
            userId = nickNode.getAttribute("href");
            if (userId != null) {
                matcher = userIdPattern.matcher(userId);
                if (matcher.find()) {
                    userId = matcher.group(1);
                    comment.setUserId(Integer.parseInt(userId));
                }
            }
            userNick = Parser.getHtml(nickNode, true);
            comment.setUserNick(ApiUtils.fromHtml(userNick));
            date = Parser.ownText(metaNode).trim();
            date = date.replace(" |", ",");
            comment.setDate(date);
        }
        Node contentNode = Parser.findNode(commentNode, "p", "class", "content");
        content = Parser.getHtml(contentNode, true);
        comment.setContent(ApiUtils.fromHtml(content));
        comment.setLevel(level);
        comment.setKarma(karmaMap.get(comment.getId()));
        parentComment.addChild(comment);
        level++;
        recurseComments(karmaMap, commentNode, comment, level);
        level--;
    }
    return parentComment;
}
Also used : Comment(forpdateam.ru.forpda.api.news.models.Comment) Matcher(java.util.regex.Matcher) Node(forpdateam.ru.forpda.api.regex.parser.Node)

Aggregations

Comment (forpdateam.ru.forpda.api.news.models.Comment)1 Node (forpdateam.ru.forpda.api.regex.parser.Node)1 Matcher (java.util.regex.Matcher)1