-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
71 lines (61 loc) · 1.96 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
const axios = require("axios");
const cheerio = require("cheerio");
const fs = require("fs");
const readline = require("node:readline").createInterface({
input: process.stdin,
output: process.stdout,
});
const getInput = (message) => {
return new Promise((resolve, reject) => {
readline.question(`${message}: `, (input) => {
resolve(input);
});
});
};
const getFollowers = async (username) => {
console.log("Data is extracting from github ...");
try {
finish_followers = false;
p_index = 1;
let followers_data = [];
while (!finish_followers) {
const response = await axios.get(
`https://github.com/${username}?page=${p_index}&tab=followers`
);
const html = response.data;
const $ = cheerio.load(html);
let follower_p_data = [];
if ($("span.Link--secondary").length == 0) {
finish_followers = true;
} else {
$("span.Link--secondary").each((i, person) =>
follower_p_data.push({
name: $(person).prev().text(),
username: $(person).text(),
profileLink: `https://github.com/${$(person).text()}`,
})
);
}
p_index++;
followers_data = await [...followers_data, ...follower_p_data];
}
return followers_data.length == 0 ? "user not found!" : followers_data;
} catch (error) {
throw error;
}
};
const fetchData = async () => {
console.log(
"Hi. I can find who unfollow you in github. please first create your followers list, \nthen send list path for me and i will find who unfollow you easily.\n"
);
const username = await getInput("Please enter your username");
return await getFollowers(username);
};
fetchData().then((data) => {
const json_data = JSON.stringify(data);
fs.writeFile(`followers.json`, json_data, "utf-8", (err) => {
if (err) console.log("Error while to save file.", err);
else console.log("Your followers list has been saved.");
});
readline.close();
});