(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 19 Mar 2006 23:00:33 +0000 (23:00 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sun, 19 Mar 2006 23:00:33 +0000 (23:00 +0000)
.classpath
build/header.xml
crawler/kiss/build.xml
crawler/kiss/conf/kiss/programs.xml
crawler/kiss/src/org/wamblee/crawler/kiss/KissCrawler.java
crawler/kiss/src/org/wamblee/crawler/kiss/Program.java
crawler/kiss/src/org/wamblee/crawler/kiss/ProgramActionExecutor.java

index 751d4442436a23be9b4c3849f2d699d2dec14cdc..1cec05af5c6038828cf942ff837ab146d6ebf6cf 100644 (file)
@@ -53,5 +53,6 @@
        <classpathentry kind="lib" path="crawler/kiss/lib/external/activation.jar"/>
        <classpathentry kind="lib" path="crawler/kiss/lib/external/mail.jar"/>
        <classpathentry kind="lib" path="crawler/kiss/lib/external/xerces-2.4.0.jar"/>
+       <classpathentry kind="lib" path="crawler/kiss/lib/external/commons-email-1.0.jar"/>
        <classpathentry kind="output" path="support/testbin"/>
 </classpath>
index 196a32b4046e2158b73116b005b13cbbbf54b0eb..689d5ca738029a9dafaee5a7b0cb3b63d16d9a03 100644 (file)
   </antcall>
 </target>
 
+<target name="commons-email.d">
+  <antcall target="download.dep">
+    <param name="group" value="commons-email"/>
+    <param name="version" value="1.0"/>
+  </antcall>
+</target>
+
+
+
 
 <target name="logging.d" depends="log4j.d,commons-logging.d">
 </target>
index ed756b4b05c29568fa310de4a240e0fd80745e01..f7b685a8fa9dea61d462148d53e8cc2f85522baa 100644 (file)
@@ -17,7 +17,7 @@
    &header;
        
        <target name="module.build.deps" 
-         depends="logging.d,mail.d,commons-beanutils.d,commons-codec.d,dom4j.d,xerces.d,httpclient.d,jtidy.d,wamblee.support.d,wamblee.crawler.d">
+         depends="logging.d,mail.d,commons-email.d,commons-beanutils.d,commons-codec.d,dom4j.d,xerces.d,httpclient.d,jtidy.d,wamblee.support.d,wamblee.crawler.d">
        </target>
        
        <!-- Set libraries to use in addition for test, a library which 
index c0bf4e835bf6f9a6e1c12df0138c1031d9706fe4..fe16c08b671019576a5e4133200bd62c88927dee 100644 (file)
@@ -1,4 +1,8 @@
 <programs>
+  
+  <program>
+    <match>wintertijd</match>
+  </program>
  
   <program>
     <category>horror</category>
index 29d7c84940717d76711593e43c0e88272f011144..0549e48f3a8a99910512c8ca125f574a021d027c 100644 (file)
@@ -16,6 +16,7 @@
 
 package org.wamblee.crawler.kiss;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -24,6 +25,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
 import java.util.Properties;
@@ -36,10 +38,16 @@ import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
+import javax.xml.transform.TransformerException;
 
 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.commons.mail.EmailException;
+import org.apache.commons.mail.HtmlEmail;
+import org.apache.xml.serialize.OutputFormat;
+import org.apache.xml.serialize.XMLSerializer;
+import org.w3c.dom.Document;
 import org.wamblee.crawler.Action;
 import org.wamblee.crawler.Configuration;
 import org.wamblee.crawler.Crawler;
@@ -47,6 +55,8 @@ 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.io.FileResource;
+import org.wamblee.xml.XSLT;
 
 /**
  * The KiSS crawler for automatic recording of interesting TV shows.
@@ -160,15 +170,13 @@ public class KissCrawler {
         ProgramActionExecutor executor = new ProgramActionExecutor();
         for (ProgramFilter filter : aProgramCondition) {
             List<Program> programs = filter.apply(aGuide);
-            ProgramAction action = filter.getAction(); 
-            for (Program program: programs) { 
+            ProgramAction action = filter.getAction();
+            for (Program program : programs) {
                 action.execute(program, executor);
             }
         }
         executor.commit();
-        String msg = executor.getReport();
-        System.out.println(msg);
-        sendMail(msg);
+        sendMail(executor);
     }
 
     /**
@@ -274,8 +282,8 @@ public class KissCrawler {
                         keywords = programInfo.getContent().element("keywords")
                                 .getText().trim();
                     } catch (PageException e) {
-                        LOG
-                                .warn("Program details could not be determined for '"
+                        LOG.warn(
+                                "Program details could not be determined for '"
                                         + action.getName() + "'", e);
                     }
                 }
@@ -297,22 +305,42 @@ public class KissCrawler {
      * @throws MessagingException
      *             In case of problems sending mail.
      */
-    private void sendMail(String aText) throws MessagingException {
+    private void sendMail(ProgramActionExecutor aExecutor) throws MessagingException {
+        String textReport = aExecutor.getReport();
+        System.out.println("Text report: \n" + textReport);
+        System.out.println("XML report:\n" + aExecutor.getXmlReport().asXML());
+        
+        
         Properties props = new Properties();
         props.put("mail.transport.protocol", "smtp");
         props.put("mail.smtp.host", "falcon");
         props.put("mail.smtp.port", "25");
 
         Session mailSession = Session.getInstance(props);
-        Message message = new MimeMessage(mailSession);
-
-        message.setFrom(new InternetAddress("erik@brakkee.org"));
-        message.setRecipient(Message.RecipientType.TO, new InternetAddress(
-                "erik@brakkee.org"));
-        message.setSentDate(new Date());
-        message.setSubject("KiSS crawler update");
-        message.setText(aText);
-        Transport.send(message);
+        InternetAddress from = new InternetAddress("erik@brakkee.org");
+    
+        HtmlEmail mail = new HtmlEmail();
+        mail.setMailSession(mailSession);
+        try {
+            mail.setFrom("erik@brakkee.org");
+            mail.setTo(Arrays.asList(new InternetAddress[] { from }));
+            mail.setSentDate(new Date());
+            mail.setSubject("KiSS Crawler Update");
+            String html = aExecutor.getXmlReport().asXML(); 
+            Document document = new XSLT().transform(html.getBytes(), new FileResource(new File("reportToHtml.xsl")));
+            ByteArrayOutputStream xhtml = new ByteArrayOutputStream();
+            XMLSerializer serializer = new XMLSerializer(xhtml, new OutputFormat());
+            serializer.serialize(document);
+            mail.setHtmlMsg(xhtml.toString());
+            mail.setTextMsg(textReport);
+            mail.send();
+        } catch (EmailException e) {
+            throw new RuntimeException(e);
+        } catch (TransformerException e) { 
+            throw new RuntimeException(e);
+        } catch (IOException e) { 
+            throw new RuntimeException(e);
+        }
     }
 
 }
index 89c3c88ce1704612eda0dc5044abac95474f6349..c80fea246badc9c8cfe19c2e9b0addb78e1ea392 100644 (file)
@@ -20,6 +20,8 @@ import java.util.Comparator;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
 import org.wamblee.crawler.Action;
 import org.wamblee.crawler.Page;
 import org.wamblee.crawler.PageException;
@@ -263,4 +265,21 @@ public class Program {
         return getName().equals(program.getName()) && 
                _programInfo.equals(program._programInfo);
     }
+    
+    /**
+     * Converts program information to XML. 
+     * @return XML representation of program information.  
+     */
+    public Element asXml() { 
+        DocumentFactory factory = DocumentFactory.getInstance();
+        Element program = factory.createElement("program"); 
+        program.addElement("name").setText(getName());
+        program.addElement("description").setText(getDescription());
+        program.addElement("keywords").setText(getKeywords());
+        program.addElement("channel").setText(getChannel());
+        Element interval = program.addElement("interval");
+        interval.addElement("begin").setText(getInterval().getBegin().toString());
+        interval.addElement("end").setText(getInterval().getEnd().toString());
+        return program; 
+    }
 }
index 8303449e5544799b5f7146f969054fcc2145340f..87d417bee644f0fe5d425603473694f9ae0a8448 100644 (file)
@@ -22,6 +22,8 @@ import java.util.Set;
 import java.util.TreeMap;
 import java.util.TreeSet;
 
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
 import org.wamblee.crawler.kiss.Program.RecordingResult;
 
 /**
@@ -134,4 +136,41 @@ public class ProgramActionExecutor {
 
         return msg.toString(); 
     }
+    
+    /**
+     * Get report as XML. 
+     * @return XML report 
+     */
+    public Element getXmlReport() { 
+        DocumentFactory factory = DocumentFactory.getInstance(); 
+        Element report = factory.createElement("report"); 
+        
+        for (RecordingResult result : RecordingResult.values()) {
+            if (_recordings.get(result).size() > 0) {
+                Element recordingResult = report.addElement("recorded").addAttribute("result", result.toString());
+         
+                for (Program program : _recordings.get(result)) {
+                    recordingResult.add(program.asXml()); 
+                }
+            }
+        }
+        
+     
+        if ( _interestingShows.size() > 0 ) { 
+            Element interesting = report.addElement("interesting");
+            for (String category: _interestingShows.keySet()) { 
+                Element categoryElem = interesting; 
+                if ( category.length() > 0 ) { 
+                    categoryElem = interesting.addElement("category");
+                    categoryElem.addAttribute("name", category);
+                }
+                for (Program program: _interestingShows.get(category)) {
+                    categoryElem.add(program.asXml()); 
+                }
+            }
+        
+        }
+        return report; 
+    }
 }