Skip to content

Commit

Permalink
fix: request add cookie, fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
zcong1993 committed Aug 23, 2021
1 parent aed4de0 commit b74930c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ $ leetcode-tool update

- [zcong1993/algo-go](https://github.com/zcong1993/algo-go)

## 常见问题

### 1. 报错 panic: runtime error: invalid memory address or nil pointer dereference

因为 LeetCode 网站现在某些请求会校验 cookie, 采取的修复方法是请求增加了 cookie, 但是内置 cookie 没法确保长期有效.

所以 `.leetcode.json` 配置文件中支持 cookie 配置, 后续请访问此链接 https://leetcode-cn.com/api/problems/all 拿到 cookie 自行更新配置文件.

## License

MIT © zcong1993
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ func showMeta(number string) {
if err != nil {
log.Fatal(err)
}
if meta == nil {
log.Fatal("mate not found")
}
meta.Content = ""
meta.Code = ""
meta.CodeSnippets = ""
Expand Down
6 changes: 5 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 h1:JYp7IbQjafo
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2cI+DC1Mbh0TJxzA3RcLoMsFw+aXw7E=
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/bmatcuk/doublestar v1.3.3 h1:pVP1d49CcQQaNOl+PI6sPybIrIOD/6sux31PFdmhTH0=
github.com/bmatcuk/doublestar/v2 v2.0.3 h1:D6SI8MzWzXXBXZFS87cFL6s/n307lEU+thM2SUnge3g=
github.com/bmatcuk/doublestar/v2 v2.0.3/go.mod h1:QMmcs3H2AUQICWhfzLXz+IYln8lRQmTZRptLie8RgRw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tidwall/gjson v1.6.3 h1:aHoiiem0dr7GHkW001T1SMTJ7X5PvyekH5WX0whWGnI=
github.com/tidwall/gjson v1.6.3/go.mod h1:BaHyNc5bjzYkPqgLq7mdVzeiRtULKULXLgZFKsxEHI0=
Expand All @@ -17,5 +19,7 @@ github.com/tidwall/pretty v1.0.2 h1:Z7S3cePv9Jwm1KwS0513MRaoUe3S01WPbLNV40pwWZU=
github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
14 changes: 13 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"io/ioutil"
)

const defaultCookie = "alicfw=1089899001%7C2081167297%7C1328233593%7C1328234352; alicfw_gfver=v1.200309.1"

type Config struct {
Lang string `json:"lang"`
Lang string `json:"lang"`
Cookie string `json:"cookie"`
}

const configPath = ".leetcode.json"
Expand All @@ -28,3 +31,12 @@ func GetLang() string {
c := LoadConfig()
return c.Lang
}

func GetCookie() string {
c := LoadConfig()
ck := c.Cookie
if ck == "" {
return defaultCookie
}
return ck
}
13 changes: 10 additions & 3 deletions pkg/leetcode/leetcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"

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

"github.com/tidwall/gjson"
)

Expand Down Expand Up @@ -37,7 +38,12 @@ var (
)

func getAllPloblem() ([]byte, error) {
resp, err := http.Get("https://leetcode-cn.com/api/problems/all/")
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())
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -96,9 +102,10 @@ func getDetail(slug string) (*Meta, error) {
}

func GetMetaByNumber(number string) (*Meta, error) {
//ploblems, err := ioutil.ReadFile("./solve/a.json")
ploblems, err := getAllPloblem()
if err != nil {
log.Fatal(err)
return nil, err
}
slug := findPloblemSlugByNumber(ploblems, number)
return getDetail(slug)
Expand Down

0 comments on commit b74930c

Please sign in to comment.