-
Notifications
You must be signed in to change notification settings - Fork 0
/
subj.js
181 lines (176 loc) · 4.27 KB
/
subj.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
170
171
172
173
174
175
176
177
178
179
180
181
import got from "got";
import { load } from "cheerio";
import fs,{ writeFile, readFileSync } from "fs";
let ans_cnt = 1;
let json = [];
const extractLinks = async (url) => {
let extras = [
"lisp-programming-questions-answers",
"technical-interview-questions",
"san-storage-mcqs-freshers-experienced"
]
extras.join(",");
if(extras.some(str=>url.includes(str))){
try {
const response = await got(url);
const html = response.body;
const links = [];
const $ = load(html);
$(".entry-content a").each( function () {
let text = $(this).text()
let link = $(this).attr("href");
if(!text.includes("interview")) links.push(link);
});
return links;
}catch(err){
console.error(err);
}
}
else{
try {
const response = await got(url);
const html = response.body;
const links = [];
const $ = load(html);
$(".sf-section a").each(function () {
let link = $(this).attr("href");
links.push(link);
});
return links;
} catch (error) {
console.log(error);
}
}
};
const mcqScrap = async (url) => {
try {
const response = await got(url);
const html = response.body;
const $ = load(html);
$(".sf-mobile-ads").remove(); //removing ads
$(".sf-desktop-ads").remove(); //removing ads
let b = [];
let ans = [];
$(".entry-content .collapseomatic").each(function () {
let id = $(this).attr("id");
$(this).remove();
if (id) {
ans.push($("#target-" + id).text());
}
});
$(".entry-content .hk1_style-wrap5").each(function () {
if ($(this).prev("p")) {
let check = $(this).prev();
if (check.html().length == 0) return;
}
b.push($(this).html());
});
let done = 0;
let answ=null;
let q;
let o;
//-------------------//
$(".entry-content p").each(function () {
let ques=false;
let arr = $(this).html().split("<br>");
// for questions along with some code snippets (impure mcq stuffs)
if (arr.length == 1 || arr.length == 2) {
if ($(this).text().length == 0) $(this).remove();
if ($(this).next().attr("class") == "hk1_style-wrap5") {
done++;
q=$(this).text() + b.shift();
if (done == 4) {
answ = ans.shift();
done = 0;
}
} else return;
}
//---------------------------
if (arr.length === 5) {
let option = $(this).text();
option = option.trim().split("\n");
let answer=null;
if(answ){
answer = answ;
answ="";
}
else
answer = ans.shift()
json.push({
id: ans_cnt++,
Question: q,
Options: option,
Answer: answer,
});
q="";
}
if(arr.length ===3){
let yes=false;
for(let i=0; i<arr.length;i++){
if(arr[i].includes("true") && arr[i].includes("false"))
yes =true;
}
if(yes){
let ques=$(this).text();
let string = ques.trim().split("\n");
let question = string.shift().replace(/^[0-9]+. /g, "");
let option = string;
let answer = ans.shift();
json.push({
id: ans_cnt++,
Question: question,
Options: option,
Answer: answer,
});
}
}
if (arr.length === 6) {
let ques=$(this).text();
let string = ques.trim().split("\n");
let question = string.shift().replace(/^[0-9]+. /g, "");
let option = string;
let answer = ans.shift();
json.push({
id: ans_cnt++,
Question: question,
Options: option,
Answer: answer,
});
}
});
} catch (error) {
console.log(error);
}
};
const extractMcq = async (URL,title) => {
if(title.includes("/"))
title = title.replace("/","-");
if (fs.existsSync("./saved/"+title+".json")) {
console.log(fname+" already exists");
return;
}
console.log("Extracting: "+title);
let li = await extractLinks(URL);
let ctr = 0;
let num = li.length;
console.time("Scraping took: ");
for (let i = 0; i < num; i++) {
ctr++;
await mcqScrap(li[i]);
}
console.timeEnd("Scraping took: ");
if (ctr === num) {
writeFile(
"./saved/" + title + ".json",
JSON.stringify(json, null, 4),
function (err) {
if (err) throw err;
json = [];
console.log("Saved JSON!");
console.log(`Total ${ans_cnt} questions scrapped`);
ans_cnt = 1;
}
);
}
};
export default extractMcq;