-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (120 loc) · 3.42 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
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
const axios = require("axios");
const cheerio = require("cheerio");
const baseUrl = "https://dangeru.us";
const apiEndpoint = `${baseUrl}/api/v2`;
async function apiFetch(path) {
return await axios.get(apiEndpoint + path)
.catch(console.error)
}
async function getNews() {
const html = await axios.get(baseUrl)
.then((response)=> response.data)
.catch(console.error)
const $ = cheerio.load(html);
const redtext = $("#index-news .redtext").text();
// The date should be the first thing in the announcement
// and follow the format of "DD/MM/YY".
// Should. Pray that it is.
const dateString = redtext
.match(/[\w\d\/]+/)[0]
// Flip date around
const dateArray = dateString
.split("/")
.reverse()
// If for some reason something's missing from the date
// prepend the current year to it
if (dateArray.length < 3)
dateArray.unshift(new Date().getFullYear());
// Better safe than sorry right
const yearLength = dateArray[0].length;
// Literally shaking and crying rn
if (yearLength < 4) {
const yearPrefix = new Date()
.getFullYear()
.toString()
.slice(0, 4 - yearLength)
dateArray[0] = yearPrefix + dateArray[0];
}
// Please work I don't want to put like 3 entire
// try-catch blocks in here please
return {
date: new Date(dateArray.join("/")) || undefined,
dateRaw: dateString,
announcement: redtext.match(/\s(.*)/)[1],
}
}
async function getStats() {
const html = await axios.get(baseUrl)
.then((response)=> response.data)
.catch(console.error)
const $ = cheerio.load(html);
const numbers = $(".comment-styled.boarda-margin.index-links span[style='font-weight: bold;']")
.map((_, span)=> parseInt( $(span).text() ))
return {
threads: {
active: numbers[0],
archived: numbers[2],
},
replies: {
active: numbers[1],
archived: numbers[3],
},
burgs: {
normal: numbers[4],
angry: numbers[5],
}
}
}
async function getBoards() {
// You can only retrieve the short board names
// from the API, so let's scrape them from the
// front page instead.
// const boards = await apiFetch("/boards")
// .then((response) => response.data)
const html = await axios.get(baseUrl)
.then((response)=> response.data)
.catch(console.error)
const $ = cheerio.load(html);
// Get all the board link tags
const boards = $("#index-boards a")
.map((_, anchor)=> ({
long: $(anchor).text(),
short: anchor.attribs.href
.match(/(?!\/)[^\/]+/g)
.reverse()[0],
}))
.toArray()
return boards;
}
async function getThreads(board, pageNumber=0) {
const threads = await apiFetch(`/board/${board}?page=${pageNumber}`)
.then((response) => response.data)
.catch(console.error)
if (!threads || !threads.length) {
console.error(new Error(
`Page ${pageNumber + 1} of /${board}/ does not exist.`));
}
return threads;
}
async function getPageCount(board) {
const html = await axios.get(`${baseUrl}/${board}`)
.then((response)=> response.data)
.catch(console.error)
const $ = cheerio.load(html);
return parseInt(
$(".pagecount").last().text())
}
async function getReplies(threadId) {
return await apiFetch(`/thread/${threadId}/replies`)
.then((response) => response.data)
.catch(console.error)
}
module.exports = {
apiFetch,
getNews,
getStats,
getBoards,
getThreads,
getReplies,
getPageCount,
}