From 1c87cc2195377e68a53ea1b92cf9c7d68e02bfea Mon Sep 17 00:00:00 2001 From: openset Date: Fri, 13 Sep 2019 16:08:19 +0800 Subject: [PATCH 1/3] Add: 1.4.5 --- internal/base/base.go | 3 +-- internal/description/description.go | 33 ++++++++++++++---------- internal/leetcode/base.go | 4 +-- internal/leetcode/problems_all.go | 10 ++++---- internal/leetcode/question_data.go | 40 ++++++++++++++--------------- internal/leetcode/topic_tag.go | 12 ++++----- internal/post/post.go | 10 ++++---- internal/readme/readme.go | 4 +-- internal/version/version.go | 2 +- 9 files changed, 62 insertions(+), 56 deletions(-) diff --git a/internal/base/base.go b/internal/base/base.go index 10fa1ed8f..35ade717c 100644 --- a/internal/base/base.go +++ b/internal/base/base.go @@ -7,7 +7,6 @@ import ( "io/ioutil" "log" "os" - "path" "path/filepath" "strings" "sync" @@ -86,7 +85,7 @@ func JsonIndent(src []byte) []byte { } func getFilePath(filename string) string { - if dir := path.Dir(filename); dir != "" { + if dir := filepath.Dir(filename); dir != "" { if err := os.MkdirAll(dir, 0755); err != nil { CheckErr(err) } diff --git a/internal/description/description.go b/internal/description/description.go index d5351c35b..f9770bdf6 100644 --- a/internal/description/description.go +++ b/internal/description/description.go @@ -20,22 +20,29 @@ func runDescription(cmd *base.Command, args []string) { cmd.Usage() } var wg sync.WaitGroup - tokens := make(chan bool, 1<<7) + limit := 1 << 7 + jobs := make(chan *leetcode.StatStatusPairsType, limit) + for i := 0; i < limit; i++ { + go worker(jobs, &wg) + } problems := leetcode.ProblemsAll() for _, problem := range problems.StatStatusPairs { - wg.Add(1) - tokens <- true + problem := problem fmt.Println(problem.Stat.FrontendQuestionId, "\t"+problem.Stat.QuestionTitle) - go func(problem leetcode.StatStatusPairsType) { - titleSlug := problem.Stat.QuestionTitleSlug - question := leetcode.QuestionData(titleSlug, false).Data.Question - if question.Content == "" && problem.PaidOnly == true && problem.Stat.QuestionArticleLive { - question.Content = leetcode.GetDescription(problem.Stat.QuestionArticleSlug) - } - question.SaveContent() - <-tokens - wg.Done() - }(problem) + wg.Add(1) + jobs <- &problem } wg.Wait() } + +func worker(jobs <-chan *leetcode.StatStatusPairsType, wg *sync.WaitGroup) { + for problem := range jobs { + titleSlug := problem.Stat.QuestionTitleSlug + question := leetcode.QuestionData(titleSlug, false).Data.Question + if question.Content == "" && problem.PaidOnly == true && problem.Stat.QuestionArticleLive { + question.Content = leetcode.GetDescription(problem.Stat.QuestionArticleSlug) + } + question.SaveContent() + wg.Done() + } +} diff --git a/internal/leetcode/base.go b/internal/leetcode/base.go index d9c5068d5..d69579870 100644 --- a/internal/leetcode/base.go +++ b/internal/leetcode/base.go @@ -6,7 +6,7 @@ import ( "net/http" "net/url" "os" - "path" + "path/filepath" "strings" "time" @@ -60,7 +60,7 @@ func getCachePath(f string) string { dir, err = os.UserCacheDir() checkErr(err) } - return path.Join(dir, ".leetcode", f) + return filepath.Join(dir, ".leetcode", f) } func Clean() { diff --git a/internal/leetcode/problems_all.go b/internal/leetcode/problems_all.go index 09f2666a6..737bab5c9 100644 --- a/internal/leetcode/problems_all.go +++ b/internal/leetcode/problems_all.go @@ -62,7 +62,7 @@ type difficultyType struct { type paidType bool -func (problem StatStatusPairsType) WriteRow(buf *bytes.Buffer) { +func (problem *StatStatusPairsType) WriteRow(buf *bytes.Buffer) { format := "| %d | [%s](https://leetcode.com/problems/%s%s)%s | [%s](https://github.com/openset/leetcode/tree/master/problems/%s) | %s |\n" id := problem.Stat.FrontendQuestionId stat := problem.Stat @@ -79,11 +79,11 @@ func (p paidType) Str() string { return "" } -func (s statType) QuestionTitleSnake() string { +func (s *statType) QuestionTitleSnake() string { return slugToSnake(s.QuestionTitleSlug) } -func (s statType) TranslationTitle() string { +func (s *statType) TranslationTitle() string { title := translationSet[s.QuestionId] if title != "" { title = fmt.Sprintf(` "%s"`, title) @@ -91,11 +91,11 @@ func (s statType) TranslationTitle() string { return title } -func (s statType) Lang() string { +func (s *statType) Lang() string { return getLang(s.QuestionTitleSlug) } -func (d difficultyType) LevelName() string { +func (d *difficultyType) LevelName() string { m := map[int]string{ 1: "Easy", 2: "Medium", diff --git a/internal/leetcode/question_data.go b/internal/leetcode/question_data.go index ac378f251..c43d7cfce 100644 --- a/internal/leetcode/question_data.go +++ b/internal/leetcode/question_data.go @@ -5,7 +5,7 @@ import ( "fmt" "log" "os" - "path" + "path/filepath" "regexp" "strconv" "strings" @@ -102,14 +102,14 @@ func (d difficultyStrType) Str() (s string) { return } -func (question questionType) SaveContent() { +func (question *questionType) SaveContent() { if question.TitleSlug != "" { filePutContents(question.getFilePath("README.md"), question.getDescContent()) question.saveMysqlSchemas() } } -func (question questionType) getDescContent() []byte { +func (question *questionType) getDescContent() []byte { var buf bytes.Buffer buf.WriteString(authInfo("description")) buf.WriteString(question.getNavigation()) @@ -127,7 +127,7 @@ func (question questionType) getDescContent() []byte { return buf.Bytes() } -func (question questionType) getNavigation() string { +func (question *questionType) getNavigation() string { nav, pre, next := "\n%s\n%s\n%s\n", "< Previous", "Next >" problems := ProblemsAll().StatStatusPairs if questionId, err := strconv.Atoi(question.QuestionId); err == nil { @@ -147,7 +147,7 @@ func (question questionType) getNavigation() string { return fmt.Sprintf(nav, pre, strings.Repeat(" ", 16), next) } -func (question questionType) getTopicTags() []byte { +func (question *questionType) getTopicTags() []byte { tags := question.TopicTags var buf bytes.Buffer if len(tags) > 0 { @@ -160,12 +160,12 @@ func (question questionType) getTopicTags() []byte { return buf.Bytes() } -func (question questionType) GetSimilarQuestion() (sq []similarQuestionType) { +func (question *questionType) GetSimilarQuestion() (sq []similarQuestionType) { jsonDecode([]byte(question.SimilarQuestions), &sq) return } -func (question questionType) getSimilarQuestion() []byte { +func (question *questionType) getSimilarQuestion() []byte { sq := question.GetSimilarQuestion() var buf bytes.Buffer if len(sq) > 0 { @@ -178,7 +178,7 @@ func (question questionType) getSimilarQuestion() []byte { return buf.Bytes() } -func (question questionType) getHints() []byte { +func (question *questionType) getHints() []byte { hints := question.Hints var buf bytes.Buffer if len(hints) > 0 { @@ -190,15 +190,15 @@ func (question questionType) getHints() []byte { return buf.Bytes() } -func (question questionType) getFilePath(filename string) string { - return path.Join("problems", question.TitleSlug, filename) +func (question *questionType) getFilePath(filename string) string { + return filepath.Join("problems", question.TitleSlug, filename) } -func (question questionType) TitleSnake() string { +func (question *questionType) TitleSnake() string { return slugToSnake(question.TitleSlug) } -func (question questionType) PackageName() string { +func (question *questionType) PackageName() string { snake := question.TitleSnake() if snake != "" && unicode.IsNumber(rune(snake[0])) { snake = "p_" + snake @@ -206,13 +206,13 @@ func (question questionType) PackageName() string { return snake } -func (question questionType) SaveCodeSnippet() { +func (question *questionType) SaveCodeSnippet() { if isLangMySQL(question.TitleSlug) { filePutContents(question.getFilePath(question.TitleSnake()+".sql"), []byte("# Write your MySQL query statement below\n")) } langSupport := [...]struct { slug string - handle func(questionType, codeSnippetsType) + handle func(*questionType, codeSnippetsType) }{ {"golang", handleCodeGolang}, {"python3", handleCodePython}, @@ -234,7 +234,7 @@ func (question questionType) SaveCodeSnippet() { } } -func (question questionType) saveCodeContent(content, ext string, permX ...bool) { +func (question *questionType) saveCodeContent(content, ext string, permX ...bool) { filePath := question.getFilePath(question.TitleSnake() + ext) filePutContents(filePath, []byte(content)) if len(permX) > 0 && permX[0] == true { @@ -242,7 +242,7 @@ func (question questionType) saveCodeContent(content, ext string, permX ...bool) } } -func (question questionType) saveMysqlSchemas() { +func (question *questionType) saveMysqlSchemas() { var buf bytes.Buffer for _, s := range question.MysqlSchemas { buf.WriteString(s + ";\n") @@ -250,7 +250,7 @@ func (question questionType) saveMysqlSchemas() { filePutContents(question.getFilePath("mysql_schemas.sql"), buf.Bytes()) } -func handleCodeGolang(question questionType, code codeSnippetsType) { +func handleCodeGolang(question *questionType, code codeSnippetsType) { content := fmt.Sprintf("package %s\n\n", question.PackageName()) content += code.Code + "\n" question.saveCodeContent(content, ".go") @@ -272,15 +272,15 @@ func handleCodeGolang(question questionType, code codeSnippetsType) { } } -func handleCodeBash(question questionType, code codeSnippetsType) { +func handleCodeBash(question *questionType, code codeSnippetsType) { question.saveCodeContent("#!/usr/bin/env bash\n\n"+code.Code, ".bash", true) } -func handleCodePython(question questionType, code codeSnippetsType) { +func handleCodePython(question *questionType, code codeSnippetsType) { question.saveCodeContent("#!/usr/bin/env python\n\n"+code.Code, ".py", true) } -func handleCodeSQL(question questionType, code codeSnippetsType) { +func handleCodeSQL(question *questionType, code codeSnippetsType) { question.saveCodeContent(code.Code, ".sql") question.saveMysqlSchemas() } diff --git a/internal/leetcode/topic_tag.go b/internal/leetcode/topic_tag.go index 4739f0eba..16e5b0230 100644 --- a/internal/leetcode/topic_tag.go +++ b/internal/leetcode/topic_tag.go @@ -5,7 +5,7 @@ import ( "fmt" "log" "os" - "path" + "path/filepath" "regexp" "sort" "strconv" @@ -16,7 +16,7 @@ import ( var ( initTags []TagType - tagsFile = path.Join("tag", "tags.json") + tagsFile = filepath.Join("tag", "tags.json") ) func init() { @@ -120,7 +120,7 @@ type ttQuestionType struct { TopicTags []TagType `json:"topicTags"` } -func (question ttQuestionType) TagsStr() string { +func (question *ttQuestionType) TagsStr() string { var buf bytes.Buffer format := "[[%s](https://github.com/openset/leetcode/tree/master/tag/%s/README.md)] " for _, tag := range question.TopicTags { @@ -130,7 +130,7 @@ func (question ttQuestionType) TagsStr() string { return string(buf.Bytes()) } -func (tag TagType) SaveContents() { +func (tag *TagType) SaveContents() { questions := GetTopicTag(tag.Slug).Data.TopicTag.Questions sort.Slice(questions, func(i, j int) bool { m, _ := strconv.Atoi(questions[i].QuestionFrontendId) @@ -149,11 +149,11 @@ func (tag TagType) SaveContents() { } buf.WriteString(fmt.Sprintf(format, question.QuestionFrontendId, question.TranslatedTitle, question.TitleSlug, question.IsPaidOnly.Str(), question.TagsStr(), question.Difficulty)) } - filename := path.Join("tag", tag.Slug, "README.md") + filename := filepath.Join("tag", tag.Slug, "README.md") filePutContents(filename, buf.Bytes()) } -func (tag TagType) ShowName() string { +func (tag *TagType) ShowName() string { if tag.TranslatedName != "" { return tag.TranslatedName } diff --git a/internal/post/post.go b/internal/post/post.go index b8a252b1c..f23c740f5 100644 --- a/internal/post/post.go +++ b/internal/post/post.go @@ -4,7 +4,7 @@ import ( "bytes" "fmt" "os" - "path" + "path/filepath" "regexp" "strings" "time" @@ -80,8 +80,8 @@ func runPost(cmd *base.Command, args []string) { buf.WriteString("\n---\n") buf.WriteString(fmt.Sprintf("\n## [答案](https://github.com/openset/leetcode/tree/master/problems/%s)\n", question.TitleSlug)) filename := fmt.Sprintf(formatFilename, t.Format("2006-01-02"), question.TitleSlug) - oldPath := path.Join(basePath, "leetcode", filename) - newPath := path.Join(basePath, "_posts", filename) + oldPath := filepath.Join(basePath, "leetcode", filename) + newPath := filepath.Join(basePath, "_posts", filename) base.FilePutContents(oldPath, buf.Bytes()) if inPosts[questionId] { _ = os.Rename(oldPath, newPath) @@ -101,7 +101,7 @@ func postTags() { } filename := fmt.Sprintf("tag-%s.md", tag.Slug) data := []byte(fmt.Sprintf(tagTmpl, title, tag.Slug, tag.Name)) - base.FilePutContents(path.Join(basePath, "_pages", filename), data) + base.FilePutContents(filepath.Join(basePath, "_pages", filename), data) } } @@ -125,7 +125,7 @@ taxonomy: %s var homeDir, _ = os.UserHomeDir() -var basePath = path.Join(homeDir, "openset", "openset") +var basePath = filepath.Join(homeDir, "openset", "openset") var inPosts = map[int]bool{ 1: true, diff --git a/internal/readme/readme.go b/internal/readme/readme.go index da62c4928..b98cdc674 100644 --- a/internal/readme/readme.go +++ b/internal/readme/readme.go @@ -3,7 +3,7 @@ package readme import ( "bytes" "fmt" - "path" + "path/filepath" "github.com/openset/leetcode/internal/base" "github.com/openset/leetcode/internal/leetcode" @@ -47,7 +47,7 @@ func writeProblems(buf *bytes.Buffer) { count-- problems[count].WriteRow(buf) } - fileName := path.Join("readme", fmt.Sprintf("%d-%d.md", pageSize*(i-1)+1, pageSize*i)) + fileName := filepath.Join("readme", fmt.Sprintf("%d-%d.md", pageSize*(i-1)+1, pageSize*i)) base.FilePutContents(fileName, buf.Bytes()) buf.Truncate(n) } diff --git a/internal/version/version.go b/internal/version/version.go index e99319bcc..6a3fd1582 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -7,7 +7,7 @@ import ( "github.com/openset/leetcode/internal/base" ) -const version = "1.4.4" +const version = "1.4.5" var CmdVersion = &base.Command{ Run: runVersion, From 4123fa5a5d5e7b95709fa9905c28586b136fa66f Mon Sep 17 00:00:00 2001 From: openset Date: Fri, 13 Sep 2019 16:12:59 +0800 Subject: [PATCH 2/3] Add: go1.13 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index bfd0a18a8..94bd3ad27 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: - - "1.12.x" + - "1.13.x" env: - GO111MODULE=on From 838c5efcf0fa08c31480c1959e6d3b5a8691e015 Mon Sep 17 00:00:00 2001 From: openset Date: Fri, 13 Sep 2019 16:18:30 +0800 Subject: [PATCH 3/3] Upgrade: go1.13 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 6bf61e8f5..6320d1775 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/openset/leetcode -go 1.12 +go 1.13