-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
94 lines (84 loc) · 2.8 KB
/
helper.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
async function fetchNodeFetch() {
const fetch = (await import('node-fetch')).default;
return fetch;
}
async function get_online_username(username) {
try {
const fetch = await fetchNodeFetch();
const url = "https://leetcode.com/graphql";
const query = `
query getUserProfile($username: String!) {
matchedUser(username: $username) {
username
submitStats: submitStatsGlobal {
acSubmissionNum {
difficulty
count
submissions
}
}
}
}
`;
const vars = { username };
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, variables: vars }),
});
if (response.ok) {
const data = await response.json();
console.log(data);
return data.data.matchedUser;
} else {
const errorMsg = await response.text();
console.log(`Error occurred: ${errorMsg}`);
return `Error occurred: ${errorMsg}`;
}
} catch (error) {
return `Request failed: ${error.message}`;
}
}
async function get_difficulty(link) {
try {
const fetch = await fetchNodeFetch();
const titleSlug = link.split('/problems/')[1].split('/')[0];
const url = "https://leetcode.com/graphql";
const query = `
query getProblemDetails($titleSlug: String!) {
question(titleSlug: $titleSlug) {
title
difficulty
}
}`;
const vars = { titleSlug };
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, variables: vars }),
});
if (response.ok) {
const data = await response.json();
const difficulty = data.data.question.difficulty;
return difficulty;
} else {
const errorMsg = await response.text();
return `Error occurred: ${errorMsg}`;
}
} catch (error) {
return error;
}
}
function extractProblem(link) {
try {
if (link.includes('/submissions') && link.includes('/problems/')) {
const problem = link.split('/problems/')[1].split('/submissions')[0].replace(/-/g, ' ');
return problem;
} else {
return null;
}
} catch (error) {
return error;
}
}
module.exports = { extractProblem, get_difficulty, get_online_username };