package us.codecraft.webmagic.model.samples;

import us.codecraft.webmagic.MultiPageModel;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.OOSpider;
import us.codecraft.webmagic.model.annotation.ComboExtract;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.ExtractByUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.pipeline.MultiPagePipeline;
import us.codecraft.webmagic.scheduler.RedisScheduler;

import java.util.Collection;
import java.util.List;

/**
 * @author code4crafter@gmail.com <br>
 */
@TargetUrl("http://news.163.com/\\d+/\\d+/\\d+/\\w+*.html")
public class News163 implements MultiPageModel {

    @ExtractByUrl("http://news\\.163\\.com/\\d+/\\d+/\\d+/([^_]*).*\\.html")
    private String pageKey;

    @ExtractByUrl(value = "http://news\\.163\\.com/\\d+/\\d+/\\d+/\\w+_(\\d+)\\.html", notNull = false)
    private String page;

    @ComboExtract(value = {@ExtractBy("//div[@class=\"ep-pages\"]//a/@href"),
            @ExtractBy(value = "http://news\\.163\\.com/\\d+/\\d+/\\d+/\\w+_(\\d+)\\.html", type = ExtractBy.Type.Regex)},
            multi = true, notNull = false)
    private List<String> otherPage;

    @ExtractBy("//h1[@id=\"h1title\"]/text()")
    private String title;

    @ExtractBy("//div[@id=\"epContentLeft\"]")
    private String content;

    @Override
    public String getPageKey() {
        return pageKey;
    }

    @Override
    public Collection<String> getOtherPages() {
        return otherPage;
    }

    @Override
    public String getPage() {
        if (page == null) {
            return "1";
        }
        return page;
    }

    @Override
    public MultiPageModel combine(MultiPageModel multiPageModel) {
        News163 news163 = new News163();
        news163.title = this.title;
        News163 pagedModel1 = (News163) multiPageModel;
        news163.content = this.content + pagedModel1.content;
        return news163;
    }

    @Override
    public String toString() {
        return "News163{" +
                "content='" + content + '\'' +
                ", title='" + title + '\'' +
                ", otherPage=" + otherPage +
                '}';
    }

    public static void main(String[] args) {
        OOSpider.create(Site.me().addStartUrl("http://news.163.com/13/0802/05/958I1E330001124J_2.html"), News163.class)
                .scheduler(new RedisScheduler("localhost")).clearPipeline().pipeline(new MultiPagePipeline()).pipeline(new ConsolePipeline()).run();
    }

}

@@ -168,68 +168,8 @@
 er;%0A
-import us.codecraft.webmagic.model.annotation.ComboExtract;%0A
 impo
@@ -221,16 +221,16 @@
 ractBy;%0A
+
 import u
@@ -961,20 +961,17 @@
    @
-Combo
 Extract
+By
 (val
@@ -979,20 +979,8 @@
 e = 
-%7B@ExtractBy(
 %22//d
@@ -1010,49 +1010,15 @@
 //a/
-@href%22),%0A            @ExtractBy(value = %22
+regex('
 http
@@ -1074,41 +1074,13 @@
 html
-%22, type = ExtractBy.Type.Regex)%7D,
+',1)%22
 %0A   
@@ -1087,16 +1087,18 @@
         
+ ,
  multi =
@@ -2242,17 +2242,28 @@
 me()
-.addStart
+, News163.class).add
 Url(
@@ -2319,32 +2319,16 @@
 2.html%22)
-, News163.class)
 %0A       
@@ -2384,25 +2384,12 @@
 %22)).
-clearPipeline().p
+addP
 ipel
@@ -2417,17 +2417,20 @@
 line()).
-p
+addP
 ipeline(

update News163: use Xsoup 0.2.0 syntax instead of ComboExtract