-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.js
More file actions
110 lines (99 loc) · 3.01 KB
/
model.js
File metadata and controls
110 lines (99 loc) · 3.01 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
Feeds = new Meteor.Collection("feeds");
Stories = new Meteor.Collection("stories");
Feeds.allow({
insert: function(userId,feed) {
return false;
},
update: function(){
return true;
},
remove: function(){
return true;
}
});
if(Meteor.is_server){
var require = __meteor_bootstrap__.require;
libxmljs = require("libxmljs");
// Changes XML to JSON
function xmlToJson(xml) {
// Create the return object
var obj = {};
if (xml.nodeType == 1) { // element
// do attributes
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
for (var j = 0; j < xml.attributes.length; j++) {
var attribute = xml.attributes.item(j);
obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType == 3) { // text
obj = xml.nodeValue;
}
// do children
if (xml.hasChildNodes()) {
for(var i = 0; i < xml.childNodes.length; i++) {
var item = xml.childNodes.item(i);
var nodeName = item.nodeName;
if (typeof(obj[nodeName]) == "undefined") {
obj[nodeName] = xmlToJson(item);
} else {
if (typeof(obj[nodeName].push) == "undefined") {
var old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
return obj;
};
Meteor.methods({
getFeed: function (options) {
this.unblock();
Meteor.http.get(options.url,function(error,res){
if(error) {
console.log(error);
}
if(options.url.indexOf("xml")>=0) {
xmlDoc = libxmljs.parseXmlString(res.content);
if(xmlDoc.find("//channel")[0]) items = xmlDoc.find("//channel")[0].childNodes()[0].find("//item");
else items = xmlDoc.find("//feed")[0].childNodes()[0].find("//entry");
// console.log(xmlToJson(items[0].childNodes()));
for(i=0;i<items.length;i++) {
data = {};
data.feed_id = options.feedId;
for(j=0;j<items[i].childNodes().length;j++) {
data[items[i].childNodes()[j].name()] = items[i].childNodes()[j].text();
}
matches = Stories.find({pubDate:data.pubDate,title:data.title});
if(matches.count()>0) return false;
storyid = Stories.insert(data);
Feeds.update({_id:feedId},{$addToSet: {stories: storyid}});
}
} else if(options.url.indexOf("json")>=0) {
}
return true;
});
},
addFeed: function(options){
this.unblock();
if(!(typeof options.url == "string") && options.url.length) return false;
Meteor.http.get(options.url,function(error,res){
if(error) {
console.log(error);
}
if(options.url.indexOf("xml")>=0) {
xmlDoc = libxmljs.parseXmlString(res.content);
if(xmlDoc.find("//channel")[0]) title = xmlDoc.find("//channel")[0].childNodes()[0].find("//title")[0].text();
else title = xmlDoc.find("//feed")[0].childNodes()[0].find("//title")[0].text();
} else if(options.url.indexOf("json")>=0) title = JSON.parse(res.content).metadata.title;
return Feeds.insert({
url: options.url,
title: title
});
});
}
});
};