-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
54 lines (49 loc) · 2.32 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
const fetch = require("node-fetch");
let baseUrl = "https://9gag.com/";
let self = module.exports = {
getFetch(url) {
return fetch(url, {
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36",
"Content-Type": "application/json"
},
})
},
async getType({ type = "hot", cursor = "", simplified = false }) {
let url = `${baseUrl}v1/group-posts/group/default/type/${type}`;
if (cursor.length > 0) {
url += `?${cursor}`
}
let res = await self.getFetch(url);
let json = await res.json();
if (json["meta"]["status"] !== "Success") {
if (typeof json["meta"]["errorMessage"] !== "undefined" && json["meta"]["errorMessage"] !== "") {
throw new Error(json["meta"]["errorMessage"]);
} else {
throw new Error("Request failed.");
}
}
return {
posts: (!simplified) ? json["data"]["posts"] : json["data"]["posts"].map(p => {
let { id, title, type, upVoteCount, downVoteCount, creationTs, images, commentsCount } = p;
return { id, title, type, upVoteCount, downVoteCount, creationTs, images, commentsCount };
}),
cursor: json["data"]["nextCursor"]
};
},
async getComments({ id = "", orderKey = "", simplified = false }) {
let url = `https://comment-cdn.9gag.com/v1/cacheable/comment-list.json?appId=a_dd8f2b7d304a10edaf6f29517ea0ca4100a43d1b&url=http:%2F%2F9gag.com%2Fgag%2F${id}&count=30&order=score&origin=https:%2F%2F9gag.com`
if (orderKey.length > 0) {
url += `&ref=${orderKey}`;
}
let res = await self.getFetch(url);
let json = await res.json();
if (json["status"] !== "OK") {
throw new Error("Request failed.");
}
return (!simplified) ? json["payload"]["comments"] : json["payload"]["comments"].map(c => {
let { commentId, parent, user, text, timestamp, type, orderKey, likeCount, dislikeCount, childrenTotal, children } = c;
return { commentId, parent, user, text, timestamp, type, orderKey, likeCount, dislikeCount, childrenTotal, children };
});
}
};