f9b9bd45eef23de75507357193d2b4d7c01f6d9f
[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 import org.wamblee.xml.XslTransformer;
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  * Test application which uses the crawler. 
34  */
35 public final class App {
36
37     /**
38      * Disabled constructor.
39      * 
40      */
41     private App() {
42         // Empty
43     }
44     
45     /**
46      * Runs a test program.
47      * 
48      * @param aArgs
49      *            Arguments. First argument is the crawler config file name and
50      *            second argument is the start url.
51      * @throws Exception
52      *             In case of problems.
53      */
54     public static void main(String[] aArgs) throws Exception {
55         String configFileName = aArgs[0];
56         String starturl = aArgs[1];
57
58         ConfigurationParser parser = new ConfigurationParser(new XslTransformer());
59         InputStream configFile = new FileInputStream(new File(configFileName));
60         Configuration config = parser.parse(configFile);
61
62         HttpClient client = new HttpClient();
63         // client.getHostConfiguration().setProxy("localhost", 3128);
64
65         Crawler crawler = new CrawlerImpl(client, config);
66
67         System.out.println("Retrieving: " + starturl);
68         Page page = crawler.getPage(starturl);
69         showPage(page);
70         page = page.getAction("channels-favorites").execute();
71         recordInterestingShows(page);
72         showPage(page);
73         page = page.getAction("Nederland 1").execute();
74         showPage(page);
75         page = page.getAction("right-now").execute();
76         showPage(page);
77         page = page.getAction("Het elfde uur").execute();
78         showPage(page);
79     }
80
81     /**
82      * @param starturl
83      * @param crawler
84      */
85     private static void showPage(Page aPage) {
86         Action[] links = aPage.getActions();
87         for (Action link : links) {
88             System.out.println("Link found '" + link.getName() + "'");
89         }
90         Element element = aPage.getContent();
91         System.out.println("Retrieved content: " + element.asXML());
92     }
93
94     private static void recordInterestingShows(Page page) throws PageException {
95         Action[] channels = page.getActions();
96         for (Action channel : channels) {
97             examineChannel(channel.getName(), channel.execute().getAction(
98                     "right-now").execute());
99         }
100     }
101
102     private static void examineChannel(String aChannel, Page aPage)
103             throws PageException {
104         Action[] programs = aPage.getActions();
105         for (Action program : programs) {
106             System.out.println(aChannel + " - " + program.getName());
107             if (program.getName().toLowerCase().matches(".*babe.*")) {
108                 Page programPage = program.execute();
109                 Action record = programPage.getAction("record");
110                 System.out.println("Recording possible: " + record != null);
111             }
112         }
113     }
114
115 }