package cn.edu.sjtu.at15.forum.crawler.discuz;

import cn.edu.sjtu.at15.forum.crawler.Parser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by at15 on 15-11-19.
 */
public class ThreadParser extends Parser {
    private static final Logger LOGGER = LoggerFactory.getLogger(ThreadParser.class);
    private String title;
    private String author;
    private Integer viewCount;
    private Integer replyCount;
    private String authorPost;

    public ThreadParser(String html) {
        super(html);
    }

    public String getTitle() {
        if (title != null) {
            return title;
        }
        title = document.select("span#thread_subject").text();
        return title;
    }

    public String getAuthor() {
        if (author != null) {
            return author;
        }
        author = document.select("#postlist > div").first()
                .select("div.authi > a.xi2").text();
        return author;
    }

    public Integer getViewCount() {
        if (viewCount != null) {
            return viewCount;
        }
        String count = document.select("#postlist > table").first()
                .select("span.xi1").first().text();
        viewCount = Integer.valueOf(count);
        return viewCount;
    }

    public Integer getReplyCount() {
        if (replyCount != null) {
            return replyCount;
        }
        String count = document.select("#postlist > table").first()
                .select("span.xi1").get(1).text();
        replyCount = Integer.valueOf(count);
        return replyCount;
    }


    public String getAuthorPost() {
        if (authorPost != null) {
            return authorPost;
        }
        // TODO: clear the ads using http://jsoup.org/cookbook/modifying-data/set-text
        authorPost = document.select("#postlist > div").first()
                .select("td.t_f").text();
        return authorPost;
    }

}

@@ -530,297 +530,88 @@
     
-%7D%0A%0A
     
-public String getTitle() %7B%0A        if (title != null) %7B%0A            return title;%0A        %7D%0A        title = document.select(%22span#thread_subject%22).text();%0A        return title;%0A    %7D%0A%0A    public String getAuthor() %7B%0A        if (author != null) %7B%0A            return author;%0A        %7D
+String count;%0A        title = document.select(%22span#thread_subject%22).text();
 %0A   
@@ -732,65 +732,112 @@
     
-return author;%0A    %7D%0A%0A    public Integer getViewCount() %7B
+count = document.select(%22#postlist %3E table%22).first()%0A                .select(%22span.xi1%22).first().text();
 %0A   
@@ -841,20 +841,16 @@
         
-if (
 viewCoun
@@ -855,47 +855,32 @@
 unt 
-!
 = 
-null) %7B%0A            return viewC
+Integer.valueOf(c
 ount
+)
 ;%0A  
@@ -885,33 +885,16 @@
         
-%7D%0A        String 
 count = 
@@ -966,38 +966,37 @@
 ect(%22span.xi1%22).
-firs
+ge
 t(
+1
 ).text();%0A      
@@ -989,36 +989,37 @@
 text();%0A        
-view
+reply
 Count = Integer.
@@ -1046,160 +1046,278 @@
     
-return viewCount;
+// TODO: clear the ads using http://jsoup.org/cookbook/modifying-data/set-text
 %0A    
-%7D%0A%0A
     
-public Integer getReplyCount() %7B%0A        if (replyCount != null) %7B%0A            return replyCount;%0A    
+authorPost = document.select(%22#postlist %3E div%22).first()%0A                .select(%22td.t_f%22).text();%0A    %7D%0A%0A    public String getTitle() %7B%0A        return title;%0A
     %7D%0A
+%0A
     
-   
+public
  String 
 coun
@@ -1316,111 +1316,87 @@
 ing 
-count = document.select(%22#postlist %3E table%22).first()%0A                .select(%22span.xi1%22).get(1).text();
+getAuthor() %7B%0A        return author;%0A    %7D%0A%0A    public Integer getViewCount() %7B
 %0A   
@@ -1406,42 +1406,67 @@
   re
-plyCount = Integer.valueOf(count);
+turn viewCount;%0A    %7D%0A%0A    public Integer getReplyCount() %7B
 %0A   
@@ -1496,17 +1496,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -1536,276 +1536,8 @@
 ) %7B%0A
-        if (authorPost != null) %7B%0A            return authorPost;%0A        %7D%0A        // TODO: clear the ads using http://jsoup.org/cookbook/modifying-data/set-text%0A        authorPost = document.select(%22#postlist %3E div%22).first()%0A                .select(%22td.t_f%22).text();%0A
     
@@ -1565,11 +1565,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

Move thread parser parse into constructor