-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFeedParser.java
More file actions
139 lines (122 loc) · 3.75 KB
/
Copy pathFeedParser.java
File metadata and controls
139 lines (122 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.apache.commons.lang.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Parses feed items that have a title, link, and description
* @author Gurch
*
*/
public class FeedParser {
private static final int DEFAULT_ENTRIES_PER_FEED = 8;
private static final String FEED_TABLE = "rss.multi.list";
private static final String XPATH_ITEM = "//item";
private static final String XPATH_FEED = "//feed";
private YQLClient client;
public FeedParser(){
client = new YQLClient();
}
public List<RSSItem> getItems(Collection<String> feedUrls) throws Exception{
return getItemBatch(feedUrls);
}
private List<RSSItem> getItemBatch(Collection<String> feedUrls) throws Exception{
List<RSSItem> feeds = new ArrayList<RSSItem>(feedUrls.size()*DEFAULT_ENTRIES_PER_FEED);
String query = buildQuery(feedUrls);
try {
Document results = client.execute(query);
NodeList items = getItems(results);
for(int i = 0; i < items.getLength(); i++){
Node itemNode = items.item(i);
Node parent = itemNode.getParentNode();
String title = StringUtils.trim(parent.getFirstChild().getTextContent());
RSSItem item = getItem(items.item(i));
if(item != null){
item.setSource(title);
feeds.add(item);
}
}
} catch (YQLException e) {
e.printStackTrace();
}
return feeds;
}
private NodeList getItems(Document doc) throws Exception{
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(XPATH_ITEM);
return (NodeList)expr.evaluate(doc, XPathConstants.NODESET);
}
private RSSItem getItem(Node item){
NodeList children = item.getChildNodes();
if(children.getLength() >= 3){
String title = null;
String url = null;
String desc = null;
for(int i = 0; i < children.getLength(); i++){
String nodeName = children.item(i).getNodeName();
String nodeValue = children.item(i).getTextContent();
if(nodeName == null)
continue;
if(nodeValue == null || nodeValue.equals(" ")){
continue;
}
if(nodeName.equals("title")){
title = nodeValue;
}
else if(nodeName.equals("link")){
url = nodeValue;
}
else if(nodeName.equals("description")){
desc = nodeValue;
}
}
if(title == null||url == null||desc == null)
return null;
Html2Text html = new Html2Text();
try{
html.parse(new StringReader(desc));
desc = html.getText();
html.parse(new StringReader(title));
title = html.getText();
}catch(Exception e){
return null;
}
return new RSSItem(title, desc, url);
}
return null;
}
private String buildQuery(Collection<String> FeedUrls){
String urls = StringUtils.join(FeedUrls, "\',\'");
urls = String.format("\'%s\'", urls);
return "select * from "+FEED_TABLE+" where feeds=\""+urls+"\"";
}
private final class Html2Text extends HTMLEditorKit.ParserCallback {
StringBuffer s;
public Html2Text() {}
public void parse(Reader in) throws IOException {
s = new StringBuffer();
ParserDelegator delegator = new ParserDelegator();
// the third parameter is TRUE to ignore charset directive
delegator.parse(in, this, Boolean.TRUE);
}
public void handleText(char[] text, int pos) {
s.append(text);
}
public String getText() {
return s.toString();
}
}
}