Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change lf feature and update docs #27

Merged
merged 16 commits into from
Mar 11, 2024
10 changes: 10 additions & 0 deletions common/utils/question-getter/getPlanQuestionList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { graphql } from '#common/utils/http/graphql.js'
import { getPlanQuestionListJson } from '#resources/headers/planQuestionListJson.js'

export async function getPlanQuestionList(slug) {
const res = await graphql(getPlanQuestionListJson(slug))
const {
data: { studyPlanV2Detail }
} = res
return studyPlanV2Detail
}
10 changes: 0 additions & 10 deletions common/utils/question-getter/getQuestionListHot100.js

This file was deleted.

10 changes: 10 additions & 0 deletions common/utils/question-getter/getStudyPlanList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { graphql } from '#common/utils/http/graphql.js'
import { getStudyPlanListJson } from '#resources/headers/studyPlanListJson.js'

export async function getStudyPlanList() {
const res = await graphql(getStudyPlanListJson())
const {
data: { studyPlansV2AdQuestionPage }
} = res
return studyPlansV2AdQuestionPage
}
4 changes: 2 additions & 2 deletions common/utils/question-handler/getHot100QuestionListCode.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'node:path'
import { createQuestionByTitleSlug } from '../create-check/createUtil.js'
import { getQuestionListHot100 } from '#common/utils/question-getter/getQuestionListHot100.js'
import { getPlanQuestionList } from '#common/utils/question-getter/getPlanQuestionList.js'

// 获取题目列表
export async function getTitleSlugList() {
const res = await getQuestionListHot100()
const res = await getPlanQuestionList()
const { planSubGroups } = res
return planSubGroups.reduce((acc, cur) => {
acc.push(...cur.questions.map((res) => res.titleSlug))
Expand Down
53 changes: 35 additions & 18 deletions common/view/finder.view.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,58 @@
import select from '@inquirer/select'
import input from '@inquirer/input'

import {
getHot100QuestionListCode,
getTitleSlugList
} from '#common/utils/question-handler/getHot100QuestionListCode.js'
// import { getHot100QuestionListCode } from '#common/utils/question-handler/getHot100QuestionListCode.js'
import {
createQuestionById,
createQuestionByTitleSlug
} from '#common/utils/create-check/createUtil.js'
import { getQuestionByKeyword } from '#common/utils/question-getter/getQuestionByKeyword.js'
import { getStudyPlanList } from '#common/utils/question-getter/getStudyPlanList.js'
import { getPlanQuestionList } from '#common/utils/question-getter/getPlanQuestionList.js'

async function hotMode(baseDir = process.cwd()) {
async function studyMode(baseDir = process.cwd()) {
const questionList = await getStudyPlanList()
const planListMode = {
message: '请选择学习计划',
choices: questionList.map((item) => ({
name: `${item.name}${item.premiumOnly ? '(VIP)' : ''}`,
value: item.slug
}))
}
const planSlug = await select(planListMode)
const createMode = await select({
message: '拉题模式',
choices: [
{ name: '单个选择', value: 'single' },
{ name: '全部拉取', value: 'all' }
{ name: '全部拉取(暂不支持)', value: 'all' }
]
})
if (createMode === 'single') {
const titleSlugList = await getTitleSlugList()
const { planSubGroups } = await getPlanQuestionList(planSlug)
const planList = planSubGroups.reduce((acc, cur) => {
acc.push(
...cur.questions.map((res) => {
return {
cnTitle: res.translatedTitle,
enTitle: res.titleSlug
}
})
)
return acc
}, [])
const singleMode = {
message: '请选择题目?',
choices: titleSlugList.map((res) => ({
name: res,
value: res
choices: planList.map((res) => ({
name: res.cnTitle,
value: res.enTitle
}))
}
const singleChoice = await select(singleMode)

await createQuestionByTitleSlug(singleChoice, baseDir)
}
if (createMode === 'all') await getHot100QuestionListCode()
if (createMode === 'all') console.log('暂不支持')
// await getHot100QuestionListCode()
}

async function keywordMode(baseDir = process.cwd()) {
Expand All @@ -54,19 +74,16 @@ async function keywordMode(baseDir = process.cwd()) {
console.log(chooseQuestion)
await createQuestionById(chooseQuestion, baseDir)
}

async function selectMode(baseDir) {
console.log(baseDir)
}

export async function easyFinderView(baseDir = process.cwd()) {
const choices = [
{ name: '关键词搜索', value: 'keyword', description: '关键词描述' },
{
name: 'hot 100列表查询',
value: 'hot',
description: '最受欢迎的100道题目'
}
// { name: '筛选模式', value: 'select', description: '筛选题目' }
{ name: '学习计划', value: 'study', description: '企业和经典面试题目列表' },
{ name: '筛选模式', value: 'select', description: '筛选题目' }
]

const modeQuestion = {
Expand All @@ -76,7 +93,7 @@ export async function easyFinderView(baseDir = process.cwd()) {
const mode = await select(modeQuestion)

const modeMap = {
hot: hotMode,
study: studyMode,
keyword: keywordMode,
select: selectMode
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* 获取hot 100 的请求header
* @returns {{headers: {"content-type": string}, method: string, body: string}}
*/
export function getQuestionListHot100Json() {
export function getPlanQuestionListJson(slug) {
return {
headers: { 'content-type': 'application/json' },
body: '{"query":"\\n query studyPlanPastSolved($slug: String!) {\\n studyPlanV2Detail(planSlug: $slug) {\\n planSubGroups {\\n slug\\n questions {\\n titleSlug\\n status\\n }\\n }\\n }\\n}\\n ","variables":{"slug":"top-100-liked"},"operationName":"studyPlanPastSolved"}',
body: `{"query":"\\n query studyPlanPastSolved($slug: String!) {\\n studyPlanV2Detail(planSlug: $slug) {\\n planSubGroups {\\n slug\\n questions {\\n titleSlug\\n translatedTitle\\n status\\n }\\n }\\n }\\n}\\n ","variables":{"slug":"${slug}"},"operationName":"studyPlanPastSolved"}`,
method: 'POST'
}
}
7 changes: 7 additions & 0 deletions resources/headers/studyPlanListJson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function getStudyPlanListJson() {
return {
headers: { 'content-type': 'application/json' },
body: '{"query":"\\n query GetProblemSetStudyPlanAds {\\n studyPlansV2AdQuestionPage {\\n cover\\n highlight\\n name\\n onGoing\\n premiumOnly\\n questionNum\\n slug\\n }\\n}\\n ","variables":{},"operationName":"GetProblemSetStudyPlanAds"}',
method: 'POST'
}
}
2 changes: 1 addition & 1 deletion test/getHot100List.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
import { getQuestionListHot100 } from '#common/utils/question-getter/getQuestionListHot100.js'
import { getQuestionListHot100 } from '#common/utils/question-getter/getPlanQuestionList.js'

describe('hot 100获取数据测试', () => {
it('是否正确获取了hot 100合集', async () => {
Expand Down
Loading