View Javadoc
1   package us.codecraft.webmagic.example;
2   
3   import us.codecraft.webmagic.Site;
4   import us.codecraft.webmagic.model.ConsolePageModelPipeline;
5   import us.codecraft.webmagic.model.HasKey;
6   import us.codecraft.webmagic.model.OOSpider;
7   import us.codecraft.webmagic.model.annotation.ExtractBy;
8   import us.codecraft.webmagic.model.annotation.ExtractByUrl;
9   
10  import java.util.List;
11  
12  /**
13   * @author code4crafter@gmail.com <br>
14   * @since 0.4.1
15   */
16  public class GithubRepoApi implements HasKey {
17  
18      @ExtractBy(type = ExtractBy.Type.JsonPath, value = "$.name", source = ExtractBy.Source.RawText)
19      private String name;
20  
21      @ExtractBy(type = ExtractBy.Type.JsonPath, value = "$..owner.login", source = ExtractBy.Source.RawText)
22      private String author;
23  
24      @ExtractBy(type = ExtractBy.Type.JsonPath, value = "$.language",multi = true, source = ExtractBy.Source.RawText)
25      private List<String> language;
26  
27      @ExtractBy(type = ExtractBy.Type.JsonPath, value = "$.stargazers_count", source = ExtractBy.Source.RawText)
28      private int star;
29  
30      @ExtractBy(type = ExtractBy.Type.JsonPath, value = "$.forks_count", source = ExtractBy.Source.RawText)
31      private int fork;
32  
33      @ExtractByUrl
34      private String url;
35  
36      public static void main(String[] args) {
37          OOSpider.create(Site.me().setSleepTime(100)
38                  , new ConsolePageModelPipeline(), GithubRepoApi.class)
39                  .addUrl("https://api.github.com/repos/code4craft/webmagic").run();
40      }
41  
42      @Override
43      public String key() {
44          return author + ":" + name;
45      }
46  
47      public String getName() {
48          return name;
49      }
50  
51      public String getAuthor() {
52          return author;
53      }
54  
55      public List<String> getLanguage() {
56          return language;
57      }
58  
59      public String getUrl() {
60          return url;
61      }
62  
63      public int getStar() {
64          return star;
65      }
66  
67      public int getFork() {
68          return fork;
69      }
70  }