-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnovelhall.js
169 lines (135 loc) · 4.9 KB
/
novelhall.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
const axios = require('axios');
const cheerio = require('cheerio');
const e = require('express');
const express = require('express');
const app = express();
const baseUrl = 'https://www.novelhall.com';
const absoluteUrl = (relativeUrl) => {
if (relativeUrl.startsWith('http')) return relativeUrl;
return baseUrl + relativeUrl;
};
app.get('/novels/:page', async (req, res) => {
try {
const page = req.params.page;
const response = await axios.get(`https://www.novelhall.com/all2022-${page}.html`);
const $ = cheerio.load(response.data);
const novels = [];
$('li.btm a').each((i, element) => {
const href = element.attribs.href;
const id = href.replace(/\//g, ''); // Remove forward slashes
const title = href
.replace(/-/g, ' ') // Replace hyphens with spaces
.replace(/\d+/g, '') // Remove numbers
.replace(/\//g, '') // Remove forward slashes
.trim();
novels.push({
id,
title,
url: absoluteUrl(href),
});
});
res.json({ novels });
} catch (error) {
console.error(error);
res.sendStatus(500);
}
});
app.get('/search/:query', async (req, res) => {
try {
const query = req.params.query;
const url = `https://www.novelhall.com/index.php?s=so&module=book&keyword=${query}`;
const response = await axios.get(url);
const $ = cheerio.load(response.data);
const results = [];
$('.section3 table tr td:nth-child(2) a').each((i, element) => {
const href = element.attribs.href;
const id = href.replace(/\//g, ''); // Remove forward slashes
const title = href
.replace(/-/g, ' ') // Replace hyphens with spaces
.replace(/\d+/g, '') // Remove numbers
.replace(/\//g, '') // Remove forward slashes
.trim();
results.push({
id,
title,
});
});
res.json({ results });
} catch (error) {
console.error(error);
res.sendStatus(500);
}
});
app.get('/novel/:id', async (req, res) => {
try {
const id = req.params.id;
const url = `https://www.novelhall.com/${id}/`;
const response = await axios.get(url);
const $ = cheerio.load(response.data);
const novel = {};
const possibleTitle = $('.book-info h1');
if (!possibleTitle) throw new Error('No novel title');
novel.title = possibleTitle.text();
const absoluteUrl = (relativeUrl) => {
if (relativeUrl.startsWith('http')) return relativeUrl;
return baseUrl + relativeUrl;
};
const possibleImage = $('div.book-img img');
if (possibleImage) {
novel.cover = absoluteUrl(possibleImage.attr('src'));
}
const author = $('div.book-info div.total.booktag span.blue').first();
author.find('p').remove();
novel.author = author.text().trim().replace('Author', '').replace(':', '');
// Add summary, genre, status, and chapters
novel.summary = $('.js-close-wrap').text().trim().replace('<<', '').replace('back', '');
novel.genre = $('.red').text().trim();
novel.status = $('span.blue:nth-child(3)').text().trim().replace('Status', '').replace(/\:/g, '');
novel.chapters = [];
$('div#morelist li').each((i, element) => {
const chapter = {};
const a = $(element).find('a');
chapter.url = absoluteUrl(a.attr('href')).replace(/^.+\//, '').replace('.html', '');
chapter.title = a.text().trim();
novel.chapters.push(chapter);
});
res.json({ novel });
} catch (error) {
console.error(error);
res.sendStatus(500);
}
});
app.get('/novel/:id/:url', async (req, res) => {
try {
const id = req.params.id;
const url = req.params.url;
const targetUrl = `https://www.novelhall.com/${id}/${url}.html`;
const response = await axios.get(targetUrl);
const $ = cheerio.load(response.data);
const chapter = {};
chapter.title = $('h1').text();
const contents = $('div#htmlContent.entry-content');
contents.find('div').remove();
chapter.content = contents.html();
// Extract next and previous links
const nextLink = $('.nav-single a:last-child').attr('href');
if (nextLink) {
chapter.next = nextLink.split('/').slice(-1)[0].replace('.html', '');
}
const novelID = $('.nav-single a:nth-child(3)').attr('href');
if (novelID) {
chapter.index = novelID.replace(/\//g, '');
}
const prevLink = $('.nav-single a:nth-child(1)').attr('href');
if (prevLink) {
chapter.prev = prevLink.split('/').slice(-1)[0].replace('.html', '');
}
res.json({ chapter });
} catch (error) {
console.error(error);
res.sendStatus(500);
}
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});