package eu.socialsensor.framework.retrievers.newsfeed.rss;

import java.io.IOException;
import java.net.URL;
import java.util.List;


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

import eu.socialsensor.framework.abstractions.newsfeed.rss.RSSItem;
import eu.socialsensor.framework.common.domain.Feed;
import eu.socialsensor.framework.common.domain.feeds.URLFeed;
import eu.socialsensor.framework.retrievers.Retriever;
import eu.socialsensor.framework.streams.newsfeed.rss.RSSStream;


public class RSSRetriever implements Retriever{
	
	RSSStream rssStream;
	
	long oneMonthPeriod = 2592000000L;
	
	public RSSRetriever(RSSStream rssStream){
		
		this.rssStream = rssStream;
	}
	
	@Override
	public Integer retrieve(Feed feed){
		URLFeed ufeed = (URLFeed) feed;
		Integer totalRetrievedItems = 0;
		try {
			
			URL url  = new URL(ufeed.getURL());
			if(ufeed.getURL().equals(""))
				return totalRetrievedItems;
			
		    XmlReader reader = new XmlReader(url);
		    
		    try{
		    	SyndFeed rssData = new SyndFeedInput().build(reader);
		    	@SuppressWarnings("unchecked")
				List<SyndEntry> rssEntries = rssData.getEntries();
				
				for (SyndEntry rss:rssEntries){
					
					if(rss.getLink() != null){
						
						if(rss.getPublishedDate() != null && rss.getPublishedDate().getTime()>0 
								&& Math.abs(System.currentTimeMillis() - rss.getPublishedDate().getTime())<oneMonthPeriod){
							
							RSSItem rssItem = new RSSItem(rss);
							
							rssStream.store(rssItem);
							
							totalRetrievedItems++;
						}
					
					}
					
					
				}
				
				
		    }catch(NullPointerException e2){
		    	return totalRetrievedItems;
		    }
		    
		} catch (IOException e) {
			// TODO Auto-generated catch block
			return 0;
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			return 0;
		} catch (FeedException e) {
			// TODO Auto-generated catch block
			return 0;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return 0;
		}
		return totalRetrievedItems;
	}

	
	@Override
	public void stop(){
	
	}
	

	
}

@@ -106,16 +106,40 @@
 t.URL;%0D%0A
+import java.util.Date;%0D%0A
 import j
@@ -154,16 +154,16 @@
 .List;%0D%0A
-
 %0D%0A%0D%0Aimpo
@@ -957,16 +957,20 @@
 feed)%7B%0D%0A
+%09%09%0D%0A
 %09%09URLFee
@@ -996,16 +996,99 @@
  feed;%0D%0A
+%09%09System.out.println(%22%5B%22+new Date()+%22%5DRetrieving RSS Feed: %22+ufeed.getURL());%0D%0A%09%09%0D%0A
 %09%09Intege
@@ -1785,33 +1785,24 @@
 Item(rss);%0D%0A
-%09%09%09%09%09%09%09%0D%0A
 %09%09%09%09%09%09%09rssSt
@@ -1823,23 +1823,41 @@
 Item);%0D%0A
-
 %09%09%09%09%09%09%09
+Thread.sleep(100);
 %0D%0A%09%09%09%09%09%09

add sleep while storing in rss retriever