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