-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
281 lines (245 loc) · 7.8 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// console.log("hello");
var base = "https://codeforces.com/api/user.info?handles=";
const search = document.getElementById("search");
const profile = document.getElementById("profile");
const form = document.getElementById("form");
const button = document.getElementById("btn1");
var api_url;
const btnPlaces = document.querySelectorAll('.search-btn');
for (let i = 0; i < btnPlaces.length; i++) {
btnPlaces[i].addEventListener("click", function(e) {
let prevBtn = document.querySelector(".checked");
if (prevBtn) {
prevBtn.classList.remove("checked");
e.target.classList.add("checked");
} else {
e.target.classList.add("checked");
}
});
}
function getUser(user) {
api_url = base+user;
// console.log(api_url);
getapi(api_url);
}
// Defining async function
async function getapi(url) {
// Storing response
const response = await fetch(url);
if(response.status!=200)
{
showEmpty(`Search Profiles.....`);
}
// Storing data in form of JSON
else{
var data = await response.json();
if(data.status=='FAILED')
{
showEmpty(data.comment.slice(9));
}
if(base=="https://codeforces.com/api/user.info?handles=")
codeforcesUserCard(data);
else if(base=="https://api.github.com/users/")
githubUserCard(data),
getRepos(url);
else if(base=="https://codechef-api.vercel.app/")
codechefUserCard(data);
}
}
// Calling that async function
getapi(api_url);
function showEmpty(msg)
{
const errorCard = `<div class="errorCard col mt-5 d-flex justify-content-center"><h1>${msg}</h1></div>`;
profile.innerHTML = errorCard;
}
function getUserId(platform)
{
if(platform=='github')
{
base = "https://api.github.com/users/";
search.placeholder = "Enter your Github handle";
}
if(platform=='codeforces')
{
base = "https://codeforces.com/api/user.info?handles=";
search.placeholder = "Enter your Codeforces handle";
}
if(platform=='codechef')
{
base = "https://codechef-api.vercel.app/";
search.placeholder = "Enter your Codechef handle";
}
}
async function getRepos(username) {
const response = await fetch(api_url+'/repos?sort=created');
var data = await response.json();
addReposToCard(data);
}
function githubUserCard(user) {
// console.log(user);
const userID = user.name || user.login
const userBio = user.bio ? `<p>${user.bio}</p>` : ''
const avatar = user.avatar_url;
// console.log(avatar);
const userCard = `
<div class="userCard my-2 ">
<div class="avatar col d-flex justify-content-center ">
<img src= "${avatar}" alt="${userID}" >
</div>
<div class="details col d-flex justify-content-center">
<h2>${userID}</h2>
</div>
<div class="col d-flex justify-content-center">
${userBio}
</div>
<div class="col d-flex justify-content-center">
<ul style="list-decoration:none;" class="d-flex justify-content-evenly ">
<li class=" mx-2">${user.followers} <strong>Followers</strong></li>
<li class=" mx-2">${user.following} <strong>Following</strong></li>
<li class=" mx-2">${user.public_repos} <strong>Public Repositories</strong></li>
</ul>
</div>
<div class=" col d-flex justify-content-center">
<button class="profile-link"><a href=${user.html_url} target="_blank">Profile</a></button>
</div>
</div>
</div>
`;
profile.innerHTML = userCard;
}
function codeforcesUserCard(data)
{
const userName = data.result[0].handle;
// console.log(data);
var avatar = data.result[0].titlePhoto;
var rank = data.result[0].rank;
var rankColor="black";
if(rank=="pupil")
{
rankColor="#008000";
}
else if(rank=="newbie")
{
rankColor="#808080";
}
else if(rank=="specialist")
{
rankColor="#03a89e";
}
else if(rank=="expert")
{
rankColor="#0000ff";
}
else if(rank=="legendary grandmaster")
{
rankColor="#ff0000";
}
else if(rank=="international master")
{
rankColor="#ff8c00";
}
else if(rank=="candidate master")
{
rankColor="#a0a";
}
else if(rank=="master")
{
rankColor="#ff8c00";
}
else if(rank=="Grandmaster")
{
rankColor="#ff0000";
}
var pro = `https://codeforces.com/profile/${userName}`;
var curr = data.result[0].rating;
const userCard = `
<div class="userCard row-sm">
<div class="avatar col d-flex justify-content-center ">
<img src= "${avatar}" alt="${userName}" >
</div>
<div class="details col d-flex justify-content-center">
<h2 > ${userName} </h2>
<h5 id="rank" style="color:${rankColor}"> ${rank}</h5>
</div>
<div class="col d-flex justify-content-center">
<p> <strong>Ratings</strong>: <strong style="color:red; font-size:20px;"> ${data.result[0].maxRating}</strong>(max) / ${curr}(curr)</p>
</div>
<div class=" col d-flex justify-content-center">
<button class="profile-link"><a href=${pro} target="_blank">Profile</a></button>
</div>
</div>
</div>
`;
profile.innerHTML = userCard;
}
function codechefUserCard(data)
{
// console.log(data);
const username = data.user_details.username;
// console.log(username);
const rating = data.rating;
// console.log(rating);
const name =data.name;
const userCard = `
<div class="userCard row-sm">
<div class="avatar col d-flex justify-content-center ">
<img src= "images/codechef.jpg" alt="${data.user_details.name}" >
</div>
<div class="details col d-flex justify-content-center">
<h2 > ${data.user_details.username} </h2>
<h4 style="color:blue;"> ${data.stars}</h4>
</div>
<div class="details col d-flex justify-content-center">
<ul>
<li class="d-flex justify-content-center">Rating: <strong> ${data.rating} </strong></li>
<li class="d-flex justify-content-center">Problems Fully Solved: <strong> ${data.fully_solved.count}</strong></li>
<li class="d-flex justify-content-center">Name: ${data.user_details.name}</li>
</ul>
</div>
</div>
</div>
`;
profile.innerHTML = userCard;
}
function addReposToCard(repos) {
const reposEl = document.getElementById('repos');
repos
.slice(0, 5)
.forEach(repo => {
const repoEl = document.createElement('a')
repoEl.classList.add('repo')
repoEl.href = repo.html_url
repoEl.target = '_blank'
repoEl.innerText = repo.name
reposEl.appendChild(repoEl);
})
}
form.addEventListener("submit", (e) => {
e.preventDefault();
const user = search.value;
// console.log(user);
if(user)
{
getUser(user);
search.value='';
}
});
function changeBg(color)
{
if(color=='green')
{
document.body.style.backgroundColor = '#19ffabc3';
}
else if(color=='red')
{
document.body.style.backgroundColor = 'paleturquoise';
}
else if(color=='yellow')
{
document.body.style.backgroundColor = '#F4DB7D';
}
else{
document.body.style.backgroundColor = '#C39EA0';
}
}