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