-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSS.js
159 lines (125 loc) · 3.67 KB
/
RSS.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* utility Modules, FeedUtilities */
/* Magic Mirror
* Module: FeedUtilities
*
* By Neil Scott
* Inspired by many in the MMM ecosystem
* MIT Licensed.
*/
const htmlparser2 = require("htmlparser2");
const axios = require("axios");
var LOG = require('./LOG');
exports.RSSitem = function (id, title, description, pubdate, age, imageURL, source, categories) {
this.id = id; // use title as probably uniue and then we hashcode it
this.title = title;
this.description = description;
this.pubdate = pubdate;
this.age = age; //in microseconds
this.imageURL = imageURL;
this.source = source;
this.getage = function (now, then) {
return (now.getTime() - then.getTime());
};
this.isImage = function (imageURL) {
return checkURL(imageURL);
};
this.isIMGUR = function (imageURL) {
return checkIMGUR(imageURL);
};
this.getimagefromhtml = function (content) {
//give precedence to images in address links
var imageURL = "";
var imageLinkURL = "";
const parser = new htmlparser2.Parser(
{
onopentag(name, attribs) {
if (name === "img") {
imageURL = (attribs.src);
}
else if (name == "a") {
if (checkURL(attribs.href)) { imageLinkURL = (attribs.href); };
}
},
ontext(text) {
//console.log("-->", text);
},
onclosetag(tagname) {
if (tagname === "script") {
//console.log("That's it?!");
}
}
},
{ decodeEntities: true }
)
parser.write(content);
parser.end();
if (imageLinkURL == "") {
return imageURL;
}
else {
return imageLinkURL;
};
};
this.gethashCode = function (s) {
for (var i = 0, h = 0; i < s.length; i++)
h = Math.imul(31, h) + s.charCodeAt(i) | 0;
return h;
};
this.categories = categories;
};
var getid = function (idasadate) {// we use the published or current date to create a numeric key; will need better processing due to duplicates
if (idasadate == null) {
return new Date().getTime();
}
return idasadate.getTime();
};
var checkURL = function (imgurl) {
const regex = /\.(jpeg|jpg|gif|png|bmp|tiff)$/;
//console.log(imgurl);
//console.log(imgurl.match(regex));
return (imgurl.match(regex) != null);
};
var checkIMGUR = function (imgurl) {
const regex = /\.(gifv)$/;
//console.log(imgurl);
//console.log(imgurl.match(regex));
return (imgurl.match(regex) != null);
};
exports.RSSitems = function () {
this.items = [];
//this.getage = function () { return age; }
};
exports.RSSsource = function () {
this.title = '';
this.sourcetitle = '';
this.url = '';
this.sourceiconclass = null; //use a suitabel fontawesome class name
};
// an ayync function that allows us to use the await option on the call to get the image; Gerneric handling for errors which could include timeouts
// async function executeAsyncTask() {
// const valueA = await functionA();
// const valueB = await functionB(valueA);
// return function3(valueA, valueB);
//}
exports.checkfortrackingpixel = async function (url, moduleinstance) {
var self = this;
if (self.logger == null) {
self.logger = LOG.createLogger("rss.log", "rss"); // will add logging by name if needed
}
self.logger.info(`check for pixel ${url}`);
try {
let response = await axios.head(url);
self.logger.info(`waited ${response.headers["content-length"]}`);
if (response.headers["content-length"] == "0") {
return true;
}
else {
return false;
}
} catch (error) {
console.error(`Could not get the URL: ${url} because we got an error: ${error}`);
return false;
}
self.logger.info(`dropped out the bottom `);
return false;
};