diff --git a/internal/leetcode/base.go b/internal/leetcode/base.go index bbd30e1ac..dd6a7d2b0 100644 --- a/internal/leetcode/base.go +++ b/internal/leetcode/base.go @@ -9,6 +9,7 @@ import ( "os" "os/user" "path" + "strings" "time" "github.com/openset/leetcode/internal/base" @@ -112,3 +113,7 @@ func getCredential() (data url.Values) { jsonDecode(cts, &data) return } + +func slugToSnake(slug string) string { + return strings.Replace(slug, "-", "_", -1) +} diff --git a/internal/leetcode/config.go b/internal/leetcode/config.go index 666ad4087..79ef09c0b 100644 --- a/internal/leetcode/config.go +++ b/internal/leetcode/config.go @@ -22,6 +22,8 @@ const ( problemsDatabaseFile = "problems_database.json" problemsShellFile = "problems_shell.json" questionTranslationFile = "question_translation.json" + questionDataFile = "question_data_%s.json" questionArticleFile = "question_article_%s.html" + topicTagFile = "topic_tag_%s.json" tagsFile = "tag/tags.json" ) diff --git a/internal/leetcode/problems_all.go b/internal/leetcode/problems_all.go index 74c422dd9..ffc9013c3 100644 --- a/internal/leetcode/problems_all.go +++ b/internal/leetcode/problems_all.go @@ -3,7 +3,6 @@ package leetcode import ( "fmt" "sort" - "strings" "github.com/openset/leetcode/internal/client" ) @@ -69,7 +68,7 @@ func (p paidType) Str() string { } func (s statType) QuestionTitleSnake() string { - return strings.Replace(s.QuestionTitleSlug, "-", "_", -1) + return slugToSnake(s.QuestionTitleSlug) } func (s statType) TranslationTitle() string { diff --git a/internal/leetcode/question_article.go b/internal/leetcode/question_article.go index 72247154d..184404aa5 100644 --- a/internal/leetcode/question_article.go +++ b/internal/leetcode/question_article.go @@ -4,14 +4,13 @@ import ( "bytes" "fmt" "regexp" - "strings" "github.com/openset/leetcode/internal/client" ) func GetDescription(articleSlug string) string { fmt.Println("\tquestion article", "saving...") - filename := fmt.Sprintf(questionArticleFile, strings.Replace(articleSlug, "-", "_", -1)) + filename := fmt.Sprintf(questionArticleFile, slugToSnake(articleSlug)) html := remember(filename, 30, func() []byte { return client.Get(fmt.Sprintf(questionArticleUrl, articleSlug)) }) diff --git a/internal/leetcode/question_data.go b/internal/leetcode/question_data.go index def43975c..af374b8e6 100644 --- a/internal/leetcode/question_data.go +++ b/internal/leetcode/question_data.go @@ -19,7 +19,7 @@ func QuestionData(titleSlug string) (qd questionDataType) { }, "query": "query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n boundTopicId\n title\n titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n contributors {\n username\n profileUrl\n avatarUrl\n __typename\n }\n langToValidPlayground\n topicTags {\n name\n slug\n translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n lang\n langSlug\n code\n __typename\n }\n stats\n hints\n solution {\n id\n canSeeDetail\n __typename\n }\n status\n sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n enableRunCode\n enableTestMode\n envInfo\n __typename\n }\n}\n" }` - filename := "question_data_" + strings.Replace(titleSlug, "-", "_", -1) + ".json" + filename := fmt.Sprintf(questionDataFile, slugToSnake(titleSlug)) graphQLRequest(filename, 30, jsonStr, &qd) if qd.Data.Question.TitleSlug == "" { os.Remove(getCachePath(filename)) @@ -177,7 +177,7 @@ func (question questionType) getFilePath(filename string) string { } func (question questionType) TitleSnake() string { - return strings.Replace(question.TitleSlug, "-", "_", -1) + return slugToSnake(question.TitleSlug) } func (question questionType) PackageName() string { diff --git a/internal/leetcode/tag.go b/internal/leetcode/topic_tag.go similarity index 98% rename from internal/leetcode/tag.go rename to internal/leetcode/topic_tag.go index d45526eb2..d39a74916 100644 --- a/internal/leetcode/tag.go +++ b/internal/leetcode/topic_tag.go @@ -7,7 +7,6 @@ import ( "regexp" "sort" "strconv" - "strings" "github.com/openset/leetcode/internal/client" ) @@ -68,7 +67,7 @@ func GetTopicTag(slug string) (tt topicTagType) { }, "query": "query getTopicTag($slug: String!) {\n topicTag(slug: $slug) {\n name\n translatedName\n questions {\n status\n questionId\n questionFrontendId\n title\n titleSlug\n translatedTitle\n stats\n difficulty\n isPaidOnly\n topicTags {\n name\n translatedName\n slug\n __typename\n }\n companyTags {\n name\n translatedName\n slug\n __typename\n }\n __typename\n }\n frequencies\n __typename\n }\n favoritesLists {\n publicFavorites {\n ...favoriteFields\n __typename\n }\n privateFavorites {\n ...favoriteFields\n __typename\n }\n __typename\n }\n}\n\nfragment favoriteFields on FavoriteNode {\n idHash\n id\n name\n isPublicFavorite\n viewCount\n creator\n isWatched\n questions {\n questionId\n title\n titleSlug\n __typename\n }\n __typename\n}\n" }` - filename := "topic_tag_" + strings.Replace(slug, "-", "_", -1) + ".json" + filename := fmt.Sprintf(topicTagFile, slugToSnake(slug)) graphQLRequest(filename, 3, jsonStr, &tt) return }