2013-03-20 20:33:42 +01:00
|
|
|
package com.commafeed.backend.feeds;
|
|
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
2013-03-31 09:53:19 +02:00
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
|
import org.jsoup.select.Elements;
|
2013-03-20 20:33:42 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2013-04-03 15:53:57 +02:00
|
|
|
import com.commafeed.backend.HttpGetter;
|
2013-03-23 16:17:19 +01:00
|
|
|
import com.commafeed.backend.model.Feed;
|
2013-03-27 09:37:15 +01:00
|
|
|
import com.sun.syndication.io.FeedException;
|
2013-03-20 20:33:42 +01:00
|
|
|
|
|
|
|
|
public class FeedFetcher {
|
|
|
|
|
|
|
|
|
|
private static Logger log = LoggerFactory.getLogger(FeedFetcher.class);
|
|
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
|
FeedParser parser;
|
|
|
|
|
|
2013-04-03 15:53:57 +02:00
|
|
|
@Inject
|
|
|
|
|
HttpGetter getter;
|
|
|
|
|
|
2013-03-27 09:37:15 +01:00
|
|
|
public Feed fetch(String feedUrl) throws FeedException {
|
2013-03-20 20:33:42 +01:00
|
|
|
log.debug("Fetching feed {}", feedUrl);
|
|
|
|
|
Feed feed = null;
|
|
|
|
|
|
|
|
|
|
try {
|
2013-04-05 22:38:35 +02:00
|
|
|
byte[] content = getter.getBinary(feedUrl);
|
|
|
|
|
String extractedUrl = extractFeedUrl(new String(content, "UTF-8"));
|
2013-03-31 18:47:17 +02:00
|
|
|
if (extractedUrl != null) {
|
2013-04-05 22:38:35 +02:00
|
|
|
content = getter.getBinary(extractedUrl);
|
2013-04-01 09:16:24 +02:00
|
|
|
feedUrl = extractedUrl;
|
2013-03-31 18:47:17 +02:00
|
|
|
}
|
2013-03-31 14:56:35 +02:00
|
|
|
feed = parser.parse(feedUrl, content);
|
2013-03-20 20:33:42 +01:00
|
|
|
} catch (Exception e) {
|
2013-03-27 09:37:15 +01:00
|
|
|
throw new FeedException(e.getMessage(), e);
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
2013-03-27 09:37:15 +01:00
|
|
|
return feed;
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|
|
|
|
|
|
2013-03-31 18:47:17 +02:00
|
|
|
private String extractFeedUrl(String html) {
|
2013-03-31 14:30:44 +02:00
|
|
|
String foundUrl = null;
|
2013-03-31 09:53:19 +02:00
|
|
|
|
2013-03-31 14:30:44 +02:00
|
|
|
Document doc = Jsoup.parse(html);
|
|
|
|
|
String root = doc.children().get(0).tagName();
|
|
|
|
|
if ("html".equals(root)) {
|
|
|
|
|
Elements rss = doc.select("link[type=application/rss+xml]");
|
|
|
|
|
Elements atom = doc.select("link[type=application/atom+xml]");
|
|
|
|
|
if (!rss.isEmpty()) {
|
|
|
|
|
foundUrl = rss.get(0).attr("abs:href").toString();
|
|
|
|
|
} else if (!atom.isEmpty()) {
|
|
|
|
|
foundUrl = atom.get(0).attr("abs:href").toString();
|
|
|
|
|
}
|
2013-03-31 09:53:19 +02:00
|
|
|
}
|
2013-03-31 14:30:44 +02:00
|
|
|
return foundUrl;
|
2013-03-31 09:53:19 +02:00
|
|
|
}
|
2013-03-20 20:33:42 +01:00
|
|
|
}
|