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