-
Notifications
You must be signed in to change notification settings - Fork 21
/
calc.js
112 lines (101 loc) · 2.91 KB
/
calc.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
// Calculate Score
const axios = require("axios");
const getProfile = async (username = "iampavangandhi") => {
try {
const { data } = await axios.get(
`https://api.github.com/users/${username}`
);
const score = await calculateScore(data);
const user_orgs = await userOrgs(data?.login);
const { totalRepoStars: repo_stars, totalRepoForks: repo_forks } =
await getRepoStarsAndForks(data?.login);
let myProfile = {
avatar: data?.avatar_url,
username: data?.login,
name: data?.name,
public_repos: data?.public_repos,
repo_stars: repo_stars,
repo_forks: repo_forks,
followers: data?.followers,
user_orgs: user_orgs,
score: score,
url: data?.html_url,
};
return myProfile;
} catch (error) {
console.log("Error in getProfile");
throw new Error(error);
}
};
// Calculate Profile Score
const calculateScore = async (profile) => {
try {
const star = await staredRepos(profile?.login);
const org = await userOrgs(profile?.login);
const { totalRepoStars: repo_stars, totalRepoForks: repo_forks } =
await getRepoStarsAndForks(data?.login);
const orgs = Number(org) * 50;
const stars = Number(star) * 5;
const repoStarsScore = Number(repo_stars) * 5;
const repoForksScore = Number(repo_forks) * 5;
const followers = Number(profile?.followers) * 15;
const publicRepos = Number(profile?.public_repos) * 10;
const publicGists = Number(profile?.public_gists) * 5;
const score =
stars +
orgs +
publicRepos +
publicGists +
repoStarsScore +
repoForksScore +
followers;
return score;
} catch (error) {
console.log("Error in calculateScore");
throw new Error(error);
}
};
// Calculate Orgs
const userOrgs = async (profile) => {
try {
const orgs = await axios.get(
`https://api.github.com/users/${profile}/orgs`
);
return orgs?.data?.length;
} catch (error) {
console.log("Error in userOrgs");
throw new Error(error);
}
};
// Calculate Stared Repos
const staredRepos = async (profile) => {
try {
const star = await axios.get(
`https://api.github.com/users/${profile}/starred`
);
return star.data?.length;
} catch (error) {
console.log("Error in staredRepos");
throw new Error(error);
}
};
// Calculate Repo Stars & Forks
const getRepoStarsAndForks = async (profile) => {
try {
let totalRepoStars = 0;
let totalRepoForks = 0;
const userRepos = await axios.get(
`https://api.github.com/users/${profile}/repos?per_page=500&type=all`
);
userRepos?.forEach((repo) => {
totalRepoStars += Number(repo?.stargazers_count);
totalRepoForks += Number(repo?.forks_count);
});
return { totalRepoStars, totalRepoForks };
} catch (error) {
console.log("Error in getRepoStarsAndForks");
throw new Error(error);
}
};
// Get Profile
module.exports = getProfile;