-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (120 loc) · 3.9 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
let request = require("request-promise");
const cookieJar = request.jar();
const { Parser } = require("json2csv");
request = request.defaults({
jar: cookieJar,
});
const fs = require("fs");
const ora = require("ora");
//
// ─── LOGIN ──────────────────────────────────────────────────────────────────────
//
async function login(email, password) {
const spinner = ora("Logging you in...").start();
try {
const result = await request.get("https://internshala.com/");
const cookieString = cookieJar.getCookieString("https://internshala.com/");
const splittedByCsrfCookieName = cookieString.split("csrf_cookie_name=");
const csrf_test_name = splittedByCsrfCookieName[1].split(";")[0];
const options = {
method: "POST",
uri: "https://internshala.com/login/verify_ajax/user",
form: {
csrf_test_name,
email,
password,
},
};
let out = await request(options);
out = JSON.parse(out);
if (out.success !== true) {
spinner.fail("Invalid login!");
return new Error("Could not login credentials!");
} else {
spinner.succeed("Logged in successfully!");
return 1;
}
} catch (error) {
return new Error(error);
}
}
//
// ─── GRAB DETAILS FROM A PAGE ───────────────────────────────────────────────────
//
async function getDetails(page_number) {
try {
let result;
const options = {
method: "POST",
uri: "https://internshala.com/student/paginated_applications",
form: {
page_number,
},
};
await request(options, (err, res, body) => {
let data = JSON.parse(body);
result = {
applications: data.applications_data,
total_pages: data.total_pages,
};
});
return result;
} catch (error) {
spinner.fail("Could not fetch data!");
return error;
}
}
//
// ─── MAKE THE CSV AFTER EXTRACTION ──────────────────────────────────────────────
//
async function Login_and_Get_Details(email, password) {
const check = await login(email, password);
if (check !== 1) {
return Error("Could not login");
}
var application = [];
var result = {
application,
};
const spinner = ora("Fetching your data!").start();
//Get the page_numbers
const data = await getDetails(1);
const total_pages = data.total_pages;
// Get the data by iteration
for (let page = 1; page <= total_pages; page++) {
const data = await getDetails(page);
const applications = data.applications;
applications.forEach((application) => {
const application_status = application.application_status;
const company_name = application.company_name;
const company_url = "https://internshala.com" + application.job_url + "/";
const profile = application.profile;
result.application.push({
application_status,
company_name,
company_url,
profile,
});
});
}
spinner.succeed("Fetched data successfully!");
spinner.start("Writing your data!");
const json2csvParser = new Parser();
const result_csv = json2csvParser.parse(result.application);
fs.writeFile("output.csv", result_csv, (err) => {
if (err) {
spinner.fail("Could not write data!");
throw err;
}
spinner.succeed("Data written successfully!");
});
}
//
// ─── TAKING INPUT FROM USER ─────────────────────────────────────────────────────
//
var email = process.argv[2];
var password = process.argv[3];
//
// ─── RUNNING THE FUNCTION ───────────────────────────────────────────────────────
//
Login_and_Get_Details(email, password);