Files
Athou_commafeed/src/main/java/com/commafeed/backend/feeds/FeedParser.java

128 lines
4.0 KiB
Java
Raw Normal View History

2013-03-20 20:33:42 +01:00
package com.commafeed.backend.feeds;
import java.io.ByteArrayInputStream;
2013-03-20 20:33:42 +01:00
import java.util.Calendar;
import java.util.Date;
2013-03-20 20:33:42 +01:00
import java.util.List;
2013-04-14 18:28:48 +02:00
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.xml.sax.InputSource;
2013-03-23 16:17:19 +01:00
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedEntryContent;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
2013-04-09 13:37:00 +02:00
import com.google.common.collect.Iterables;
import com.sun.syndication.feed.synd.SyndContent;
2013-04-09 13:37:00 +02:00
import com.sun.syndication.feed.synd.SyndEnclosure;
2013-03-20 20:33:42 +01:00
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;
public class FeedParser {
private static final Date START = new Date(0);
private static final Date END = new Date(Integer.MAX_VALUE);
2013-04-14 18:51:12 +02:00
private static final Function<SyndContent, String> CONTENT_TO_STRING = new Function<SyndContent, String>() {
public String apply(SyndContent content) {
return content.getValue();
}
};
2013-03-20 20:33:42 +01:00
@SuppressWarnings("unchecked")
public Feed parse(String feedUrl, byte[] xml) throws FeedException {
2013-03-20 20:33:42 +01:00
Feed feed = new Feed();
feed.setLastUpdated(Calendar.getInstance().getTime());
try {
InputSource source = new InputSource(new ByteArrayInputStream(xml));
if (new String(ArrayUtils.subarray(xml, 0, 100))
.split(SystemUtils.LINE_SEPARATOR)[0].toUpperCase()
.contains("ISO-8859-1")) {
// they probably use word, we need to handle curly quotes and
// other word special characters
source.setEncoding("windows-1252");
}
SyndFeed rss = new SyndFeedInput().build(source);
feed.setUrl(feedUrl);
2013-03-30 20:51:51 +01:00
feed.setTitle(rss.getTitle());
2013-04-03 15:53:57 +02:00
feed.setLink(rss.getLink());
2013-03-20 20:33:42 +01:00
List<SyndEntry> items = rss.getEntries();
for (SyndEntry item : items) {
FeedEntry entry = new FeedEntry();
entry.setGuid(item.getUri());
2013-04-14 18:28:48 +02:00
entry.setGuidHash(DigestUtils.sha1Hex(item.getUri()));
2013-03-20 20:33:42 +01:00
entry.setUrl(item.getLink());
entry.setUpdated(validateDate(getUpdateDate(item)));
2013-03-20 20:33:42 +01:00
FeedEntryContent content = new FeedEntryContent();
2013-04-14 18:51:12 +02:00
content.setContent(getContent(item));
content.setTitle(item.getTitle());
2013-04-09 13:37:00 +02:00
SyndEnclosure enclosure = (SyndEnclosure) Iterables.getFirst(
item.getEnclosures(), null);
if (enclosure != null) {
content.setEnclosureUrl(enclosure.getUrl());
content.setEnclosureType(enclosure.getType());
2013-04-09 13:37:00 +02:00
}
entry.setContent(content);
2013-04-09 13:37:00 +02:00
2013-03-20 20:33:42 +01:00
feed.getEntries().add(entry);
}
Date publishedDate = validateDate(rss.getPublishedDate());
if (publishedDate == null && !feed.getEntries().isEmpty()) {
FeedEntry first = feed.getEntries().iterator().next();
publishedDate = first.getUpdated();
}
feed.setPublishedDate(publishedDate);
2013-03-20 20:33:42 +01:00
} catch (Exception e) {
2013-03-25 12:24:00 +01:00
throw new FeedException(String.format(
"Could not parse feed from %s : %s", feedUrl,
e.getMessage()), e);
2013-03-20 20:33:42 +01:00
}
return feed;
}
private Date getUpdateDate(SyndEntry item) {
Date date = item.getUpdatedDate();
if (date == null) {
date = item.getPublishedDate();
}
if (date == null) {
date = new Date();
}
return date;
}
private Date validateDate(Date date) {
if (date == null) {
2013-04-21 23:07:19 +02:00
return new Date();
}
if (date.before(START) || date.after(END)) {
2013-04-21 23:07:19 +02:00
return new Date();
}
return date;
}
@SuppressWarnings("unchecked")
private String getContent(SyndEntry item) {
String content = null;
if (item.getContents().isEmpty()) {
content = item.getDescription() == null ? null : item
.getDescription().getValue();
} else {
content = StringUtils.join(Collections2.transform(
2013-04-14 18:51:12 +02:00
item.getContents(), CONTENT_TO_STRING),
SystemUtils.LINE_SEPARATOR);
}
return content;
}
2013-03-23 09:13:04 +01:00
2013-03-20 20:33:42 +01:00
}