package jnode.rssposter.jscript;

import java.net.URL;
import java.util.Date;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

import jnode.dto.Echoarea;
import jnode.ftn.FtnTools;
import jnode.jscript.IJscriptHelper;
import jnode.logger.Logger;

/**
 * Постит RSS куда надо
 * 
 * @author kreon
 * 
 */
public class RssPosterHelper extends IJscriptHelper {
	private static final Logger logger = Logger
			.getLogger(RssPosterHelper.class);

	@Override
	public Version getVersion() {
		return new Version() {

			@Override
			public int getMinor() {
				return 2;
			}

			@Override
			public int getMajor() {
				return 1;
			}
		};
	}

	/**
	 * Запостить новости за последние сутки
	 */
	public void postNewsToEchoarea(String title, String echoarea, String URL,
			int daysBefore) {
        postNewsToEchoareaInHours(title, echoarea, URL, daysBefore * 24);
	}

    /**
     * Запостить новости за последний час
     */
    public void postNewsToEchoareaInHours(String title, String echoarea, String URL,
                                   int hoursBefore) {
        Long date = new Date().getTime() - 3600000 * hoursBefore;
        postNewsToEchoareaSinceX(title, echoarea, URL, new Date(date));
    }

    /**
     * Дата, смещенная относительно текущей на shiftInSec секунд
     * @param shiftInSec смещение в секундах (с минусом - в прошлое, с плюсом - в будущее)
     * @return смещенная дата
     */
    public Date shiftedDate(int shiftInSec){
       return new Date(new Date().getTime() + 1000 * shiftInSec);
    }

	public void postNewsToEchoareaSinceX(String title, String echoarea,
			String URL, Date x) {
		Echoarea area = FtnTools.getAreaByName(echoarea, null);
		if (area == null) {
			logger.l4("No such echoarea - " + echoarea);
			return;
		}
        StringBuilder sb = getText(URL, x);
        if (sb == null) return;
        if (sb.length() != 0){
            FtnTools.writeEchomail(area, title, sb.toString());
        }
	}

    private StringBuilder getText(String URL, Date x) {
        SyndFeedInput feedInput = new SyndFeedInput();
        StringBuilder sb = new StringBuilder();

        try {
            SyndFeed feed = feedInput.build(new XmlReader(new java.net.URL(URL)));
            if (feed.getPublishedDate() != null && x.after(feed.getPublishedDate())) {
                logger.l4("There's no new entries at " + URL);
                return null;
            }

            for (Object object : feed.getEntries()) {
                SyndEntry entry = (SyndEntry) object;
                if (x.after(entry.getPublishedDate())) {
                    break;
                }
                sb.append("*");
                sb.append(entry.getTitle());
                sb.append("*");
                sb.append("\n\n");
                sb.append(entry.getDescription().getValue()
                        .replaceAll("\\<.*?>", ""));
                sb.append("\nRead more: ");
                sb.append(entry.getUri());
                sb.append("\n----------------------------------------------------------------------\n");
            }
        } catch (Exception e) {
            logger.l2("Some error happens while parsing " + URL, e);
        }
        return sb;
    }
}

@@ -48,16 +48,48 @@
 et.URL;%0A
+import java.text.MessageFormat;%0A
 import j
@@ -1759,27 +1759,27 @@
 ,%0A%09%09%09String 
-URL
+url
 , Date x) %7B%0A
@@ -1778,16 +1778,180 @@
 te x) %7B%0A
+%0A        logger.l5(MessageFormat.format(%22postNewsToEchoareaSinceX title = %7B0%7D, echoarea = %7B1%7D, url = %7B2%7D, dateX = %7B3%7D%22,%0A                title, echoarea, url, x));%0A%0A
 %09%09Echoar
@@ -2120,19 +2120,19 @@
 getText(
-URL
+url
 , x);%0A  
@@ -2148,37 +2148,120 @@
 (sb 
-=
+!
 = null
-) return;%0A        if (
+ && sb.length() != 0)%7B%0A            logger.l5(MessageFormat.format(%22write in area %7B0%7D %7B1%7D entries%22, area, 
 sb.l
@@ -2259,39 +2259,35 @@
 rea, sb.length()
- != 0)%7B
+));
 %0A            Ftn
@@ -2796,24 +2796,25 @@
          %7D%0A%0A
+%0A
             
@@ -2958,32 +2958,155 @@
 ishedDate())) %7B%0A
+                    logger.l5(MessageFormat.format(%22x = %7B0%7D, published date = %7B1%7D - break%22, x, entry.getPublishedDate()));%0A
                 

improve logging in RssPosterHelper