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

边做边学 #56

Merged
merged 1 commit into from
Jan 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
1. 在`LeetCode-in-Go`目录下,添加文本文件`config.toml`。
1. 把以下内容复制到`config.toml`中。
1. 把`config.toml`中的`test`分别修改为你的 leetcode `用户名`和`密码`。
1. 去 leetcode 登录后,把网站 cookie 复制 (有洁癖者只要复制 LEETCODE_SESSION 就够了) 并替换 `config.toml`中的`Cookie`。

```toml
Login="test"
Username="test"
Password="test"
Cookie="LEETCODE_SESSION=XXXXXXXXX;"
```
27 changes: 16 additions & 11 deletions Helper/buildProblemDir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"log"
"os"
"os/exec"
"runtime/debug"
"strings"
"syscall"
Expand Down Expand Up @@ -52,6 +51,7 @@ func build(p problem) {
}
}()

// windows用户注释这两行
mask := syscall.Umask(0)
defer syscall.Umask(mask)

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

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

content, fc := getGraphql(p)
if fc == "" {
log.Panicf("查无Go语言写法")
}

// 利用 chrome 打开题目页面
go func() {
cmd := exec.Command("google-chrome", p.link())
_, err = cmd.Output()
if err != nil {
panic(err.Error())
}
}()
// go func() {
// cmd := exec.Command("google-chrome", p.link())
// _, err = cmd.Output()
// if err != nil {
// panic(err.Error())
// }
// }()

fc := getFunction(p.link())
// fc := getFunction(p.link())

fcName, para, ans, fc := parseFunction(fc)
fcName, para, ans, _ := parseFunction(fc)

creatGo(p, fc, ans)

creatGoTest(p, fcName, para, ans)

creatREADME(p)
creatREADME(p, content)

log.Printf("%d.%s 的文件夹,创建完毕。\n", p.ID, p.Title)
}
Expand Down
1 change: 1 addition & 0 deletions Helper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
type config struct {
Username string
Password string
Cookie string

// 以下是电子邮件设置
SMTP string
Expand Down
75 changes: 75 additions & 0 deletions Helper/leetcode-getGraphql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
)

type JsL4CodeSnippets []struct {
Lang string
Code string
}

type JsL3Detail struct {
Content string
CodeSnippets JsL4CodeSnippets
}

type JsL2Question struct {
Question JsL3Detail
}

type JsL1Data struct {
Data JsL2Question
}

func getGraphql(p problem) (string, string) {
// req := newReq()

params := make(map[string]interface{})
params["operationName"] = "questionData"
params["query"] = "query questionData($titleSlug: String!) { question(titleSlug: $titleSlug) { content codeSnippets { lang code } } }"
titleSlug := make(map[string]string)
titleSlug["titleSlug"] = p.TitleSlug
params["variables"] = titleSlug

// Make this JSON
postJson, _ := json.Marshal(params)

// http.POST expects an io.Reader, which a byte buffer does
postContent := bytes.NewBuffer(postJson)

resp, err := http.Post("https://leetcode.com/graphql", "application/json", postContent)
if err != nil {
log.Fatal("getGraphql: POST Error: " + err.Error())
}

defer resp.Body.Close()

if resp.StatusCode != 200 {
log.Fatal("抓题失败 code: " + resp.Status)
}

respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal("getGraphql: Read Error: " + err.Error())
}

//byte数组直接转成string,优化内存
// str := (*string)(unsafe.Pointer(&respBytes))
// fmt.Printf("%#v\n", *str)

res := &JsL1Data{}
json.Unmarshal(respBytes, &res)

code := ""
for _, qc := range res.Data.Question.CodeSnippets {
if qc.Lang == "Go" {
code = qc.Code
}
}
return res.Data.Question.Content, code
}
2 changes: 1 addition & 1 deletion Helper/problemGoFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

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

defer func() { // 必须要先声明defer,否则不能捕获到panic异常
if err := recover(); err != nil {
Expand Down
10 changes: 7 additions & 3 deletions Helper/problemReadme.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import (
"fmt"
"sort"
"strings"

"github.com/TruthHun/html2md"
)

func creatREADME(p problem) {
func creatREADME(p problem, s string) {
fileFormat := `# [%d. %s](%s)

%s
`

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

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

content = replaceCharacters(content)

content = html2md.Convert(content)

filename := fmt.Sprintf("%s/README.md", p.Dir())

write(filename, content)
Expand All @@ -36,7 +40,7 @@ func replaceCharacters(s string) string {
"&lt;": "<",
"&gt;": ">",
"&ge;": ">=",
"&nbsp;": "`",
"&nbsp;": " ",
"&amp;": "&",
"&#39;": "'",
" \n": "\n",
Expand Down
30 changes: 17 additions & 13 deletions Helper/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,37 @@ func newReq() *request.Request {
// 登录 leetcode
// 返回的 req 带有 cookie
func signin() *request.Request {
log.Println("正在登录中...")
// log.Println("正在登录中...")
cfg := getConfig()

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

// 配置request
req.Headers = map[string]string{
"Content-Type": "application/json",
"Accept-Encoding": "",
"Referer": "https://leetcode.com/",
"cookie": cfg.Cookie,
"Referer": "https://leetcode.com/accounts/login/",
"origin": "https://leetcode.com",
}

// login
csrfToken := getCSRFToken(req)
// csrfToken := getCSRFToken(req)

log.Printf("csrfToken: %s", csrfToken)
// log.Printf("csrfToken: %s", csrfToken)

req.Data = map[string]string{
"csrfmiddlewaretoken": csrfToken,
"login": cfg.Username,
"password": cfg.Password,
}
if err := login(req); err != nil {
log.Fatal(err)
}
// req.Data = map[string]string{
// "csrfmiddlewaretoken": csrfToken,
// "login": cfg.Username,
// "password": cfg.Password,
// "next": "problems",
// }
// if err := login(req); err != nil {
// log.Fatal(err)
// }

log.Println("成功登录")
// log.Println("成功登录")

return req
}
Expand Down