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

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by gpl on 15/11/14.
 */
public class DiscuzPageProcessor implements PageProcessor {
    private static final Logger LOGGER = LoggerFactory.getLogger(DiscuzPageProcessor.class);
    private final DiscuzUrl discuzUrl;
    private Site site;

    public DiscuzPageProcessor(String baseUrl) {
        site = Site.me().setRetryTimes(3).setSleepTime(1000);
        discuzUrl = new DiscuzUrl(baseUrl);
    }

    public Site getSite() {
        return site;
    }

    public void process(Page page) {
        LOGGER.debug("processing page : " + page.getUrl().toString());
        // determine the type of this page
        // TODO
        // 1. if this is not inner link, ignore it.
        // 2. if this is a list page, get all the links in this page, and add other pages
        // NOTE: discuzz will show the total page in the last of pagination
        // 3. if this is a thread page, abstract the info we need
        for (String l : page.getHtml().links().all()) {
            LOGGER.debug(l);
        }
    }

    public static void main(String[] args) throws Exception {
        Spider.create(new DiscuzPageProcessor("http://www.1point3acres.com/bbs/"))
                .addUrl("http://www.1point3acres.com/bbs/forum.php?mod=guide&view=hot")
                .addPipeline(new ConsolePipeline())
                .thread(5)
                .run();
    }
}

@@ -848,181 +848,507 @@
     
-LOGGER.debug(%22processing page : %22 + page.getUrl().toString());%0A        // determine the type of this page%0A        // TODO%0A        // 1. if this is not inner link, ignore it.
+String currentUrl = page.getUrl().toString();%0A        LOGGER.debug(%22processing page : %22 + currentUrl);%0A        if (!discuzUrl.isInner(currentUrl)) %7B%0A            LOGGER.debug(%22not inner link : %22 + currentUrl);%0A            return;%0A        %7D%0A        // determine the type of this page%0A        if (discuzUrl.isThread(currentUrl)) %7B%0A            // TODO: parse thread content here%0A            // in fact we should handle all the comments as well, but this is not so important%0A            return;%0A        %7D
 %0A   
@@ -1354,16 +1354,22 @@
       //
+ TODO:
  2. if t
@@ -1528,66 +1528,147 @@
     
-// 3. if this is a thread page, abstract the info we need%0A
+if (discuzUrl.isList(currentUrl)) %7B%0A            // TODO: get all the threads in the list%0A            // we don't consider nested list.%0A    
     
@@ -1715,24 +1715,28 @@
 ().all()) %7B%0A
+    
             
@@ -1748,24 +1748,58 @@
 R.debug(l);%0A
+            %7D%0A            return;%0A
         %7D%0A  

Add logic for processor