Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite3a1060

Browse files
feature: add fetching custom problems
1 parent3a82743 commite3a1060

File tree

5 files changed

+176
-11
lines changed

5 files changed

+176
-11
lines changed

‎src/_tests/leetcode-advanced.test.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { beforeAll, describe, expect, it } from 'vitest';
33
import{Cache}from'../cache';
44
import{Credential}from'../credential';
55
import{LeetCodeAdvanced}from'../leetcode-advanced';
6+
import{SimilarQuestion}from'../leetcode-types';
67
importproblemPropertiesfrom'../problem-properties';
78

89
describe('LeetCode Advanced',{timeout:60_000*60},()=>{
@@ -32,6 +33,23 @@ describe('LeetCode Advanced', { timeout: 60_000 * 60 }, () => {
3233
expect(companyTags.length).toBeGreaterThan(0);
3334
});
3435

36+
it('should be able to get problem types',async()=>{
37+
constproblemTypes=awaitlc.getProblemTypes();
38+
console.log(problemTypes);
39+
expect(Object.keys(problemTypes).length).toBeGreaterThan(3000);
40+
});
41+
42+
it('should be able to get leetcode problems',async()=>{
43+
letcount=0;
44+
constproblems=awaitlc.getLeetcodeProblems(500,(problems)=>{
45+
count=problems.length;
46+
});
47+
expect(problems.length).toBeGreaterThan(3000);
48+
expect(count).greaterThan(3000);
49+
expect(problems[0].similarQuestionsasSimilarQuestion[]).toBeTypeOf('object');
50+
expect((problems[0].similarQuestionsasSimilarQuestion[]).length).toBeGreaterThan(0);
51+
});
52+
3553
it('should be able to get problems with property',async()=>{
3654
constproblemProperty=problemProperties.filter(
3755
({ property})=>property==='titleSlug',
@@ -50,17 +68,17 @@ describe('LeetCode Advanced', { timeout: 60_000 * 60 }, () => {
5068
expect(problems.length).toBeGreaterThan(3000);
5169
});
5270

53-
it('should be able to get detailed problems',async()=>{
54-
constproblems=awaitlc.detailedProblems({
55-
category:'',
56-
offset:0,
57-
limit:10,
58-
});
59-
expect(problems.length).equals(10);
60-
expect(problems[0].questionFrontendId).toBeTruthy();
61-
expect(problems[0].title).toBeTruthy();
62-
expect(problems[0].difficulty).toBeTruthy();
63-
});
71+
//it('should be able to get detailed problems', async () => {
72+
//const problems = await lc.detailedProblems({
73+
//category: '',
74+
//offset: 0,
75+
//limit: 10,
76+
//});
77+
//expect(problems.length).equals(10);
78+
//expect(problems[0].questionFrontendId).toBeTruthy();
79+
//expect(problems[0].title).toBeTruthy();
80+
//expect(problems[0].difficulty).toBeTruthy();
81+
//});
6482
});
6583

6684
describe('Authenticated',()=>{

‎src/graphql/custom-problem.graphql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
queryproblemsetQuestionList(
2+
$categorySlug:String
3+
$limit:Int
4+
$skip:Int
5+
$filters:QuestionListFilterInput
6+
) {
7+
problemsetQuestionList:questionList(
8+
categorySlug:$categorySlug
9+
limit:$limit
10+
skip:$skip
11+
filters:$filters
12+
) {
13+
total:totalNum
14+
questions:data {
15+
difficulty
16+
topicTags {
17+
name
18+
}
19+
companyTagStats
20+
frequency
21+
similarQuestions
22+
questionFrontendId
23+
isPaidOnly
24+
solution {
25+
url
26+
paidOnly
27+
hasVideoSolution
28+
}
29+
questionId
30+
likes
31+
dislikes
32+
stats
33+
titleSlug
34+
}
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
queryproblemsetQuestionList(
2+
$categorySlug:String
3+
$limit:Int
4+
$skip:Int
5+
$filters:QuestionListFilterInput
6+
) {
7+
problemsetQuestionList:questionList(
8+
categorySlug:$categorySlug
9+
limit:$limit
10+
skip:$skip
11+
filters:$filters
12+
) {
13+
total:totalNum
14+
questions:data {
15+
questionFrontendId
16+
}
17+
}
18+
}

‎src/leetcode-advanced.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import{cacheasdefault_cache}from'./cache';
2+
import{PROBLEM_CATEGORIES}from'./constants';
23
importCredentialfrom'./credential';
34
importCHECKINfrom'./graphql/checkin.graphql?raw';
45
importCOLLECT_EASTER_EGGfrom'./graphql/collect-easter-egg.graphql?raw';
56
importCOMPANY_TAGSfrom'./graphql/company-tags.graphql?raw';
7+
importLEETCODE_PROBLEMS_QUERYfrom'./graphql/custom-problem.graphql?raw';
68
importIS_EASTER_EGG_COLLECTEDfrom'./graphql/is-easter-egg-collected.graphql?raw';
79
importNO_OF_QUESTIONSfrom'./graphql/no-of-problems.graphql?raw';
10+
importQUESTION_FRONTEND_IDSfrom'./graphql/question-frontend-ids.graphql?raw';
811
import{LeetCode}from'./leetcode';
912
import{
1013
AllCompanyTags,
1114
CompanyTagDetail,
1215
DetailedProblem,
1316
EasterEggStatus,
17+
LeetcodeProblem,
1418
ProblemFieldDetails,
1519
QueryParams,
1620
RecentSubmission,
@@ -163,6 +167,73 @@ export class LeetCodeAdvanced extends LeetCode {
163167
returndata.problemsetQuestionList.totalasnumber;
164168
}
165169

170+
publicasyncgetProblemTypes():Promise<Record<string,string[]>>{
171+
constproblemTypes:Record<string,string[]>={};
172+
for(constcategoryofPROBLEM_CATEGORIES){
173+
const{ data}=awaitthis.graphql({
174+
query:QUESTION_FRONTEND_IDS,
175+
variables:{
176+
categorySlug:category,
177+
filters:{},
178+
skip:0,
179+
limit:100000,
180+
},
181+
});
182+
constquestions=data.problemsetQuestionList.questionsas{questionFrontendId:string}[];
183+
for(constquestionofquestions){
184+
constid=question.questionFrontendId;
185+
if(!problemTypes[id]){
186+
problemTypes[id]=[];
187+
}
188+
problemTypes[id]=[...problemTypes[id],category];
189+
}
190+
}
191+
returnproblemTypes;
192+
}
193+
194+
publicasyncgetLeetcodeProblems(
195+
limit=500,
196+
callbackFn:((problems:LeetcodeProblem[])=>void)|null=null,
197+
):Promise<LeetcodeProblem[]>{
198+
awaitthis.initialized;
199+
constnoOfProblems=awaitthis.noOfProblems();
200+
letproblems:LeetcodeProblem[]=[];
201+
for(letskip=0;skip<noOfProblems;skip+=limit){
202+
const{ data}=awaitthis.graphql({
203+
query:LEETCODE_PROBLEMS_QUERY,
204+
variables:{
205+
categorySlug:'',
206+
filters:{},
207+
skip,
208+
limit,
209+
},
210+
});
211+
constconsolidatedProblems=data.problemsetQuestionList.questionsasLeetcodeProblem[];
212+
problems=[...problems, ...consolidatedProblems];
213+
if(callbackFn){
214+
awaitcallbackFn(problems);
215+
}
216+
}
217+
returnthis.parseProblems(problems);
218+
}
219+
220+
privateasyncparseProblems(problems:LeetcodeProblem[]){
221+
constparsingDetails:Record<string,boolean>={};
222+
for(constfieldDetailsofproblemProperties){
223+
parsingDetails[fieldDetails.property]=fieldDetails.needParsing;
224+
}
225+
for(constproblemofproblems){
226+
for(constfieldofObject.keys(problem)){
227+
if(parsingDetails[field]){
228+
problem[fieldaskeyofLeetcodeProblem]=JSON.parse(
229+
problem[fieldaskeyofLeetcodeProblem]asstring,
230+
)asnever;
231+
}
232+
}
233+
}
234+
returnproblems;
235+
}
236+
166237
/**
167238
* Get list of detailed problems by tags and difficulty.
168239
* This will collect details according to the problemProperties configuration and this is slow.

‎src/leetcode-types.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ export interface QueryParams {
1010
};
1111
}
1212

13+
exportinterfaceLeetcodeProblem{
14+
difficulty:ProblemDifficulty;
15+
topicTags:TopicTag[];
16+
companyTagStats:OfficialCompanyTagStats|null;
17+
frequency:number;
18+
similarQuestions:SimilarQuestion[]|string;
19+
questionFrontendId:string;
20+
isPaidOnly:boolean;
21+
solution:LeetcodeSolution;
22+
questionId:string;
23+
likes:string;
24+
dislikes:string;
25+
stats:Stats|string;
26+
titleSlug:string;
27+
}
28+
29+
exportinterfaceLeetcodeSolution{
30+
url:string;
31+
paidOnly:boolean;
32+
hasVideoSolution:boolean;
33+
}
34+
1335
exportinterfaceDetailedProblem{
1436
allowDiscuss?:boolean;
1537
article?:Article;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp