new fetchedfeed class

This commit is contained in:
Athou
2013-04-25 12:30:21 +02:00
parent 3cbdbcc6f7
commit effa009241
6 changed files with 81 additions and 62 deletions

View File

@@ -28,11 +28,11 @@ public class FeedFetcher {
@Inject
HttpGetter getter;
public Feed fetch(String feedUrl, boolean extractFeedUrlFromHtml,
public FetchedFeed fetch(String feedUrl, boolean extractFeedUrlFromHtml,
String lastModified, String eTag) throws FeedException,
ClientProtocolException, IOException, NotModifiedException {
log.debug("Fetching feed {}", feedUrl);
Feed feed = null;
FetchedFeed fetchedFeed = null;
HttpResult result = getter.getBinary(feedUrl, lastModified, eTag);
if (extractFeedUrlFromHtml) {
@@ -43,12 +43,12 @@ public class FeedFetcher {
feedUrl = extractedUrl;
}
}
feed = parser.parse(feedUrl, result.getContent());
fetchedFeed = parser.parse(feedUrl, result.getContent());
Feed feed = fetchedFeed.getFeed();
feed.setLastModifiedHeader(result.getLastModifiedSince());
feed.setEtagHeader(result.geteTag());
feed.setFetchDuration(result.getDuration());
return feed;
fetchedFeed.setFetchDuration(result.getDuration());
return fetchedFeed;
}
private String extractFeedUrl(String html, String baseUri) {

View File

@@ -2,6 +2,7 @@ package com.commafeed.backend.feeds;
import java.io.StringReader;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
@@ -35,8 +36,10 @@ public class FeedParser {
};
@SuppressWarnings("unchecked")
public Feed parse(String feedUrl, byte[] xml) throws FeedException {
Feed feed = new Feed();
public FetchedFeed parse(String feedUrl, byte[] xml) throws FeedException {
FetchedFeed fetchedFeed = new FetchedFeed();
Feed feed = fetchedFeed.getFeed();
Collection<FeedEntry> entries = fetchedFeed.getEntries();
feed.setLastUpdated(Calendar.getInstance().getTime());
try {
@@ -47,8 +50,8 @@ public class FeedParser {
InputSource source = new InputSource(new StringReader(xmlString));
SyndFeed rss = new SyndFeedInput().build(source);
fetchedFeed.setTitle(rss.getTitle());
feed.setUrl(feedUrl);
feed.setTitle(rss.getTitle());
feed.setLink(rss.getLink());
List<SyndEntry> items = rss.getEntries();
for (SyndEntry item : items) {
@@ -69,21 +72,21 @@ public class FeedParser {
}
entry.setContent(content);
feed.getEntries().add(entry);
entries.add(entry);
}
Date publishedDate = validateDate(rss.getPublishedDate());
if (publishedDate == null && !feed.getEntries().isEmpty()) {
FeedEntry first = feed.getEntries().iterator().next();
FeedEntry first = entries.iterator().next();
publishedDate = first.getUpdated();
}
feed.setPublishedDate(publishedDate);
fetchedFeed.setPublishedDate(publishedDate);
} catch (Exception e) {
throw new FeedException(String.format(
"Could not parse feed from %s : %s", feedUrl,
e.getMessage()), e);
}
return feed;
return fetchedFeed;
}
private Date getUpdateDate(SyndEntry item) {

View File

@@ -84,7 +84,7 @@ public class FeedRefreshWorker {
int errorCount = 0;
Date disabledUntil = null;
Feed fetchedFeed = null;
FetchedFeed fetchedFeed = null;
boolean modified = true;
try {
fetchedFeed = fetcher.fetch(feed.getUrl(), false,
@@ -127,9 +127,10 @@ public class FeedRefreshWorker {
Collection<FeedEntry> entries = null;
if (fetchedFeed != null) {
feed.setLink(fetchedFeed.getLink());
feed.setLastModifiedHeader(fetchedFeed.getLastModifiedHeader());
feed.setEtagHeader(fetchedFeed.getEtagHeader());
feed.setLink(fetchedFeed.getFeed().getLink());
feed.setLastModifiedHeader(fetchedFeed.getFeed()
.getLastModifiedHeader());
feed.setEtagHeader(fetchedFeed.getFeed().getEtagHeader());
entries = fetchedFeed.getEntries();
}
FeedRefreshTask task = new FeedRefreshTask(feed, entries);

View File

@@ -0,0 +1,59 @@
package com.commafeed.backend.feeds;
import java.util.Collection;
import java.util.Date;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry;
import com.google.api.client.util.Lists;
public class FetchedFeed {
private Feed feed = new Feed();
private Collection<FeedEntry> entries = Lists.newArrayList();
private String title;
private long fetchDuration;
private Date publishedDate;
public Feed getFeed() {
return feed;
}
public void setFeed(Feed feed) {
this.feed = feed;
}
public Collection<FeedEntry> getEntries() {
return entries;
}
public void setEntries(Collection<FeedEntry> entries) {
this.entries = entries;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public long getFetchDuration() {
return fetchDuration;
}
public void setFetchDuration(long fetchDuration) {
this.fetchDuration = fetchDuration;
}
public Date getPublishedDate() {
return publishedDate;
}
public void setPublishedDate(Date publishedDate) {
this.publishedDate = publishedDate;
}
}

View File

@@ -10,7 +10,6 @@ import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.hibernate.annotations.Index;
@@ -31,25 +30,6 @@ public class Feed extends AbstractModel {
@Index(name = "urlHash_index")
private String urlHash;
/**
* title of the feed, used only when fetching, not stored
*/
@Transient
private String title;
/**
* time it took to fetch the feed, used only when fetching, not stored
*/
@Transient
private long fetchDuration;
/**
* extracted published date from the feed, used only when fetching, not
* stored
*/
@Transient
private Date publishedDate;
/**
* The url of the website, extracted from the feed
*/
@@ -138,14 +118,6 @@ public class Feed extends AbstractModel {
this.subscriptions = subscriptions;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
@@ -194,22 +166,6 @@ public class Feed extends AbstractModel {
this.etagHeader = etagHeader;
}
public long getFetchDuration() {
return fetchDuration;
}
public void setFetchDuration(long fetchDuration) {
this.fetchDuration = fetchDuration;
}
public Date getPublishedDate() {
return publishedDate;
}
public void setPublishedDate(Date publishedDate) {
this.publishedDate = publishedDate;
}
public Date getLastUpdateSuccess() {
return lastUpdateSuccess;
}

View File

@@ -88,7 +88,7 @@ public class FeedREST extends AbstractResourceREST {
url = prependHttp(url);
Feed feed = null;
try {
feed = feedFetcher.fetch(url, true, null, null);
feed = feedFetcher.fetch(url, true, null, null).getFeed();
} catch (Exception e) {
throw new WebApplicationException(e, Response
.status(Status.INTERNAL_SERVER_ERROR)