(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 19 Mar 2006 00:44:19 +0000 (00:44 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 19 Mar 2006 00:44:19 +0000 (00:44 +0000)
crawler/basic/src/org/wamblee/crawler/impl/CrawlerImpl.java
crawler/kiss/src/org/wamblee/crawler/kiss/InterestingProgramAction.java [new file with mode: 0644]
crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java
crawler/kiss/src/org/wamblee/crawler/kiss/ProgramAction.java [new file with mode: 0644]
crawler/kiss/src/org/wamblee/crawler/kiss/ProgramActionExecutor.java [new file with mode: 0644]
crawler/kiss/src/org/wamblee/crawler/kiss/ProgramConfigurationParser.java
crawler/kiss/src/org/wamblee/crawler/kiss/ProgramFilter.java
crawler/kiss/src/org/wamblee/crawler/kiss/ProgramNameMatcher.java [deleted file]
crawler/kiss/src/org/wamblee/crawler/kiss/RecordProgramAction.java [new file with mode: 0644]

index 5633078cabaa71cfd2a5970cd7b1e9743270c79f..9f985dc46109a74a28092d7da82a4e88be32c4cc 100644 (file)
@@ -36,8 +36,6 @@ public class CrawlerImpl implements Crawler {
 
     private static final Log LOG = LogFactory.getLog(CrawlerImpl.class);
 
-    private static final int MAX_DELAY = 5000;
-
     private HttpClient _client;
 
     private Configuration _config;
diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/InterestingProgramAction.java b/crawler/kiss/src/org/wamblee/crawler/kiss/InterestingProgramAction.java
new file mode 100644 (file)
index 0000000..5cb9925
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2005 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wamblee.crawler.kiss;
+
+/**
+ * Represents an action to execute for an interesting program.
+ */
+public class InterestingProgramAction implements ProgramAction {
+
+    /**
+     * Category under which the interesting program is listed.
+     */
+    private String _category;
+
+    /**
+     * Constructs the action.
+     * 
+     * @param aCategory
+     *            Category of the program. Useful for structuring the output.
+     */
+    public InterestingProgramAction(String aCategory) {
+        _category = aCategory;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.wamblee.crawler.kiss.ProgramAction#execute(org.wamblee.crawler.kiss.Program,
+     *      org.wamblee.crawler.kiss.Report)
+     */
+    public void execute(Program aProgram, ProgramActionExecutor aReport) {
+        aReport.interestingProgram(_category, aProgram);
+    }
+}
index d302fb6e915b8e5bffcf9228837ef015a6fbd453..5922cc4209c175ee1c812a5f83b74769902e1e01 100644 (file)
@@ -25,7 +25,6 @@ import java.io.InputStream;
 import java.io.PrintStream;
 import java.util.ArrayList;
 import java.util.Date;
-import java.util.EnumMap;
 import java.util.List;
 import java.util.Properties;
 import java.util.Set;
@@ -50,7 +49,6 @@ import org.wamblee.crawler.Page;
 import org.wamblee.crawler.PageException;
 import org.wamblee.crawler.impl.ConfigurationParser;
 import org.wamblee.crawler.impl.CrawlerImpl;
-import org.wamblee.crawler.kiss.Program.RecordingResult;
 
 /**
  * The KiSS crawler for automatic recording of interesting TV shows.
@@ -141,9 +139,7 @@ public class KissCrawler {
             TVGuide guide = createGuide(page);
             PrintVisitor printer = new PrintVisitor(System.out);
             guide.accept(printer);
-
-          
-            recordInterestingShows(programFilters, guide);
+            processResults(programFilters, guide);
         } finally {
             os.flush();
             os.close();
@@ -161,81 +157,22 @@ public class KissCrawler {
      * @throws MessagingException
      *             In case of problems sending a summary mail.
      */
-    private void recordInterestingShows(List<ProgramFilter> aProgramCondition,
+    private void processResults(List<ProgramFilter> aProgramCondition,
             TVGuide aGuide) throws MessagingException {
-
-        Set<Program> showsToRecord = new TreeSet<Program>(new Program.TimeSorter());
-        Set<Program> interestingShows = new TreeSet<Program>(new Program.TimeSorter());
-       
+        ProgramActionExecutor executor = new ProgramActionExecutor();
         for (ProgramFilter filter : aProgramCondition) {
-            List<Program> programs = filter.apply(aGuide); 
-            switch (filter.getAction()) {
-            case RECORD: {
-                for (Program program: programs) { 
-                    showsToRecord.add(program);
-                }
-                break; 
-            }
-            case NOTIFY: { 
-                for (Program program: programs) {
-                    if ( program.isRecordingPossible()) { 
-                        interestingShows.add(program);
-                    }
-                }
-                break;
-            }
-            default: { 
-                throw new RuntimeException("Unknown action '" + filter.getAction() + "'"); 
-            }
+            List<Program> programs = filter.apply(aGuide);
+            ProgramAction action = filter.getAction(); 
+            for (Program program: programs) { 
+                action.execute(program, executor);
             }
         }
-
-        EnumMap<RecordingResult, List<Program>> messages = recordShows(showsToRecord);
-        
-        String msg = "Summary of KiSS crawler: \n\n\n";
-
-        for (RecordingResult result : RecordingResult.values()) {
-            if (messages.get(result).size() > 0) {
-                msg += result.getDescription() + "\n\n";
-                for (Program program : messages.get(result)) {
-                    msg += program + "\n\n";
-                }
-            }
-        }
-        
-        if ( interestingShows.size() > 0 ) { 
-            msg += "Possibly interesting shows:\n\n"; 
-            for (Program program: interestingShows) { 
-                msg += program + "\n\n";
-            }
-        }
-        if (showsToRecord.size()  + interestingShows.size() == 0) {
-            msg += "No suitable programs found";
-        }
-
+        executor.commit();
+        String msg = executor.getReport();
         System.out.println(msg);
         sendMail(msg);
     }
 
-    /**
-     * Records shows.
-     * @param showsToRecord Shows to record.
-     * @return Recording results. 
-     */
-    private EnumMap<RecordingResult, List<Program>> recordShows(Set<Program> showsToRecord) {
-        EnumMap<RecordingResult, List<Program>> messages = new EnumMap<RecordingResult, List<Program>>(
-                RecordingResult.class);
-        for (RecordingResult result : RecordingResult.values()) {
-            messages.put(result, new ArrayList<Program>());
-        }
-        
-        for (Program program : showsToRecord) {
-            Program.RecordingResult result = program.record();
-            messages.get(result).add(program);
-        }
-        return messages;
-    }
-
     /**
      * Creates the crawler.
      * 
diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramAction.java b/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramAction.java
new file mode 100644 (file)
index 0000000..e7395b5
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2005 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+package org.wamblee.crawler.kiss;
+
+/**
+ * Represents an action configured for a program.  
+ */
+public interface ProgramAction {
+
+    /**
+     * Executes the action. 
+     * @param aProgram Program to execute the action for. 
+     * @param aReport Report to use. 
+     */
+    void execute(Program aProgram, ProgramActionExecutor aReport);
+}
diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramActionExecutor.java b/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramActionExecutor.java
new file mode 100644 (file)
index 0000000..3865dcc
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2005 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+package org.wamblee.crawler.kiss;
+
+import java.util.ArrayList;
+import java.util.EnumMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import org.wamblee.crawler.kiss.Program.RecordingResult;
+
+/**
+ * Provides execution of actions for programs. Actions use
+ * this class to tell the executor what to do. The executor then decide
+ * on exactly what to do and in what order and makes decisions in case
+ * of conflicts.     
+ */
+public class ProgramActionExecutor {
+    
+    /**
+     * A map of category name to a set of program. Useful for displaying the output of 
+     * possibly interesting programs on a per category basis. 
+     */
+    private Map<String, Set<Program>> _interestingShows;
+    
+    /**
+     * Set of programs to record. 
+     */
+    private Set<Program> _showsToRecord;
+   
+    /**
+     * Map or recording result to a set of programs. 
+     */
+    private EnumMap<RecordingResult, Set<Program>> _recordings;
+    
+    /**
+     * Constructs the program action executor. 
+     *
+     */
+    public ProgramActionExecutor() { 
+        _interestingShows = new TreeMap<String,Set<Program>>();
+        _showsToRecord = new TreeSet<Program>(new Program.TimeSorter());
+        _recordings = new EnumMap<RecordingResult, Set<Program>>(
+                RecordingResult.class);
+        for (RecordingResult result : RecordingResult.values()) {
+            _recordings.put(result, new TreeSet<Program>(new Program.TimeSorter()));
+        }
+    }
+    
+    /**
+     * Called by an action to indicate the desire to record a program.
+     * @param aPriority Priority of the program. Used to resolve conflicts.  
+     * @param aProgram Program to record. 
+     */
+    public void recordProgram(int aPriority, Program aProgram) { 
+        _showsToRecord.add(aProgram);
+    }
+    
+    /**
+     * Called by an action to indicate that a program is interesting. 
+     * @param aCategory Category of the program. 
+     * @param aProgram Program. 
+     */
+    public void interestingProgram(String aCategory, Program aProgram) { 
+        Set<Program> programs = _interestingShows.get(aCategory);   
+        if ( programs == null ) { 
+            programs = new TreeSet<Program>(new Program.TimeSorter());
+            _interestingShows.put(aCategory, programs);
+        }
+        programs.add(aProgram);
+    }
+    
+    /**
+     * Makes sure that the actions are performed.
+     *
+     */
+    public void commit() { 
+        for (Program program: _showsToRecord) { 
+            RecordingResult result = program.record(); 
+            _recordings.get(result).add(program);
+        }
+    }
+    
+    /**
+     * Gets the report describing what was done. 
+     * @return Report. 
+     */
+    public String getReport() {
+        StringBuffer msg = new StringBuffer("Summary of KiSS crawler: \n\n\n");
+        
+        boolean printed = false; 
+
+        for (RecordingResult result : RecordingResult.values()) {
+            if (_recordings.get(result).size() > 0) {
+                msg.append(result.getDescription() + "\n\n");
+                for (Program program : _recordings.get(result)) {
+                    msg.append(program + "\n\n");
+                    printed = true; 
+                }
+            }
+        }
+        
+        if ( _interestingShows.size() > 0 ) { 
+            msg.append("Possibly interesting shows:\n\n");
+            for (String category: _interestingShows.keySet()) { 
+                if ( category.length() > 0 ) { 
+                    msg.append("Category: " + category + "\n\n");
+                }
+                for (Program program: _interestingShows.get(category)) { 
+                    msg.append(program + "\n\n");
+                    printed = true; 
+                }
+            }
+        
+        }
+        if (!printed) {
+            msg.append("No suitable programs found");
+        }
+
+        return msg.toString(); 
+    }
+}
index 2c05b827233a82888c34be1452ef07e8ed5ff269..5d9f578d6e596d2912de5149017fcd6600b61588 100644 (file)
@@ -29,7 +29,6 @@ import org.dom4j.io.SAXReader;
 import org.wamblee.conditions.AndCondition;
 import org.wamblee.conditions.Condition;
 import org.wamblee.conditions.PropertyRegexCondition;
-import org.wamblee.crawler.kiss.ProgramFilter.ProgramAction;
 
 /**
  * Parse the configuration of desired programs.
@@ -63,10 +62,10 @@ class ProgramConfigurationParser {
                 Element program = (Element) i.next();
 
                 Element actionElem = program.element(ELEM_ACTION);
-                ProgramAction action = ProgramAction.RECORD;
+                ProgramAction action = new RecordProgramAction();
                 if (actionElem != null) {
                     if (actionElem.getText().equals(ACTION_NOTIFY)) {
-                        action = ProgramAction.NOTIFY;
+                        action = new InterestingProgramAction("");
                     }
                 }
                 List<Condition<Program>> regexConditions = 
index 3f65847b134ed7e8c565a53a3b85be3c0338ea94..5f66e0bd49c64382bf9d0cacda6381bd00e6778a 100644 (file)
@@ -27,8 +27,6 @@ import org.wamblee.conditions.Condition;
  */
 public class ProgramFilter {
     
-    public enum ProgramAction { RECORD, NOTIFY }; 
-
     private Condition<Program> _condition; 
     
     private ProgramAction _action; 
diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramNameMatcher.java b/crawler/kiss/src/org/wamblee/crawler/kiss/ProgramNameMatcher.java
deleted file mode 100644 (file)
index 867d5b3..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Copyright 2005 the original author or authors.
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *      http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */ 
-
-package org.wamblee.crawler.kiss;
-
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.wamblee.conditions.Condition;
-
-/**
- * Match programs based on a regular expression for the name in lower case. 
- */
-public class ProgramNameMatcher implements Condition<Program> {
-    
-    /**
-     * Pattern which describes interesting programs. 
-     */
-    private Pattern _pattern; 
-    
-    /**
-     * Constructs the matcher. 
-     * @param aPattern Pattern that describes interesting programs. 
-     */
-    public ProgramNameMatcher(String aPattern) { 
-        _pattern = Pattern.compile(aPattern);
-    }
-
-    /**
-     * Determines if the program name matches.
-     * @param aProgram Program. 
-     * @return True iff the program name matches. 
-     */
-    public boolean matches(Program aProgram) {
-        Matcher matcher = _pattern.matcher(aProgram.getName().toLowerCase());
-        return matcher.matches();
-    }
-}
diff --git a/crawler/kiss/src/org/wamblee/crawler/kiss/RecordProgramAction.java b/crawler/kiss/src/org/wamblee/crawler/kiss/RecordProgramAction.java
new file mode 100644 (file)
index 0000000..ac784f6
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2005 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+package org.wamblee.crawler.kiss;
+
+import org.wamblee.crawler.kiss.Program.RecordingResult;
+
+/**
+ * Represents an action to record a program. 
+ */
+public class RecordProgramAction implements ProgramAction {
+    
+    private int _priority; 
+    
+    /**
+     * Constructs the action. 
+     *
+     */
+    public void ReportProgramAction(int aPriority) { 
+        _priority = aPriority; 
+    }
+
+    /* (non-Javadoc)
+     * @see org.wamblee.crawler.kiss.ProgramAction#execute(org.wamblee.crawler.kiss.Program, org.wamblee.crawler.kiss.Report)
+     */
+    public void execute(Program aProgram, ProgramActionExecutor aReport) { 
+        aReport.recordProgram(_priority, aProgram);
+    }
+
+}