(no commit message)
[utils] / crawler / basic / src / org / wamblee / crawler / impl / App.java
1 package org.wamblee.crawler.impl;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.InputStream;
7 import java.io.PrintStream;
8
9 import org.apache.commons.httpclient.HttpClient;
10 import org.dom4j.Element;
11 import org.wamblee.crawler.Action;
12 import org.wamblee.crawler.Configuration;
13 import org.wamblee.crawler.Crawler;
14 import org.wamblee.crawler.Page;
15
16 /*
17  * Copyright 2005 the original author or authors.
18  * 
19  * Licensed under the Apache License, Version 2.0 (the "License");
20  * you may not use this file except in compliance with the License.
21  * You may obtain a copy of the License at
22  * 
23  *      http://www.apache.org/licenses/LICENSE-2.0
24  * 
25  * Unless required by applicable law or agreed to in writing, software
26  * distributed under the License is distributed on an "AS IS" BASIS,
27  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  * See the License for the specific language governing permissions and
29  * limitations under the License.
30  */
31
32 /**
33  * Entry point for the crawler.
34  */
35 public class App {
36
37     private static final String LOG_FILE = "crawler.log";
38
39     public static void main(String[] args) throws Exception {
40         String configFileName = args[0];
41         String starturl = args[1];
42  
43         FileOutputStream fos = new FileOutputStream(new File(LOG_FILE));
44         PrintStream os = new PrintStream(fos);
45
46         try {
47             ConfigurationParser parser = new ConfigurationParser(os);
48             InputStream configFile = new FileInputStream(new File(
49                     configFileName));
50             Configuration config = parser.parse(configFile);
51
52             HttpClient client = new HttpClient();
53             // client.getHostConfiguration().setProxy("localhost", 3128);
54             
55             Crawler crawler = new CrawlerImpl(client, config);
56
57             System.out.println("Retrieving: " + starturl);
58             Page page = crawler.getPage(starturl);
59             showPage(page);
60             page = page.getAction("channels-favorites").execute();
61             recordInterestingShows(page);
62             showPage(page);
63             page = page.getAction("Nederland 1").execute();
64             showPage(page);
65             page = page.getAction("right-now").execute();
66             showPage(page);
67             page = page.getAction("Het elfde uur").execute();
68             showPage(page);
69         } finally {
70             os.flush();
71             os.close();
72             System.out.println("Output written on '" + LOG_FILE + "'");
73         }
74     }
75
76     /**
77      * @param starturl
78      * @param crawler
79      */
80     private static void showPage(Page aPage) {
81         Action[] links = aPage.getActions();
82         for (Action link: links) { 
83             System.out.println("Link found '" + link.getName() + "'");
84         }
85         Element element = aPage.getContent(); 
86         System.out.println("Retrieved content: " + element.asXML());
87     }
88     
89     private static void recordInterestingShows(Page page) { 
90         Action[] channels = page.getActions();
91         for (Action channel: channels) { 
92             examineChannel(channel.getName(), channel.execute().getAction("right-now").execute());
93         }
94     }
95     
96     private static void examineChannel(String aChannel, Page aPage) { 
97         Action[] programs = aPage.getActions(); 
98         for (Action program: programs) { 
99             System.out.println(aChannel + " - " + program.getName());
100             if ( program.getName().toLowerCase().matches(".*babe.*")) { 
101                 Page programPage = program.execute();
102                 Action record = programPage.getAction("record"); 
103                 System.out.println("Recording possible: " + record != null);
104             }
105         }
106     }
107
108 }