package org.bee.webBee;

import org.bee.webBee.download.HttpClientDownloader;
import org.bee.webBee.html.Html;
import org.bee.webBee.processor.PageProcessor;
import org.bee.webBee.download.DownLoader;
import org.bee.webBee.linker.Page;
import org.bee.webBee.linker.Request;
import org.bee.webBee.processor.Setting;
import org.bee.webBee.processor.Task;

import java.io.IOException;

/**
 * webBee框架核心入口
 *
 * data 2017-03-23   01:06
 * E-mail   sis.nonacosa@gmail.com
 * @author sis.nonacosa
 */
public class Bee implements Runnable,Task {

    private PageProcessor pageProcessor;

    private DownLoader downLoader = new HttpClientDownloader();

    private Request request;

    private Setting setting;

    private static  Integer COUNT = 0;

    private Html html;
    /**
     * 实例化处理规则
     * @param pageProcessor 用来指向指针到具体规则实现类
     */
    public Bee(PageProcessor pageProcessor) {
        this.pageProcessor = pageProcessor;
        this.setting = pageProcessor.getSetting(); //获取用户配置
    }

    /**
     * 新建一个处理器
     * @param pageProcessor 自定义的处理规则
     * @return
     */
    public static Bee create(PageProcessor pageProcessor){
        return new Bee(pageProcessor);
    }


    @Override
    public void run() {
        if(COUNT==0){
            requestProcessor();
        }else{
            requestNextProcessor();
        }

        System.out.println("this is Bee.class implement Runnable's run function! --request:" + request.toString());
        try {
            pageProcessor.process(pageProcessor(request));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取带抓取请求信息
     * @return
     */
    public void requestProcessor(){
        this.request = new Request(setting.getUrl());
        COUNT++;
    }

    public Page pageProcessor(Request request){

        return downLoader.download(request, this); //annotation: this 'this' to substitute one expression with Task's implement
    }

    /**
     * 获取下一个带抓取请求信息
     * @return
     */
    private void requestNextProcessor() {
        System.out.println("---"+html.getApi());
    }

    @Override
    public Setting getSetting() {
        return setting;
    }

    @Override
    public void setHtml(Html html) {
        this.html = html;
    }


}

@@ -18,16 +18,90 @@
 ebBee;%0A%0A
+import com.alibaba.fastjson.JSON;%0Aimport com.alibaba.fastjson.JSONObject;%0A
 import o
@@ -1300,34 +1300,8 @@
 ) %7B%0A
-        if(COUNT==0)%7B%0A    
     
@@ -1336,15 +1336,63 @@
     
-%7Delse%7B%0A
+while (request!=null  )%7B%0A            if(COUNT%3E1)%7B%0A%0A    
     
@@ -1427,27 +1427,34 @@
 ();%0A        
-%7D%0A%0A
+    %7D%0A    
         Syst
@@ -1569,14 +1569,58 @@
     
+    
 try %7B%0A
+                Thread.sleep(1000);%0A    
     
@@ -1686,64 +1686,38 @@
     
-%7D catch (IOException e) %7B%0A            e.printStackTrace(
+        System.out.println(%22 %22
 );%0A 
@@ -1727,100 +1727,97 @@
     
-%7D%0A
     %7D
-%0A%0A    /**%0A     * %E8%8E%B7%E5%8F%96%E5%B8%A6%E6%8A%93%E5%8F%96%E8%AF%B7%E6%B1%82%E4%BF%A1%E6%81%AF%0A     * @return%0A
+ catch (IOException %7C InterruptedException e) %7B%0A      
      
-*/%0A
     
-public void requestProcessor()%7B
+ e.printStackTrace();
 %0A   
@@ -1825,53 +1825,13 @@
     
-this.request = new Request(setting.getUrl());
+    %7D
 %0A   
@@ -1839,16 +1839,10 @@
     
-COUNT++;
+%7D%0A
 %0A   
@@ -2050,129 +2050,527 @@
 * %E8%8E%B7%E5%8F%96
-%E4%B8%8B%E4%B8%80%E4%B8%AA%E5%B8%A6%E6%8A%93%E5%8F%96%E8%AF%B7%E6%B1%82%E4%BF%A1%E6%81%AF%0A     * @return%0A     */%0A    private void requestNextProcessor() %7B%0A        System.out.println(%22---%22+html.getApi(
+%E5%B8%A6%E6%8A%93%E5%8F%96%E8%AF%B7%E6%B1%82%E4%BF%A1%E6%81%AF%0A     * @return%0A     */%0A    public void requestProcessor()%7B%0A        this.request = new Request(setting.getUrl());%0A        COUNT++;%0A    %7D%0A%0A    /**%0A     * %E8%8E%B7%E5%8F%96%E4%B8%8B%E4%B8%80%E4%B8%AA%E5%B8%A6%E6%8A%93%E5%8F%96%E8%AF%B7%E6%B1%82%E4%BF%A1%E6%81%AF%0A     * @return%0A     */%0A    private void requestNextProcessor() %7B%0A        this.request = new Request(setting.getUrl());%0A        JSONObject json = (JSONObject) JSON.parse(html.getApi());%0A        json.get(setting.getNextUrlKeyOnResult());%0A        System.out.println(%22---%22+ ((JSONObject) JSON.parse(html.getApi())).get(setting.getNextUrlKeyOnResult()
 ));%0A

fix #12 auto get next page context