Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
zcong1993 committed Jan 12, 2022
1 parent 0915f99 commit 32c5d80
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 43 deletions.
14 changes: 9 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"log"
"os"

"github.com/zcong1993/leetcode-tool/internal/config"

"github.com/zcong1993/leetcode-tool/cmd/new"
"github.com/zcong1993/leetcode-tool/cmd/tags"
"github.com/zcong1993/leetcode-tool/cmd/update"
Expand Down Expand Up @@ -36,8 +38,8 @@ var (
tagsForce = tagsCmd.Flag("force", "force update file").Short('f').Bool()
)

func showMeta(number string) {
meta, err := leetcode.GetMetaByNumber(number)
func showMeta(lc *leetcode.Leetcode, number string) {
meta, err := lc.GetMetaByNumber(number)
if err != nil {
log.Fatal(err)
}
Expand All @@ -55,15 +57,17 @@ func main() {
app.VersionFlag.Short('v')
app.HelpFlag.Short('h')

lc := leetcode.NewLeetcode(config.NewConfig())

switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case updateCmd.FullCommand():
update.Run()
case newCmd.FullCommand():
new.Run(*number, *lang)
new.Run(lc, *number, *lang)
case metaCmd.FullCommand():
showMeta(*metaNumber)
showMeta(lc, *metaNumber)
case tagsCmd.FullCommand():
tags.Run(*tagsForce)
tags.Run(lc, *tagsForce)
}
}

Expand Down
9 changes: 4 additions & 5 deletions cmd/new/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"strings"
"text/template"

config2 "github.com/zcong1993/leetcode-tool/internal/config"

"github.com/tidwall/gjson"

"github.com/zcong1993/leetcode-tool/pkg/leetcode"
Expand Down Expand Up @@ -83,10 +81,11 @@ type MetaWithFolder struct {
FrontendId string
}

func Run(n string, lang string) {
func Run(lc *leetcode.Leetcode, n string, lang string) {
if lang == "" {
lang = config2.GetLang()
lang = lc.Config.Lang
}

config, ok := languageConfigs[lang]
if !ok {
supportLangs := make([]string, len(languageConfigs))
Expand All @@ -97,7 +96,7 @@ func Run(n string, lang string) {
}
log.Fatalf("invalid lang, now support %s\n", strings.Join(supportLangs, ","))
}
meta, err := leetcode.GetMetaByNumber(n)
meta, err := lc.GetMetaByNumber(n)
if err != nil || meta == nil {
log.Fatal(err, meta)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/tags/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func fileExists(path string) bool {
return !os.IsNotExist(err)
}

func Run(force bool) {
tags, err := leetcode.GetTags()
func Run(lc *leetcode.Leetcode, force bool) {
tags, err := lc.GetTags()
if err != nil {
log.Fatal(err)
}
Expand Down
24 changes: 9 additions & 15 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ type Config struct {

const configPath = ".leetcode.json"

func LoadConfig() Config {
func NewConfig() *Config {
c := loadConfig()
if c.Cookie == "" {
c.Cookie = defaultCookie
}
return &c
}

func loadConfig() Config {
var c Config
content, err := ioutil.ReadFile(configPath)
if err != nil {
Expand All @@ -26,17 +34,3 @@ func LoadConfig() Config {
}
return c
}

func GetLang() string {
c := LoadConfig()
return c.Lang
}

func GetCookie() string {
c := LoadConfig()
ck := c.Cookie
if ck == "" {
return defaultCookie
}
return ck
}
34 changes: 18 additions & 16 deletions pkg/leetcode/leetcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ var (
}
)

func getAllPloblem() ([]byte, error) {
type Leetcode struct {
Config *config.Config
}

func NewLeetcode(config *config.Config) *Leetcode {
return &Leetcode{Config: config}
}

func (l *Leetcode) getAllProblem() ([]byte, error) {
req, err := http.NewRequest(http.MethodGet, "https://leetcode-cn.com/api/problems/all/", nil)
if err != nil {
return nil, err
}
req.Header.Set("Cookie", config.GetCookie())
req.Header.Set("Cookie", l.Config.Cookie)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
Expand All @@ -51,11 +59,11 @@ func getAllPloblem() ([]byte, error) {
return ioutil.ReadAll(resp.Body)
}

func findPloblemSlugByNumber(ploblems []byte, number string) string {
return gjson.GetBytes(ploblems, fmt.Sprintf("stat_status_pairs.#(stat.frontend_question_id=\"%s\").stat.question__title_slug", number)).String()
func (l *Leetcode) findProblemSlugByNumber(problems []byte, number string) string {
return gjson.GetBytes(problems, fmt.Sprintf("stat_status_pairs.#(stat.frontend_question_id=\"%s\").stat.question__title_slug", number)).String()
}

func getDetail(slug string) (*Meta, error) {
func (l *Leetcode) getDetail(slug string) (*Meta, error) {
if slug == "" {
return nil, nil
}
Expand All @@ -76,7 +84,6 @@ func getDetail(slug string) (*Meta, error) {
if err != nil {
return nil, err
}
//fmt.Println(string(content))
tagsResult := gjson.GetBytes(content, "data.question.topicTags.#.slug").Array()
tags := make([]string, len(tagsResult))
for i, t := range tagsResult {
Expand All @@ -85,10 +92,6 @@ func getDetail(slug string) (*Meta, error) {

codeSnippets := gjson.GetBytes(content, "data.question.codeSnippets").String()

//for _, v := range gjson.GetBytes(content, "data.question.codeSnippets.#.lang").Array() {
// println(v.String())
//}

return &Meta{
Index: gjson.GetBytes(content, "data.question.questionId").String(),
Title: gjson.GetBytes(content, "data.question.translatedTitle").String(),
Expand All @@ -101,17 +104,16 @@ func getDetail(slug string) (*Meta, error) {
}, nil
}

func GetMetaByNumber(number string) (*Meta, error) {
//ploblems, err := ioutil.ReadFile("./solve/a.json")
ploblems, err := getAllPloblem()
func (l *Leetcode) GetMetaByNumber(number string) (*Meta, error) {
problems, err := l.getAllProblem()
if err != nil {
return nil, err
}
slug := findPloblemSlugByNumber(ploblems, number)
return getDetail(slug)
slug := l.findProblemSlugByNumber(problems, number)
return l.getDetail(slug)
}

func GetTags() ([]Tag, error) {
func (l *Leetcode) GetTags() ([]Tag, error) {
resp, err := http.Get("https://leetcode-cn.com/problems/api/tags/")
if err != nil {
return nil, err
Expand Down

0 comments on commit 32c5d80

Please sign in to comment.