package org.wamblee.crawler.kiss;
+import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;
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;
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.
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);
}
/**
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);
}
}
* @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);
+ }
}
}
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;
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;
+ }
}
import java.util.TreeMap;
import java.util.TreeSet;
+import org.dom4j.DocumentFactory;
+import org.dom4j.Element;
import org.wamblee.crawler.kiss.Program.RecordingResult;
/**
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;
+ }
}