1 package us.codecraft.webmagic.main;
2
3 import us.codecraft.webmagic.Site;
4 import us.codecraft.webmagic.model.OOSpider;
5 import us.codecraft.webmagic.model.samples.IteyeBlog;
6 import us.codecraft.webmagic.model.samples.News163;
7 import us.codecraft.webmagic.model.samples.OschinaBlog;
8 import us.codecraft.webmagic.pipeline.ConsolePipeline;
9 import us.codecraft.webmagic.pipeline.MultiPagePipeline;
10
11 import java.util.LinkedHashMap;
12 import java.util.Map;
13 import java.util.Scanner;
14
15
16
17
18 public class QuickStarter {
19
20 private static Map<String, Class> clazzMap;
21
22 private static Map<String, String> urlMap;
23
24 private static void init(){
25 clazzMap = new LinkedHashMap<String, Class>();
26 clazzMap.put("1", OschinaBlog.class);
27 clazzMap.put("2", IteyeBlog.class);
28 clazzMap.put("3", News163.class);
29 urlMap = new LinkedHashMap<String, String>();
30 urlMap.put("1", "http://my.oschina.net/flashsword/blog");
31 urlMap.put("2", "http://flashsword20.iteye.com/");
32 urlMap.put("3", "http://news.163.com/");
33 }
34
35 public static void main(String[] args) {
36 init();
37 String key = null;
38 key = readKey(key);
39 System.out.println("The demo started and will last 20 seconds...");
40
41 OOSpider.create(Site.me(), clazzMap.get(key)).addUrl(urlMap.get(key)).addPipeline(new MultiPagePipeline()).addPipeline(new ConsolePipeline()).runAsync();
42
43 try {
44 Thread.sleep(20000);
45 } catch (InterruptedException e) {
46 e.printStackTrace();
47 }
48 System.out.println("The demo stopped!");
49 System.out.println("To more usage, try to customize your own Spider!");
50 System.exit(0);
51 }
52
53 private static String readKey(String key) {
54 Scanner stdin = new Scanner(System.in);
55 System.out.println("Choose a Spider demo:");
56 for (Map.Entry<String, Class> classEntry : clazzMap.entrySet()) {
57 System.out.println(classEntry.getKey()+"\t" + classEntry.getValue() + "\t" + urlMap.get(classEntry.getKey()));
58 }
59 while (key == null) {
60 key = stdin.nextLine();
61 if (clazzMap.get(key) == null) {
62 System.out.println("Invalid choice!");
63 key = null;
64 }
65 }
66 return key;
67 }
68 }