-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.js
140 lines (140 loc) · 6.37 KB
/
main.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
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const got_1 = __importDefault(require("got"));
const config_1 = require("./config");
const error_1 = __importDefault(require("./error"));
const random_useragent_1 = __importDefault(require("random-useragent"));
const util_1 = __importDefault(require("./util"));
class Brainly {
/**
*
* @param country - Here, please put your application server country code. if you do not enter valid region/country code. It will trigger an Error Exception.
*/
constructor(country = "id") {
this.country = country;
this.clientRequest = (lang) => got_1.default.extend({
prefixUrl: `${this.getBaseURL(this.country)}/graphql`,
headers: {
"user-agent": this.getAgent(),
"origin": this.getBaseURL(lang),
"sec-gpc": "1",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"batch": "true"
}
});
if (!this.isValidLanguage(country))
throw new error_1.default("Please put valid country!");
}
/**
* Use this function if you want search question, it will returns question detail, question author, answer detail, attachments (if question or answer attachments is any), rating question and answer.
*
* @param language What language want to search?
* @param question A question you want to search. Example: `Pythagoras theory`
* @param length Length array from question list
*/
async search(language = "id", question, length = 10) {
try {
if (!this.isValidLanguage(language))
throw new error_1.default("Please put valid language!");
const body = this.getRequestBody(question, length);
const response = await this.clientRequest(this.country.toLowerCase()).post(language.toLowerCase(), {
json: body
});
const validJSON = JSON.parse(response.body)[0].data.questionSearch.edges;
const objects = validJSON.map(obj => {
const question = {
id: obj.node.databaseId,
content: util_1.default.clearContent(obj.node.content),
attachments: obj.node.attachments.map(attach => attach.url),
author: obj.node.author ? {
id: obj.node.author.databaseId,
avatar_url: obj.node.author.avatar ? obj.node.author.avatar.url : undefined,
deleted: obj.node.author.isDeleted,
url: `${this.getBaseURL(language)}/app/profile/${obj.node.author.databaseId}`,
rank: obj.node.author.rank ? obj.node.author.rank.name : "-",
username: obj.node.author.nick,
receivedThanks: obj.node.author.receivedThanks,
bestAnswersCount: obj.node.author.bestAnswersCount,
gender: obj.node.author.gender,
description: obj.node.author.description,
points: obj.node.author.points,
helpedUsersCount: obj.node.author.helpedUsersCount
} : undefined,
points: {
points: obj.node.points,
forBest: obj.node.pointsForBestAnswer
},
grade: obj.node.grade.name,
education: obj.node.subject.name,
created: obj.node.created,
can_be_answered: obj.node.canBeAnswered,
url: `${this.getBaseURL(language)}/${util_1.default.resolveWorkName(language.toLowerCase())}/${obj.node.databaseId}`
};
const answers = obj.node.answers.nodes.map(answerObj => ({
content: util_1.default.clearContent(answerObj.content),
attachments: answerObj.attachments.map(attach => attach.url),
rates: answerObj.ratesCount,
rating: answerObj.rating,
isBest: answerObj.isBest,
created: answerObj.created,
author: answerObj.author ? {
id: answerObj.author.databaseId,
username: answerObj.author.nick,
gender: answerObj.author.gender,
avatar_url: answerObj.author.avatar ? answerObj.author.avatar.url : undefined,
deleted: answerObj.author.isDeleted,
url: `${this.getBaseURL(language.toLowerCase())}/app/profile/${answerObj.author.databaseId}`,
description: answerObj.author.description,
bestAnswersCount: answerObj.author.bestAnswersCount,
points: answerObj.author.points,
helpedUsersCount: answerObj.author.helpedUsersCount,
receivedThanks: answerObj.author.receivedThanks,
rank: answerObj.author.rank ? answerObj.author.rank.name : "-"
} : undefined
}));
return {
question, answers
};
});
return objects;
}
catch (err) {
throw new Error(JSON.stringify(err));
}
}
getRequestBody(question, length = 10) {
return [{
operationName: "SearchQuery",
query: config_1.graphql_query,
variables: {
after: null,
first: length,
query: question
}
}];
}
isValidLanguage(lang) {
return config_1.languages.includes(lang.toLowerCase());
}
/**
* This function will return brainly site url from your country selection in the constructor
*
* @returns {String} - A base url of your country selection
*/
getBaseURL(lang) {
return config_1.baseURLs[lang];
}
/**
* Use this function if you want get random user agent.
*
*/
getAgent() {
return random_useragent_1.default.getRandom();
}
}
exports.default = Brainly;