Skip to content

Commit 7c16209

Browse files
committed
change direcotry structure
1 parent 47d8dd7 commit 7c16209

File tree

14 files changed

+58
-87
lines changed

14 files changed

+58
-87
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
*.so
66
*.dylib
77

8+
*.idea
9+
.password
10+
all_problems.json
11+
812
# Test binary, built with `go test -c`
913
*.test
1014

cmd/collect_all.go

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
11
package main
22

33
import (
4-
"github.com/realzhangm/leetcode_collector/collector"
4+
collector "github.com/realzhangm/leetcode_collector/pkg/collector"
5+
"github.com/realzhangm/leetcode_collector/pkg/doa"
56
)
67

78
// 实现简单的功能
8-
// 将 Leetcode 上的提交的代码,同步到本地,并生产一个 Markdown 汇总文件。
9-
func extractOneMarkDown() {
9+
// 将 LeetCode 上的提交的代码,同步到本地,并生产一个 Markdown 汇总文件。
10+
func run() {
1011
c := collector.NewCollector(collector.GetConfig())
11-
if err := c.LoadInfo(); err != nil {
12-
panic(err)
13-
}
14-
15-
err := c.FetchFromLeetCode()
16-
if err != nil {
17-
panic(err)
18-
}
19-
20-
//err = c.ExtractOneMarkDown()
21-
//if err != nil {
22-
// panic(err)
23-
//}
24-
}
25-
26-
func json2Md() {
27-
c := collector.NewCollector(collector.GetConfig())
28-
if err := c.LoadInfo(); err != nil {
29-
panic(err)
30-
}
31-
c.Json2MD()
32-
c.OutputSolutions()
12+
doa.MustOK(c.LoadInfo())
13+
doa.MustOK(c.FetchAllFromLeetCode())
14+
doa.MustOK(c.JsonToMarkDown())
15+
doa.MustOK(c.OutputSolutionsCode())
3316
}
3417

3518
func main() {
36-
extractOneMarkDown()
37-
json2Md()
19+
run()
3820
}
File renamed without changes.

collector/collector.go renamed to pkg/collector/collector.go

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7+
leetcode_cli2 "github.com/realzhangm/leetcode_collector/pkg/collector/leetcode_cli"
8+
"github.com/realzhangm/leetcode_collector/pkg/collector/model"
9+
"github.com/realzhangm/leetcode_collector/pkg/util"
710
"os"
811
"path"
912
"strconv"
@@ -14,9 +17,6 @@ import (
1417

1518
import (
1619
"github.com/pkg/errors"
17-
"github.com/realzhangm/leetcode_collector/collector/leetcode_cli"
18-
"github.com/realzhangm/leetcode_collector/collector/model"
19-
"github.com/realzhangm/leetcode_collector/collector/util"
2020
"golang.org/x/sync/errgroup"
2121
)
2222

@@ -26,22 +26,22 @@ var (
2626
)
2727

2828
type Collector struct {
29-
ltClit *leetcode_cli.Client
29+
ltClit *leetcode_cli2.Client
3030
conf Config
3131

3232
personInfo model.PersonInfoNode
3333
}
3434

3535
func NewCollector(c *Config) *Collector {
3636
collector := &Collector{
37-
ltClit: leetcode_cli.NewClient(&c.ltClientConf),
37+
ltClit: leetcode_cli2.NewClient(&c.ltClientConf),
3838
conf: *c,
3939
personInfo: model.PersonInfoNode{
4040
Mutex: sync.Mutex{},
4141
InfoNode: model.InfoNode{},
42-
AcProblems: make(map[string]leetcode_cli.ProblemStatus),
43-
AcProblemsDetail: make(map[string]leetcode_cli.Question),
44-
AcSubmissions: make(map[string]map[string]leetcode_cli.SubmissionDetail),
42+
AcProblems: make(map[string]leetcode_cli2.ProblemStatus),
43+
AcProblemsDetail: make(map[string]leetcode_cli2.Question),
44+
AcSubmissions: make(map[string]map[string]leetcode_cli2.SubmissionDetail),
4545
},
4646
}
4747
return collector
@@ -69,7 +69,7 @@ func (c *Collector) fetchAcProblemsDetail() error {
6969
return nil
7070
})
7171

72-
tmpMap := make(map[string]*leetcode_cli.Question)
72+
tmpMap := make(map[string]*leetcode_cli2.Question)
7373
mu := new(sync.Mutex)
7474
for i := 0; i < reqRoutineNum; i++ {
7575
g.Go(func() error {
@@ -148,8 +148,8 @@ func tryNTimes(n int, f func(i int) error) error {
148148
}
149149

150150
// 从sbl中选择
151-
func (c *Collector) submissionForOneLang(sbl []leetcode_cli.Submission) map[string]leetcode_cli.Submission {
152-
langSubmissionMap := make(map[string]leetcode_cli.Submission)
151+
func (c *Collector) submissionForOneLang(sbl []leetcode_cli2.Submission) map[string]leetcode_cli2.Submission {
152+
langSubmissionMap := make(map[string]leetcode_cli2.Submission)
153153
for _, sb := range sbl {
154154
v, e := langSubmissionMap[sb.Lang]
155155
if !e || strings.Compare(v.Timestamp, sb.Timestamp) < 0 {
@@ -232,7 +232,7 @@ func (c *Collector) fetchAllSubmissionsXX() error {
232232
g, ctx := errgroup.WithContext(context.TODO())
233233

234234
slugChan := make(chan string)
235-
submissionsChan := make(chan *leetcode_cli.SubmissionsByQuestionResponse)
235+
submissionsChan := make(chan *leetcode_cli2.SubmissionsByQuestionResponse)
236236

237237
g.Go(func() error {
238238
defer close(slugChan)
@@ -265,7 +265,7 @@ func (c *Collector) fetchAllSubmissionsXX() error {
265265

266266
g.Go(func() error {
267267
for sbs := range submissionsChan {
268-
langSubmissionMap := make(map[string]leetcode_cli.Submission)
268+
langSubmissionMap := make(map[string]leetcode_cli2.Submission)
269269
for _, sb := range sbs.SubmissionList.Submissions {
270270
v, e := langSubmissionMap[sb.Lang]
271271
if !e || strings.Compare(v.Timestamp, sb.Timestamp) < 0 {
@@ -309,7 +309,7 @@ func (c *Collector) LoadInfo() error {
309309
return nil
310310
}
311311

312-
func (c *Collector) FetchFromLeetCode() error {
312+
func (c *Collector) FetchAllFromLeetCode() error {
313313
// 所有的AC
314314
if err := c.fetchAllProblems(); err != nil {
315315
return err
@@ -366,24 +366,11 @@ func (c *Collector) dumpInfo() error {
366366
return nil
367367
}
368368

369-
func (c *Collector) ExtractOneMarkDown() error {
370-
for slug, v := range c.personInfo.AcProblems {
371-
fmt.Printf("slug=%s %v %s \n", slug, v.IsFavor, c.personInfo.AcProblemsDetail[slug].QuestionID)
372-
}
373-
374-
for slug, v := range c.personInfo.AcProblemsDetail {
375-
fmt.Printf("slugPD=%s %v \n", slug, v.QuestionID)
376-
}
377-
378-
fmt.Println("len(c.personInfo.AcProblemsDetail)=", len(c.personInfo.AcProblemsDetail))
379-
return nil
380-
}
381-
382-
func (c *Collector) Json2MD() error {
369+
func (c *Collector) JsonToMarkDown() error {
383370
mdPath := path.Join(c.conf.OutputDir, "README.md")
384371
return c.personInfo.Json2Md(mdPath)
385372
}
386373

387-
func (c *Collector) OutputSolutions() error {
374+
func (c *Collector) OutputSolutionsCode() error {
388375
return c.personInfo.OutputSolutions(c.conf.SolutionsDir)
389376
}

collector/config.go renamed to pkg/collector/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package collector
33
import (
44
"bufio"
55
"fmt"
6+
"github.com/realzhangm/leetcode_collector/pkg/collector/leetcode_cli"
67
"os"
78
"path"
89
"strings"
@@ -12,7 +13,6 @@ import (
1213
"github.com/howeyc/gopass"
1314
"github.com/jinzhu/configor"
1415
"github.com/pkg/errors"
15-
"github.com/realzhangm/leetcode_collector/collector/leetcode_cli"
1616
)
1717

1818
type Config struct {

collector/leetcode_cli/client.go renamed to pkg/collector/leetcode_cli/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"github.com/realzhangm/leetcode_collector/pkg/bufferpool"
9+
"github.com/realzhangm/leetcode_collector/pkg/util"
810
"io"
911
"log"
1012
"net"
@@ -18,8 +20,6 @@ import (
1820
import (
1921
"github.com/machinebox/graphql"
2022
"github.com/pkg/errors"
21-
"github.com/realzhangm/leetcode_collector/collector/bufferpool"
22-
"github.com/realzhangm/leetcode_collector/collector/util"
2323
)
2424

2525
const (

collector/model/2md.go renamed to pkg/collector/model/main_markdown.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package model
22

33
import (
44
"fmt"
5+
"github.com/realzhangm/leetcode_collector/pkg/bufferpool"
6+
leetcode_cli2 "github.com/realzhangm/leetcode_collector/pkg/collector/leetcode_cli"
57
"os"
68
"sort"
79
"strconv"
@@ -10,11 +12,6 @@ import (
1012
"time"
1113
)
1214

13-
import (
14-
"github.com/realzhangm/leetcode_collector/collector/bufferpool"
15-
"github.com/realzhangm/leetcode_collector/collector/leetcode_cli"
16-
)
17-
1815
const markdownTemplate = `
1916
<p align="center"><img width="300" src="https://raw.githubusercontent.com/KivenCkl/LeetCode_Helper/master/imgs/leetcode-logo.png"></p>
2017
<p align="center">
@@ -42,8 +39,8 @@ const TableLine1 = `|{frontend_id}|{title}{paid_only}{is_favor}|{solutions}|{ac_
4239

4340
type TableLineFormat struct {
4441
slug string
45-
ps *leetcode_cli.ProblemStatus
46-
q *leetcode_cli.Question
42+
ps *leetcode_cli2.ProblemStatus
43+
q *leetcode_cli2.Question
4744
}
4845

4946
func (t TableLineFormat) frontendId() string {
@@ -61,7 +58,7 @@ func (t TableLineFormat) solutions() string {
6158

6259
func (t TableLineFormat) title() string {
6360
return fmt.Sprintf("[%s](%s%s)",
64-
t.ps.Stat.QuestionTitle, leetcode_cli.UrlProblems, t.ps.Stat.QuestionTitleSlug)
61+
t.ps.Stat.QuestionTitle, leetcode_cli2.UrlProblems, t.ps.Stat.QuestionTitleSlug)
6562
}
6663

6764
func (t TableLineFormat) paidOnly() string {
@@ -93,7 +90,7 @@ func (t TableLineFormat) tags() string {
9390
res := ""
9491
for _, tag := range t.q.TopicTags {
9592
res += fmt.Sprintf("[%s](%s%s)",
96-
tag.TranslatedName, leetcode_cli.UrlTag, tag.Slug) + "<br>"
93+
tag.TranslatedName, leetcode_cli2.UrlTag, tag.Slug) + "<br>"
9794
}
9895
return res
9996
}
@@ -123,15 +120,15 @@ func (t *TableLineFormat) templateExe() string {
123120
return buffer.String()
124121
}
125122

126-
type SubmissionDetailSlice []leetcode_cli.SubmissionDetail
123+
type SubmissionDetailSlice []leetcode_cli2.SubmissionDetail
127124

128125
func (s SubmissionDetailSlice) Len() int { return len(s) }
129126
func (s SubmissionDetailSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
130127
func (s SubmissionDetailSlice) Less(i, j int) bool {
131128
return strings.Compare(s[i].Lang, s[i].Lang) < 0
132129
}
133130

134-
type ProblemStatusSlice []leetcode_cli.ProblemStatus
131+
type ProblemStatusSlice []leetcode_cli2.ProblemStatus
135132

136133
func (s ProblemStatusSlice) Len() int { return len(s) }
137134
func (s ProblemStatusSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

collector/model/output_solutions.go renamed to pkg/collector/model/output_solutions.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package model
33
import (
44
"encoding/json"
55
"fmt"
6+
"github.com/realzhangm/leetcode_collector/pkg/bufferpool"
7+
leetcode_cli2 "github.com/realzhangm/leetcode_collector/pkg/collector/leetcode_cli"
8+
"github.com/realzhangm/leetcode_collector/pkg/util"
69
"os"
710
"path"
811
"sort"
@@ -11,12 +14,6 @@ import (
1114
"time"
1215
)
1316

14-
import (
15-
"github.com/realzhangm/leetcode_collector/collector/bufferpool"
16-
"github.com/realzhangm/leetcode_collector/collector/leetcode_cli"
17-
"github.com/realzhangm/leetcode_collector/collector/util"
18-
)
19-
2017
// 题目描述 README 中文模板
2118
const SolutionReadme = `
2219
# {{title_cn}}
@@ -68,15 +65,15 @@ type SolutionReadMeFormatter struct {
6865
slug string
6966
preSlug string
7067
nextSlug string
71-
subLangMap map[string]leetcode_cli.SubmissionDetail
72-
question *leetcode_cli.Question
68+
subLangMap map[string]leetcode_cli2.SubmissionDetail
69+
question *leetcode_cli2.Question
7370
p *PersonInfoNode
7471
}
7572

7673
// 支持函数参数
7774
func (s SolutionReadMeFormatter) titleCn() string {
7875
return fmt.Sprintf("[%s](%s%s)",
79-
s.question.TranslatedTitle, leetcode_cli.UrlProblems, s.slug)
76+
s.question.TranslatedTitle, leetcode_cli2.UrlProblems, s.slug)
8077
}
8178

8279
func (s SolutionReadMeFormatter) contentCn() string {
@@ -107,7 +104,7 @@ func (s SolutionReadMeFormatter) tagsCn() string {
107104
res := ""
108105
for _, tag := range s.question.TopicTags {
109106
res += fmt.Sprintf("- [%s](%s%s) \n",
110-
tag.TranslatedName, leetcode_cli.UrlTag, tag.Slug)
107+
tag.TranslatedName, leetcode_cli2.UrlTag, tag.Slug)
111108
}
112109
return res
113110
}
@@ -167,7 +164,7 @@ func (s *SolutionReadMeFormatter) outPutSolutionReadme(slugDir string) {
167164
}
168165
}
169166

170-
func (p *PersonInfoNode) writeOneSourceCode(slugDir, slug string, subDetail *leetcode_cli.SubmissionDetail) {
167+
func (p *PersonInfoNode) writeOneSourceCode(slugDir, slug string, subDetail *leetcode_cli2.SubmissionDetail) {
171168
lang := subDetail.Lang
172169
dst := path.Join(slugDir, slug+findExt(lang))
173170
buff := bufferpool.GetBuffer()
@@ -185,7 +182,7 @@ func (p *PersonInfoNode) writeOneSourceCode(slugDir, slug string, subDetail *lee
185182
buff.WriteString(time.Unix(int64(subDetail.Timestamp), 0).Format(time.RFC3339))
186183
buff.WriteString("\n")
187184
buff.WriteString("// @URL: ")
188-
buff.WriteString(leetcode_cli.UrlProblems + slug)
185+
buff.WriteString(leetcode_cli2.UrlProblems + slug)
189186
buff.WriteString("\n")
190187
buff.WriteString("\n")
191188
buff.WriteString("\n")

collector/model/model.go renamed to pkg/collector/model/person_info_node.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@ package model
22

33
import (
44
"fmt"
5+
"github.com/realzhangm/leetcode_collector/pkg/collector/leetcode_cli"
56
"strconv"
67
"strings"
78
"sync"
89
)
910

10-
import (
11-
"github.com/realzhangm/leetcode_collector/collector/leetcode_cli"
12-
)
13-
1411
type InfoNode struct {
1512
UserName string `json:"user_name"`
1613
NumSolved int `json:"num_solved"`

collector/test/scraping.go renamed to pkg/collector/test/scraping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"github.com/gocolly/colly"
8-
"github.com/realzhangm/leetcode_collector/collector/leetcode_cli"
8+
"github.com/realzhangm/leetcode_collector/pkg/collector/leetcode_cli"
99
"log"
1010
"net/http"
1111
)

pkg/doa/doa.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package doa
2+
3+
func MustOK(err error) {
4+
if err != nil {
5+
panic(err)
6+
}
7+
}
File renamed without changes.

0 commit comments

Comments
 (0)