Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit ada3365

Browse files
authored
Merge pull request #56 from aocgame/master
边做边学
2 parents bfa18e3 + 2699983 commit ada3365

7 files changed

+120
-29
lines changed

Helper/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
1.`LeetCode-in-Go`目录下,添加文本文件`config.toml`
77
1. 把以下内容复制到`config.toml`中。
88
1.`config.toml`中的`test`分别修改为你的 leetcode `用户名``密码`
9+
1. 去 leetcode 登录后,把网站 cookie 复制 (有洁癖者只要复制 LEETCODE_SESSION 就够了) 并替换 `config.toml`中的`Cookie`
910

1011
```toml
11-
Login="test"
12+
Username="test"
1213
Password="test"
14+
Cookie="LEETCODE_SESSION=XXXXXXXXX;"
1315
```

Helper/buildProblemDir.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"log"
66
"os"
7-
"os/exec"
87
"runtime/debug"
98
"strings"
109
"syscall"
@@ -52,6 +51,7 @@ func build(p problem) {
5251
}
5352
}()
5453

54+
// windows用户注释这两行
5555
mask := syscall.Umask(0)
5656
defer syscall.Umask(mask)
5757

@@ -63,24 +63,29 @@ func build(p problem) {
6363

6464
log.Printf("开始创建 %d %s 的文件夹...\n", p.ID, p.Title)
6565

66+
content, fc := getGraphql(p)
67+
if fc == "" {
68+
log.Panicf("查无Go语言写法")
69+
}
70+
6671
// 利用 chrome 打开题目页面
67-
go func() {
68-
cmd := exec.Command("google-chrome", p.link())
69-
_, err = cmd.Output()
70-
if err != nil {
71-
panic(err.Error())
72-
}
73-
}()
72+
// go func() {
73+
// cmd := exec.Command("google-chrome", p.link())
74+
// _, err = cmd.Output()
75+
// if err != nil {
76+
// panic(err.Error())
77+
// }
78+
// }()
7479

75-
fc := getFunction(p.link())
80+
// fc := getFunction(p.link())
7681

77-
fcName, para, ans, fc := parseFunction(fc)
82+
fcName, para, ans, _ := parseFunction(fc)
7883

7984
creatGo(p, fc, ans)
8085

8186
creatGoTest(p, fcName, para, ans)
8287

83-
creatREADME(p)
88+
creatREADME(p, content)
8489

8590
log.Printf("%d.%s 的文件夹,创建完毕。\n", p.ID, p.Title)
8691
}

Helper/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
type config struct {
1515
Username string
1616
Password string
17+
Cookie string
1718

1819
// 以下是电子邮件设置
1920
SMTP string

Helper/leetcode-getGraphql.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io/ioutil"
7+
"log"
8+
"net/http"
9+
)
10+
11+
type JsL4CodeSnippets []struct {
12+
Lang string
13+
Code string
14+
}
15+
16+
type JsL3Detail struct {
17+
Content string
18+
CodeSnippets JsL4CodeSnippets
19+
}
20+
21+
type JsL2Question struct {
22+
Question JsL3Detail
23+
}
24+
25+
type JsL1Data struct {
26+
Data JsL2Question
27+
}
28+
29+
func getGraphql(p problem) (string, string) {
30+
// req := newReq()
31+
32+
params := make(map[string]interface{})
33+
params["operationName"] = "questionData"
34+
params["query"] = "query questionData($titleSlug: String!) { question(titleSlug: $titleSlug) { content codeSnippets { lang code } } }"
35+
titleSlug := make(map[string]string)
36+
titleSlug["titleSlug"] = p.TitleSlug
37+
params["variables"] = titleSlug
38+
39+
// Make this JSON
40+
postJson, _ := json.Marshal(params)
41+
42+
// http.POST expects an io.Reader, which a byte buffer does
43+
postContent := bytes.NewBuffer(postJson)
44+
45+
resp, err := http.Post("https://leetcode.com/graphql", "application/json", postContent)
46+
if err != nil {
47+
log.Fatal("getGraphql: POST Error: " + err.Error())
48+
}
49+
50+
defer resp.Body.Close()
51+
52+
if resp.StatusCode != 200 {
53+
log.Fatal("抓题失败 code: " + resp.Status)
54+
}
55+
56+
respBytes, err := ioutil.ReadAll(resp.Body)
57+
if err != nil {
58+
log.Fatal("getGraphql: Read Error: " + err.Error())
59+
}
60+
61+
//byte数组直接转成string,优化内存
62+
// str := (*string)(unsafe.Pointer(&respBytes))
63+
// fmt.Printf("%#v\n", *str)
64+
65+
res := &JsL1Data{}
66+
json.Unmarshal(respBytes, &res)
67+
68+
code := ""
69+
for _, qc := range res.Data.Question.CodeSnippets {
70+
if qc.Lang == "Go" {
71+
code = qc.Code
72+
}
73+
}
74+
return res.Data.Question.Content, code
75+
}

Helper/problemGoFile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func parseFunction(fc string) (fcName, para, ansType, nfc string) {
12-
log.Println("准备分解函数:", fc)
12+
log.Println("正在分解函数")
1313

1414
defer func() { // 必须要先声明defer,否则不能捕获到panic异常
1515
if err := recover(); err != nil {

Helper/problemReadme.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@ import (
44
"fmt"
55
"sort"
66
"strings"
7+
8+
"github.com/TruthHun/html2md"
79
)
810

9-
func creatREADME(p problem) {
11+
func creatREADME(p problem, s string) {
1012
fileFormat := `# [%d. %s](%s)
1113
1214
%s
1315
`
1416

1517
questionDescription := strings.TrimSpace(getDescription(p.link()))
1618

17-
content := fmt.Sprintf(fileFormat, p.ID, p.Title, p.link(), questionDescription)
19+
content := fmt.Sprintf(fileFormat, p.ID, p.Title, p.link(), questionDescription) + s + "\n\n## 解题思路\n\n## 可能的變化"
1820

1921
content = replaceCharacters(content)
2022

23+
content = html2md.Convert(content)
24+
2125
filename := fmt.Sprintf("%s/README.md", p.Dir())
2226

2327
write(filename, content)
@@ -36,7 +40,7 @@ func replaceCharacters(s string) string {
3640
"&lt;": "<",
3741
"&gt;": ">",
3842
"&ge;": ">=",
39-
"&nbsp;": "`",
43+
"&nbsp;": " ",
4044
"&amp;": "&",
4145
"&#39;": "'",
4246
" \n": "\n",

Helper/signin.go

+17-13
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,37 @@ func newReq() *request.Request {
2323
// 登录 leetcode
2424
// 返回的 req 带有 cookie
2525
func signin() *request.Request {
26-
log.Println("正在登录中...")
26+
// log.Println("正在登录中...")
2727
cfg := getConfig()
2828

2929
// 对 req 赋值
3030
req := request.NewRequest(new(http.Client))
3131

3232
// 配置request
3333
req.Headers = map[string]string{
34+
"Content-Type": "application/json",
3435
"Accept-Encoding": "",
35-
"Referer": "https://leetcode.com/",
36+
"cookie": cfg.Cookie,
37+
"Referer": "https://leetcode.com/accounts/login/",
38+
"origin": "https://leetcode.com",
3639
}
3740

3841
// login
39-
csrfToken := getCSRFToken(req)
42+
// csrfToken := getCSRFToken(req)
4043

41-
log.Printf("csrfToken: %s", csrfToken)
44+
// log.Printf("csrfToken: %s", csrfToken)
4245

43-
req.Data = map[string]string{
44-
"csrfmiddlewaretoken": csrfToken,
45-
"login": cfg.Username,
46-
"password": cfg.Password,
47-
}
48-
if err := login(req); err != nil {
49-
log.Fatal(err)
50-
}
46+
// req.Data = map[string]string{
47+
// "csrfmiddlewaretoken": csrfToken,
48+
// "login": cfg.Username,
49+
// "password": cfg.Password,
50+
// "next": "problems",
51+
// }
52+
// if err := login(req); err != nil {
53+
// log.Fatal(err)
54+
// }
5155

52-
log.Println("成功登录")
56+
// log.Println("成功登录")
5357

5458
return req
5559
}

0 commit comments

Comments
 (0)