Skip to content

Commit 608e588

Browse files
author
openset
committed
Add: slugToSnake
1 parent 2d50949 commit 608e588

File tree

6 files changed

+12
-8
lines changed

6 files changed

+12
-8
lines changed

internal/leetcode/base.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/user"
1111
"path"
12+
"strings"
1213
"time"
1314

1415
"github.com/openset/leetcode/internal/base"
@@ -112,3 +113,7 @@ func getCredential() (data url.Values) {
112113
jsonDecode(cts, &data)
113114
return
114115
}
116+
117+
func slugToSnake(slug string) string {
118+
return strings.Replace(slug, "-", "_", -1)
119+
}

internal/leetcode/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const (
2222
problemsDatabaseFile = "problems_database.json"
2323
problemsShellFile = "problems_shell.json"
2424
questionTranslationFile = "question_translation.json"
25+
questionDataFile = "question_data_%s.json"
2526
questionArticleFile = "question_article_%s.html"
27+
topicTagFile = "topic_tag_%s.json"
2628
tagsFile = "tag/tags.json"
2729
)

internal/leetcode/problems_all.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package leetcode
33
import (
44
"fmt"
55
"sort"
6-
"strings"
76

87
"github.com/openset/leetcode/internal/client"
98
)
@@ -69,7 +68,7 @@ func (p paidType) Str() string {
6968
}
7069

7170
func (s statType) QuestionTitleSnake() string {
72-
return strings.Replace(s.QuestionTitleSlug, "-", "_", -1)
71+
return slugToSnake(s.QuestionTitleSlug)
7372
}
7473

7574
func (s statType) TranslationTitle() string {

internal/leetcode/question_article.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"bytes"
55
"fmt"
66
"regexp"
7-
"strings"
87

98
"github.com/openset/leetcode/internal/client"
109
)
1110

1211
func GetDescription(articleSlug string) string {
1312
fmt.Println("\tquestion article", "saving...")
14-
filename := fmt.Sprintf(questionArticleFile, strings.Replace(articleSlug, "-", "_", -1))
13+
filename := fmt.Sprintf(questionArticleFile, slugToSnake(articleSlug))
1514
html := remember(filename, 30, func() []byte {
1615
return client.Get(fmt.Sprintf(questionArticleUrl, articleSlug))
1716
})

internal/leetcode/question_data.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func QuestionData(titleSlug string) (qd questionDataType) {
1919
},
2020
"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"
2121
}`
22-
filename := "question_data_" + strings.Replace(titleSlug, "-", "_", -1) + ".json"
22+
filename := fmt.Sprintf(questionDataFile, slugToSnake(titleSlug))
2323
graphQLRequest(filename, 30, jsonStr, &qd)
2424
if qd.Data.Question.TitleSlug == "" {
2525
os.Remove(getCachePath(filename))
@@ -177,7 +177,7 @@ func (question questionType) getFilePath(filename string) string {
177177
}
178178

179179
func (question questionType) TitleSnake() string {
180-
return strings.Replace(question.TitleSlug, "-", "_", -1)
180+
return slugToSnake(question.TitleSlug)
181181
}
182182

183183
func (question questionType) PackageName() string {

internal/leetcode/tag.go renamed to internal/leetcode/topic_tag.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"regexp"
88
"sort"
99
"strconv"
10-
"strings"
1110

1211
"github.com/openset/leetcode/internal/client"
1312
)
@@ -68,7 +67,7 @@ func GetTopicTag(slug string) (tt topicTagType) {
6867
},
6968
"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"
7069
}`
71-
filename := "topic_tag_" + strings.Replace(slug, "-", "_", -1) + ".json"
70+
filename := fmt.Sprintf(topicTagFile, slugToSnake(slug))
7271
graphQLRequest(filename, 3, jsonStr, &tt)
7372
return
7473
}

0 commit comments

Comments
 (0)