-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
executable file
·39 lines (35 loc) · 1.04 KB
/
api.js
File metadata and controls
executable file
·39 lines (35 loc) · 1.04 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
'use strict';
const driver = require('promise-phantom');
const phantomjs = require('phantomjs-prebuilt');
function init(page, cb) {
page.evaluate(function () {
const $ = document.querySelectorAll.bind(document);
var arr = [];
while(arr.length < 3) {
var randomnumber = Math.ceil(Math.random()*8)
if(arr.indexOf($('.titlenews')[randomnumber].textContent) > -1) continue;
arr[arr.length] = $('.titlenews')[randomnumber].textContent;
}
return {
news: arr,
isDone: arr.length > 0
};
})
.then(result => {
if (result.isDone) {
cb(null, result)
page.close();
} else {
setTimeout(init, 100, page, cb);
}
})
.catch(cb);
}
module.exports = cb => {
driver.create({path: phantomjs.path})
.then(phantom => phantom.createPage())
.then(page => page.open('http://www.sunnyskyz.com/good-news').then(() => {
init(page, cb);
}))
.catch(cb);
};