-
Notifications
You must be signed in to change notification settings - Fork 1
/
fetchutil.js
251 lines (211 loc) · 9.01 KB
/
fetchutil.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
const fetch = require('node-fetch');
const { JSDOM } = require("jsdom");
const sharp = require('sharp');
const DOMPurify = require('isomorphic-dompurify');
const { DOMParser } = require('xmldom');
const crypto = require('crypto');
// Loading process:
// 1. grab news from google
// 2. fetch redirect link
// 3. grab metadata from redirect link
// 4. grab image from metadata
// Dynamic loading:
// 1. send titles immediately
// 2. send metadata
// 3. send images
// All independent
function createHash(text) {
const hash = crypto.createHash('sha256');
hash.update(text);
return hash.digest('hex');
}
function createFeedItemUID(content) {
return createHash(content);
}
function fetchAndCropImage(url) {
return fetch(url)
.then(response => response.buffer())
.then(buffer => {
return sharp(buffer)
.resize(200, 200, {
fit: 'cover',
position: 'center',
})
.toBuffer()
.then(croppedImgBuffer => {
const croppedImgDataUrl = `data:image/jpeg;base64,${croppedImgBuffer.toString('base64')}`;
return croppedImgDataUrl;
});
})
.catch(error => {
console.error("error: failed fetchAndCropImage");
});
}
function queryMetaElement(doc, str, prop) {
const obj = doc.querySelector('meta['+prop+'="'+str+'"]');
if (obj != null) {
return obj.getAttribute('content');
}
return null;
}
function fetchRedirectLink(url) {
return fetch(url)
.then(response => response.text())
.then(data => {
var resurl = undefined
const regex = /<a href="(.*?)"/;
const match = data.match(regex);
if (match) {
resurl = match[1];
console.log("redirect url:", resurl)
} else {
console.log('No URL found in HTML');
}
return resurl
})
.catch(error => console.error("error: failed fetchRedirectLink"));
}
function fetchMetaData(url) {
return fetch(url)
.then(response => response.text())
.then(html => {
const modhtml = html.replace(/(\s+property="[^"]*)\:([^"]*")/g, '$1-$2').replace(/(\s+name="[^"]*)\:([^"]*")/g, '$1-$2');; // replace : in properties with -
var cleanhtml = DOMPurify.sanitize(modhtml, {
ALLOWED_TAGS: ['meta'],
ALLOWED_ATTR: ["name", "property", "content"],
WHOLE_DOCUMENT: true
});
cleanhtml = cleanhtml.replace(/(\s+property="[^"]*)\-([^"]*")/g, '$1:$2').replace(/(\s+name="[^"]*)\-([^"]*")/g, '$1:$2'); // revert : in properties
const dom = new JSDOM(cleanhtml, { includeNodeLocations: true });
const doc = dom.window.document;
const twitterimg_meta_name_query_str = "twitter:image"; // twitter:image:src
const tags_meta_name_query_str = "parsely-tags";
const description_meta_name_query_str = "description";
const keywords_meta_name_query_str = "keywords";
const type_meta_property_query_str = "og:type";
const sitename_meta_property_query_str = "og:site_name";
const image_meta_property_query_str = "og:image";
const title_meta_property_query_str = "og:title";
const description_meta_property_query_str = "og:description";
const pubdate_meta_property_query_str = "article:published_time";
const m_img_twitter = queryMetaElement(doc, twitterimg_meta_name_query_str, "name");
const m_tags = queryMetaElement(doc, tags_meta_name_query_str, "name");
const m_description = queryMetaElement(doc, description_meta_name_query_str, "name");
const m_keywords = queryMetaElement(doc, keywords_meta_name_query_str, "name");
const m_img = queryMetaElement(doc, image_meta_property_query_str, "property");
const m_type = queryMetaElement(doc, type_meta_property_query_str, "property");
const m_sitename = queryMetaElement(doc, sitename_meta_property_query_str, "property");
const m_title = queryMetaElement(doc, title_meta_property_query_str, "property");
const m_ogdescription = queryMetaElement(doc, description_meta_property_query_str, "property");
const m_pubdate = queryMetaElement(doc, pubdate_meta_property_query_str, "property");
var metadata = {
"m_img_twitter" : m_img_twitter,
"m_tags" : m_tags,
"m_description" : m_description,
"m_keywords" : m_keywords,
"m_img" : m_img,
"m_type" : m_type,
"m_sitename" : m_sitename,
"m_title" : m_title,
"m_ogdescription" : m_ogdescription,
"m_pubdate": m_pubdate,
"m_url": url
}
return metadata;
})
}
async function getNewsFeedImage(googlenewslink) {
return fetchRedirectLink(googlenewslink)
.then(redirectedlink => {
if (redirectedlink != undefined) {
return fetchMetaData(redirectedlink);
}
else {
return undefined;
}
})
.then(metadata => {
if (metadata != undefined) {
if (metadata["m_img"]) {
return fetchAndCropImage(metadata["m_img"]);
}
else {
return undefined
}
}
else {
return undefined;
}
})
}
async function getMetaFeedData(googlenewslink) {
return fetchRedirectLink(googlenewslink)
.then(redirectedlink => {
if (redirectedlink != undefined) {
return fetchMetaData(redirectedlink);
}
else {
return undefined;
}
})
}
function collectNews(socket, newsfeed, startindex, count) {
let feedid = newsfeed["feedid"];
const url = 'https://news.google.com/rss/search?q='+newsfeed["feedkeywordstr"]+'&hl=en-NZ&gl=NZ&ceid=NZ:en';
fetch(url)
.then(response => response.text())
.then(async data => {
const parser = new DOMParser();
let relinkdata = data.replace(/<link>/g, "<relink>").replace(/<\/link>/g, "</relink>"); // this is ugly but dompurify sanitieser removes </link> as we are dealing with XML
const cleanData = DOMPurify.sanitize(relinkdata, {
ALLOWED_TAGS: ['item', 'title', 'description', 'relink']
});
const xml = parser.parseFromString(cleanData, 'text/xml');
const items = xml.getElementsByTagName('item');
const maxitem = Math.min(items.length, startindex + count);
for (let i = startindex; i < maxitem; i++) {
let itemnumber = i;
let title = items[i].getElementsByTagName('title')[0].textContent;
let description = items[i].getElementsByTagName('description')[0].textContent;
let linkurl = items[i].getElementsByTagName('relink')[0].textContent;
let fuid = createFeedItemUID(title+description);
// 1. send titles + google redirect url immediately
let feeditem = { itemnumber, feedid, title, linkurl, fuid };
socket.emit('newsfeeditem', feeditem);
//////
let imagesrc = "";
let metadescr = "";
let pubdate = "";
getMetaFeedData(linkurl)
.then(metadata => {
if (metadata != undefined) {
if (metadata["m_description"]) {
metadescr = metadata["m_description"]
}
if (metadata["m_url"]) {
linkurl = metadata["m_url"]
}
if (metadata["m_pubdate"]) {
pubdate = metadata["m_pubdate"]
}
if (metadata["m_img"]) {
return fetchAndCropImage(metadata["m_img"]);
}
// 2. Send updated metadata, direct link, without image
let feeditem = { itemnumber, feedid, title, description, linkurl, metadescr, pubdate, fuid };
socket.emit('newsfeeditem', feeditem);
}
}).then(imagedata => {
imagesrc = imagedata;
// 3. Send full data
let feeditem = { itemnumber, feedid, title, description, linkurl, metadescr, imagesrc, pubdate, fuid };
socket.emit('newsfeeditem', feeditem);
})
.catch(error => console.error("could not fetch"));
}
})
.catch(error => {
console.error("error: failed fetching newsfeed"+newsfeed);
});
}
module.exports = { collectNews};