-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (103 loc) · 3.92 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
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
const express = require('express');
const quest = require('request');
const cheerio = require('cheerio');
const puppeteer = require('puppeteer');
var cors = require('cors')
let port = process.env.PORT || 3000
const app = express();
app.use(cors())
//ROUTES
//search
app.get('/search', (request, response) => {
console.log(request.query.name);
quest(`https://b-ok.africa/s/${request.query.name}`, (error, _response, html) => {
if (!error && _response.statusCode == 200) {
const $ = cheerio.load(html);
const bookArray = [];
$('h3 > a').each((i,el) => {
const title = $(el).text()
const link = $(el).attr('href');
bookArray.push({name:title, link:link})
});
response.send(bookArray)
}
});
});
// Details
app.get('/details', (request, response) => {
console.log(request.query.link);
getdetails(request.query.link).then((data)=>{
response.send(data)
}).catch(console.error);
});
// Download
app.get('/download', (request, response) => {
console.log(request.query.link);
DownloadBook(request.query.link).then((data)=>{
console.log(data);
response.send(data)
}).catch(console.error);
});
app.listen(port)
//FUNCTIONS
function getdetails (link) {
return new Promise(async (resolve, reject) => {
try {
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
],
});
const page = await browser.newPage();
await page.goto(`https://b-ok.africa${link}`, {waitUntil: 'networkidle2'});
await page.waitForSelector('.details-book-cover > img',{visible: true})
let urls = await page.evaluate(() => {
let results = {}
if(document.querySelector('#bookDescriptionBox')){
let text = document.querySelector('#bookDescriptionBox').innerText;
let img = document.querySelector('.details-book-cover > img').getAttribute('src')
let size = document.querySelector('a.btn.btn-primary.dlButton.addDownloadedBook').innerText;
results ={description:text, image:img, size:size}
}else{
let img = document.querySelector('.details-book-cover > img').getAttribute('src')
let size = document.querySelector('a.btn.btn-primary.dlButton.addDownloadedBook').innerText;
results = {image:img, size:size}
}
return results;
})
browser.close();
return resolve(urls);
} catch (e) {
return reject(e);
}
})
}
function DownloadBook (link) {
return new Promise(async (resolve, reject) => {
try {
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
],
});
const page = await browser.newPage();
await page.goto(`https://b-ok.africa${link}`, {waitUntil: 'networkidle2'});
console.log('Clicking on "Download PDF" button');
await page.on('response', response => {
console.log(response.url());
if (response.url().indexOf('dtoken') > -1){
console.log("response code: ", response.status(), response.url());
urls = response.url()
return resolve(urls);
}
});
await page.click('a.btn.btn-primary.dlButton.addDownloadedBook')
await page.waitForNavigation({waitUntil: 'networkidle2'})
browser.close();
} catch (e) {
return reject(e);
}
})
}