-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
83 lines (74 loc) · 2.47 KB
/
index.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
var express = require('express');
var request = require('request');
var cheerio = require('cheerio');
var URL = require('url-parse');
var bodyParser = require('body-parser');
var app = express();
app.listen(3000, function(){
console.log('Listening on port 3000');
});
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
app.use(bodyParser.urlencoded({extended: true}));
app.post('/scripture', function(req, res){
scripture(req.body.book, req.body.chapter, req.body.verseNum);
res.redirect('/');
});
function format(arr) {
for (i=0; i<arr.length; i++) {
if (arr[i].length == 1) {
arr[i] = "00" + arr[i];
} else if (arr[i].length == 2) {
arr[i] = "0" + arr[i];
}
}
console.log(arr);
return arr;
}
function scripture(book, chapter, verseNum) {
verseNum = verseNum.replace(/\s/g, '');
if (verseNum.indexOf(',') > -1) {
verseNum = verseNum.split(',');
verseNum = format(verseNum);
} else if(verseNum.indexOf('-') > -1) {
verseNum = verseNum.split('-');
var lowEnd = parseInt(verseNum[0]);
var highEnd = parseInt(verseNum[1]);
verseNum = [];
for (x=lowEnd; x<=highEnd; x++) {
verseNum.push(x.toString());
}
verseNum = format(verseNum);
} else {
if (verseNum.length == 1) {
verseNum = "00" + verseNum;
} else if (verseNum.length == 2) {
verseNum = "0" + verseNum;
}
}
var pageToVisit = '';
book = book.replace(/^\s\s*/, '').replace(/\s\s*$/, '').replace(/ /g,"-");;
pageToVisit = "https://www.jw.org/en/publications/bible/nwt/books/" + book + "/" + chapter + "/";
request(pageToVisit, function(error, response, body) {
if(error) {
console.log("Error: " + error);
}
// Check status code (200 is HTTP OK)
//console.log("Status code: " + response.statusCode);
if(response.statusCode === 200) {
var $ = cheerio.load(body);
$('.verse span, .footnote').children().remove();
var verse = '';
if (Array.isArray(verseNum)) {
for (v=0;v<verseNum.length;v++){
verse += $('[id$="' + verseNum[v] + '"]').text();
}
} else {
verse = $('[id$="' + verseNum + '"]').text();
}
verse = verse.replace(/\r?\n|\r/g, ' ');
console.log(verse);
}
});
}