Search in sources :

Example 1 with Topic

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

the class GetTopicListTask method run.

@Override
public void run() {
    List<Topic> topics = new ArrayList<>();
    boolean succeed = false;
    boolean hasMore = false;
    try {
        Document doc = get(mUrl);
        Elements elements = doc.select("div.topic-item");
        for (Element element : elements) {
            Topic topic = createTopicFromElement(element);
            topics.add(topic);
        }
        succeed = true;
        Elements paginationElements = doc.select("ul.pagination");
        if (!paginationElements.isEmpty()) {
            Elements disabledElements = paginationElements.select("li.disabled");
            if (disabledElements.isEmpty()) {
                hasMore = true;
            } else if (disabledElements.last() != null) {
                Elements disableLinkElements = disabledElements.last().select("a");
                if (!ConstantUtil.NEXT_PAGE.equals(disableLinkElements.text())) {
                    hasMore = true;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (succeed) {
        TopicList topicList = new TopicList();
        topicList.setTopics(topics);
        topicList.setHasMore(hasMore);
        successOnUI(topicList);
    } else {
        failedOnUI("获取主题列表失败");
    }
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) TopicList(org.mazhuang.guanggoo.data.entity.TopicList) IOException(java.io.IOException) Topic(org.mazhuang.guanggoo.data.entity.Topic) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 2 with Topic

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

the class GetTopicListTask method createTopicFromElement.

public static Topic createTopicFromElement(Element element) {
    Topic topic = new Topic();
    Element titleElement = element.select("h3.title").select("a").first();
    if (titleElement != null) {
        // 主题列表页
        topic.setTitle(titleElement.text());
    } else {
        // 主题详情页
        titleElement = element.select("h3.title").first();
        if (titleElement != null) {
            topic.setTitle(titleElement.text());
        }
    }
    topic.setUrl(titleElement.absUrl("href"));
    topic.setAvatar(element.select("img.avatar").attr("src"));
    Element metaElement = element.select("div.meta").first();
    Meta meta = createMetaFromElement(metaElement);
    topic.setMeta(meta);
    Elements countElements = element.select("div.count");
    if (!countElements.isEmpty()) {
        topic.setCount(Integer.valueOf(countElements.first().select("a").first().text()));
    }
    return topic;
}
Also used : Meta(org.mazhuang.guanggoo.data.entity.Meta) Element(org.jsoup.nodes.Element) Topic(org.mazhuang.guanggoo.data.entity.Topic) Elements(org.jsoup.select.Elements)

Example 3 with Topic

use of org.mazhuang.guanggoo.data.entity.Topic 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 4 with Topic

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

the class GetReplyListTask method run.

@Override
public void run() {
    List<Reply> replies = new ArrayList<>();
    boolean succeed = false;
    boolean hasMore = false;
    try {
        Document doc = get(mUrl);
        Elements elements = doc.select("div.reply-item");
        for (Element element : elements) {
            Reply reply = new Reply();
            Elements topicElements = element.select("span.title a");
            if (topicElements.isEmpty()) {
                continue;
            }
            Topic topic = new Topic();
            topic.setTitle(topicElements.first().text());
            topic.setUrl(topicElements.first().absUrl("href"));
            reply.setTopic(topic);
            Elements contentElements = element.select("div.content");
            if (contentElements.isEmpty()) {
                continue;
            }
            reply.setContent(contentElements.outerHtml());
            replies.add(reply);
        }
        succeed = true;
        Elements paginationElements = doc.select("ul.pagination");
        if (!paginationElements.isEmpty()) {
            Elements disabledElements = paginationElements.select("li.disabled");
            if (disabledElements.isEmpty()) {
                hasMore = true;
            } else if (disabledElements.last() != null) {
                Elements disableLinkElements = disabledElements.last().select("a");
                if (!ConstantUtil.NEXT_PAGE.equals(disableLinkElements.text())) {
                    hasMore = true;
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (succeed) {
        ReplyList replyList = new ReplyList();
        replyList.setReplies(replies);
        replyList.setHasMore(hasMore);
        successOnUI(replyList);
    } else {
        failedOnUI("获取回复列表失败");
    }
}
Also used : Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Reply(org.mazhuang.guanggoo.data.entity.Reply) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Topic(org.mazhuang.guanggoo.data.entity.Topic) ReplyList(org.mazhuang.guanggoo.data.entity.ReplyList)

Aggregations

Elements (org.jsoup.select.Elements)4 Topic (org.mazhuang.guanggoo.data.entity.Topic)4 IOException (java.io.IOException)3 Document (org.jsoup.nodes.Document)3 Element (org.jsoup.nodes.Element)3 ArrayList (java.util.ArrayList)2 Comment (org.mazhuang.guanggoo.data.entity.Comment)1 Favorite (org.mazhuang.guanggoo.data.entity.Favorite)1 Meta (org.mazhuang.guanggoo.data.entity.Meta)1 Reply (org.mazhuang.guanggoo.data.entity.Reply)1 ReplyList (org.mazhuang.guanggoo.data.entity.ReplyList)1 TopicDetail (org.mazhuang.guanggoo.data.entity.TopicDetail)1 TopicList (org.mazhuang.guanggoo.data.entity.TopicList)1