From 0e91fac134c861c45fe7ef912170054cccb16b29 Mon Sep 17 00:00:00 2001 From: sahil <025.sahil@gmail.com> Date: Fri, 19 Sep 2025 11:37:31 +0530 Subject: [PATCH] "added /problem to new format" --- src/Controllers/fetchProblems.ts | 49 ---------------------- src/Controllers/index.ts | 1 - src/GQLQueries/index.ts | 1 - src/GQLQueries/problemList.ts | 71 ++++++++++++++++++-------------- src/app.ts | 31 ++++++++++++-- src/leetCode.ts | 16 ------- 6 files changed, 68 insertions(+), 101 deletions(-) delete mode 100644 src/Controllers/fetchProblems.ts diff --git a/src/Controllers/fetchProblems.ts b/src/Controllers/fetchProblems.ts deleted file mode 100644 index 1d70441..0000000 --- a/src/Controllers/fetchProblems.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { Response } from 'express'; -import { ProblemSetQuestionListData } from '../types'; - -const fetchProblems = async ( - options: { limit?: number; skip?: number; tags?: string; difficulty?: string}, // Mark parameters as optional - res: Response, - formatData: (data: ProblemSetQuestionListData) => {}, - query: string -) => { - try { - // Set default limit to 1 if only skip is provided - const limit = options.skip !== undefined && options.limit === undefined ? 1 : options.limit || 20; - const skip = options.skip || 0; // Default to 0 if not provided - const tags = options.tags ? options.tags.split(' ') : []; // Split tags or default to empty array - const difficulty = options.difficulty || undefined; // difficulty has to be 'EASY', 'MEDIUM' or 'HARD' - - const response = await fetch('https://leetcode.com/graphql', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - Referer: 'https://leetcode.com', - }, - body: JSON.stringify({ - query: query, - variables: { - categorySlug: '', - skip, - limit, - filters: { tags, - difficulty - }, - }, - }), - }); - console.log(response) - - const result = await response.json(); - - if (result.errors) { - return res.status(400).json(result.errors); // Return errors with a 400 status code - } - return res.json(formatData(result.data)); - } catch (err) { - console.error('Error: ', err); - return res.status(500).json({ error: 'Internal server error' }); // Return a 500 status code for server errors - } -}; - -export default fetchProblems; \ No newline at end of file diff --git a/src/Controllers/index.ts b/src/Controllers/index.ts index 2c9b9b9..5842bc2 100644 --- a/src/Controllers/index.ts +++ b/src/Controllers/index.ts @@ -1,5 +1,4 @@ export { default as fetchSingleProblem } from './fetchSingleProblem'; -export { default as fetchProblems } from './fetchProblems'; export { default as fetchUserDetails } from './fetchUserDetails'; export { default as fetchTrendingTopics } from './fetchDiscussion'; export { default as fetchDataRawFormat } from './fetchDataRawFormat'; \ No newline at end of file diff --git a/src/GQLQueries/index.ts b/src/GQLQueries/index.ts index 45f1c9c..cbe34e5 100644 --- a/src/GQLQueries/index.ts +++ b/src/GQLQueries/index.ts @@ -3,7 +3,6 @@ export { default as AcSubmissionQuery } from './recentAcSubmit'; export { default as contestQuery } from './contest'; export { default as dailyProblemQuery } from './dailyProblem'; -export { default as problemListQuery } from './problemList'; export { default as userProfileQuery } from './userProfile'; export { default as selectProblemQuery } from './selectProblem'; export { default as submissionQuery } from './recentSubmit'; diff --git a/src/GQLQueries/problemList.ts b/src/GQLQueries/problemList.ts index 761a003..a6b4e1c 100644 --- a/src/GQLQueries/problemList.ts +++ b/src/GQLQueries/problemList.ts @@ -1,31 +1,42 @@ -const query = `#graphql - query getProblems($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) { - problemsetQuestionList: questionList( - categorySlug: $categorySlug - limit: $limit - skip: $skip - filters: $filters - ) { - total: totalNum - questions: data { - acRate - difficulty - freqBar - questionFrontendId - isFavor - isPaidOnly - status - title - titleSlug - topicTags { - name - id - slug - } - hasSolution - hasVideoSolution - } +export const problemList = ` + query problemsetQuestionListV2( + $filters: QuestionFilterInput, + $limit: Int, + $searchKeyword: String, + $skip: Int, + $sortBy: QuestionSortByInput, + $categorySlug: String + ) { + problemsetQuestionListV2( + filters: $filters + limit: $limit + searchKeyword: $searchKeyword + skip: $skip + sortBy: $sortBy + categorySlug: $categorySlug + ) { + questions { + id + titleSlug + title + translatedTitle + questionFrontendId + paidOnly + difficulty + topicTags { + name + slug + nameTranslated } -}`; - -export default query; + status + isInMyFavorites + frequency + acRate + contestPoint + } + totalLength + finishedLength + hasMore + } + } +`; \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 981f5aa..2969357 100644 --- a/src/app.ts +++ b/src/app.ts @@ -5,6 +5,7 @@ import * as leetcode from './leetCode'; import { FetchUserDataRequest } from './types'; import apicache from 'apicache'; import axios from 'axios'; +import { problemList } from './GQLQueries/problemList'; import { userContestRankingInfoQuery, discussCommentsQuery, @@ -38,6 +39,7 @@ app.use((req: express.Request, _res: Response, next: NextFunction) => { }); async function queryLeetCodeAPI(query: string, variables: any) { + console.log(variables) try { const response = await axios.post(API_URL, { query, variables }); if (response.data.errors) { @@ -131,6 +133,31 @@ app.get('/officialSolution', async (req, res) => { return res.status(500).json({ error: error.message }); } }); +app.get('/problemList', async (req, res) => { + const skip = req.query.skip?req.query.skip:0; + const limit=req.query.limit?req.query.limit:20; + const searchKeyword=req.query.searchKeyword?req.query.searchKeyword:""; + const difficulties = typeof req.query.difficulties === "string" + ? req.query.difficulties.split(",") + : []; + const tags=req.query.tags?req.query.tags:"all-code-essentials" + const sortBy= { + "sortField": "CUSTOM", + "sortOrder": "ASCENDING" + } + const filters= { + "filterCombineType": "ALL", + "difficultyFilter": { + "difficulties": difficulties, + "operator": "IS" + }}; + try { + const data = await queryLeetCodeAPI(problemList, { skip ,limit,searchKeyword,filters,categorySlug:tags,sortBy}); + return res.json(data); + } catch (error) { + return res.status(500).json({ error: error.message }); + } +}); app.get('/userProfileCalendar', async (req, res) => { const { username, year } = req.query; @@ -242,10 +269,6 @@ app.get('/daily', leetcode.dailyProblem); //get the selected question app.get('/select', leetcode.selectProblem); - -//get list of problems -app.get('/problems', leetcode.problems); - //get 20 trending Discuss app.get('/trendingDiscuss', leetcode.trendingCategoryTopics); diff --git a/src/leetCode.ts b/src/leetCode.ts index 70a9840..132de92 100644 --- a/src/leetCode.ts +++ b/src/leetCode.ts @@ -113,22 +113,6 @@ export const selectProblem = (req: Request, res: Response) => { } }; -export const problems = ( - req: Request<{}, {}, {}, { limit: number; skip: number; tags: string; difficulty: string }>, - res: Response -) => { - const difficulty=req.query.difficulty; - const limit = req.query.limit; - const skip = req.query.skip; - const tags = req.query.tags; - - controllers.fetchProblems( - { limit, skip, tags, difficulty }, - res, - formatUtils.formatProblemsData, - gqlQueries.problemListQuery - ); -}; export const trendingCategoryTopics = (_req: Request, res: Response) => {