1 package org.wamblee.crawler.impl;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.InputStream;
7 import java.io.PrintStream;
9 import org.apache.commons.httpclient.HttpClient;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.dom4j.Element;
13 import org.wamblee.crawler.Action;
14 import org.wamblee.crawler.Configuration;
15 import org.wamblee.crawler.Crawler;
16 import org.wamblee.crawler.Page;
17 import org.wamblee.crawler.PageException;
20 * Copyright 2005 the original author or authors.
22 * Licensed under the Apache License, Version 2.0 (the "License");
23 * you may not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
26 * http://www.apache.org/licenses/LICENSE-2.0
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
36 * Entry point for the crawler.
38 public final class App {
41 * Disabled constructor.
48 private static final Log LOG = LogFactory.getLog(App.class);
50 private static final String LOG_FILE = "crawler.log";
52 public static void main(String[] aArgs) throws Exception {
53 String configFileName = aArgs[0];
54 String starturl = aArgs[1];
56 FileOutputStream fos = new FileOutputStream(new File(LOG_FILE));
57 PrintStream os = new PrintStream(fos);
60 ConfigurationParser parser = new ConfigurationParser(os);
61 InputStream configFile = new FileInputStream(new File(
63 Configuration config = parser.parse(configFile);
65 HttpClient client = new HttpClient();
66 // client.getHostConfiguration().setProxy("localhost", 3128);
68 Crawler crawler = new CrawlerImpl(client, config);
70 System.out.println("Retrieving: " + starturl);
71 Page page = crawler.getPage(starturl);
73 page = page.getAction("channels-favorites").execute();
74 recordInterestingShows(page);
76 page = page.getAction("Nederland 1").execute();
78 page = page.getAction("right-now").execute();
80 page = page.getAction("Het elfde uur").execute();
85 System.out.println("Output written on '" + LOG_FILE + "'");
93 private static void showPage(Page aPage) {
94 Action[] links = aPage.getActions();
95 for (Action link : links) {
96 System.out.println("Link found '" + link.getName() + "'");
98 Element element = aPage.getContent();
99 System.out.println("Retrieved content: " + element.asXML());
102 private static void recordInterestingShows(Page page) throws PageException {
103 Action[] channels = page.getActions();
104 for (Action channel : channels) {
105 examineChannel(channel.getName(), channel.execute().getAction(
106 "right-now").execute());
110 private static void examineChannel(String aChannel, Page aPage) throws PageException {
111 Action[] programs = aPage.getActions();
112 for (Action program : programs) {
113 System.out.println(aChannel + " - " + program.getName());
114 if (program.getName().toLowerCase().matches(".*babe.*")) {
115 Page programPage = program.execute();
116 Action record = programPage.getAction("record");
117 System.out.println("Recording possible: " + record != null);