-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwikiArticle.js
More file actions
50 lines (46 loc) · 1.6 KB
/
wikiArticle.js
File metadata and controls
50 lines (46 loc) · 1.6 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
var scraperjs = require('scraperjs');
var request = require('request');
var monthNames = [
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"
];
function getDate(ts) {
var date = new Date(ts);
var day = date.getDate();
var monthIndex = date.getMonth();
var year = date.getFullYear();
return monthNames[monthIndex] + "_" + day + ",_" + year;
}
function wikiArticle(ts, callback) {
var date;
if (ts == undefined || ts == null) {
date = getDate(Date.now());
} else {
date = getDate(ts);
}
console.log("Date: " + date);
scraperjs.StaticScraper.create('https://en.m.wikipedia.org/wiki/Wikipedia:Today\'s_featured_article/' + date)
.scrape(function ($) {
var value= $("#bodyContent a")[1];
var article ={}
article.title= value.attribs.title;
article.url = "https://en.wikipedia.org/wiki/"+value.attribs.href;
return article;
})
.then(function (article) {
var encodedTitle = encodeURIComponent(article.title);
var request = require("request");
request.get("https://rest.wikimedia.org/en.wikipedia.org/v1/page/summary/" + encodedTitle, function (error, response, body) {
if (error) {
console.log(error);
} else {
var result= JSON.parse(body);
result.url =article.url;
callback(result);
}
});
})
}
module.exports = wikiArticle;