Search in sources :

Example 1 with Comment

use of org.mazhuang.guanggoo.data.entity.Comment in project guanggoo-android by mzlogin.

the class GetCommentsTask method run.

@Override
public void run() {
    try {
        Document doc = get(mUrl);
        Map<Integer, Comment> comments = getCommentsFromElements(doc.select("div.topic-reply"));
        successOnUI(comments);
    } catch (IOException e) {
        e.printStackTrace();
        failedOnUI(e.getMessage());
    }
}
Also used : Comment(org.mazhuang.guanggoo.data.entity.Comment) IOException(java.io.IOException) Document(org.jsoup.nodes.Document)

Example 2 with Comment

use of org.mazhuang.guanggoo.data.entity.Comment in project guanggoo-android by mzlogin.

the class GetTopicDetailTask method run.

@Override
public void run() {
    Document doc;
    try {
        doc = get(mUrl);
    } catch (IOException e) {
        e.printStackTrace();
        failedOnUI(e.getMessage());
        return;
    }
    Elements topicDetailElements = doc.select("div.topic-detail");
    if (topicDetailElements.isEmpty()) {
        failedOnUI("找不到主题详情");
        return;
    }
    Elements elements = topicDetailElements.select("div.ui-header");
    if (elements.isEmpty()) {
        failedOnUI("找不到主题元信息");
        return;
    }
    TopicDetail topicDetail = new TopicDetail();
    Topic topic = GetTopicListTask.createTopicFromElement(elements.first());
    topicDetail.setTopic(topic);
    // 解析收藏
    Favorite favorite = new Favorite();
    Elements favouriteElement = doc.select(".J_topicFavorite");
    if (favouriteElement != null) {
        String dataType = favouriteElement.attr("data-type");
        favorite.setFavorite(!Favorite.TYPE_NOT_FAVORITE.equals(dataType));
    }
    topicDetail.setFavorite(favorite);
    elements = topicDetailElements.select("div.ui-content");
    if (elements.isEmpty()) {
        failedOnUI("找不到主题内容");
        return;
    }
    topicDetail.setContent(elements.first().outerHtml());
    Elements commentsElements = doc.select("div.topic-reply");
    Map<Integer, Comment> comments = GetCommentsTask.getCommentsFromElements(commentsElements);
    topicDetail.setComments(comments);
    successOnUI(topicDetail);
}
Also used : Favorite(org.mazhuang.guanggoo.data.entity.Favorite) Comment(org.mazhuang.guanggoo.data.entity.Comment) TopicDetail(org.mazhuang.guanggoo.data.entity.TopicDetail) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Topic(org.mazhuang.guanggoo.data.entity.Topic)

Example 3 with Comment

use of org.mazhuang.guanggoo.data.entity.Comment in project guanggoo-android by mzlogin.

the class GetCommentsTask method getCommentFromElement.

public static Comment getCommentFromElement(Element element) {
    Comment comment = new Comment();
    comment.setAvatar(element.select("img.avatar").attr("src"));
    Element metaElement = element.select("div.meta").first();
    Comment.Meta meta = new Comment.Meta();
    Element replyUsernameElement = metaElement.select("a.reply-username").first();
    User replier = new User();
    replier.setUrl(replyUsernameElement.absUrl("href"));
    replier.setUsername(replyUsernameElement.select("span.username").text());
    meta.setReplier(replier);
    meta.setTime(metaElement.select("span.time").text());
    try {
        meta.setFloor(Integer.valueOf(metaElement.select("span.fr.floor").first().text().replaceAll("[^\\d]", "")));
    } catch (NumberFormatException e) {
        e.printStackTrace();
        meta.setFloor(0);
    }
    Element voteElement = metaElement.select("a.J_replyVote").first();
    Comment.Vote vote = new Comment.Vote();
    vote.setUrl(voteElement.absUrl("href"));
    try {
        vote.setCount(Integer.valueOf(voteElement.attr("data-count")));
    } catch (NumberFormatException e) {
        e.printStackTrace();
        vote.setCount(0);
    }
    meta.setVote(vote);
    comment.setMeta(meta);
    comment.setContent(element.select("span.content").outerHtml());
    return comment;
}
Also used : Comment(org.mazhuang.guanggoo.data.entity.Comment) User(org.mazhuang.guanggoo.data.entity.User) Element(org.jsoup.nodes.Element)

Example 4 with Comment

use of org.mazhuang.guanggoo.data.entity.Comment in project guanggoo-android by mzlogin.

the class GetCommentsTask method getCommentsFromElements.

public static Map<Integer, Comment> getCommentsFromElements(Elements elements) {
    TreeMap<Integer, Comment> comments = new TreeMap<>();
    Elements replyItems = elements.select("div.reply-item");
    for (Element replyItem : replyItems) {
        Comment comment = getCommentFromElement(replyItem);
        comments.put(comment.getMeta().getFloor(), comment);
    }
    return comments.descendingMap();
}
Also used : Comment(org.mazhuang.guanggoo.data.entity.Comment) Element(org.jsoup.nodes.Element) TreeMap(java.util.TreeMap) Elements(org.jsoup.select.Elements)

Aggregations

Comment (org.mazhuang.guanggoo.data.entity.Comment)4 IOException (java.io.IOException)2 Document (org.jsoup.nodes.Document)2 Element (org.jsoup.nodes.Element)2 Elements (org.jsoup.select.Elements)2 TreeMap (java.util.TreeMap)1 Favorite (org.mazhuang.guanggoo.data.entity.Favorite)1 Topic (org.mazhuang.guanggoo.data.entity.Topic)1 TopicDetail (org.mazhuang.guanggoo.data.entity.TopicDetail)1 User (org.mazhuang.guanggoo.data.entity.User)1