forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathleetcode-getGraphql.go
75 lines (59 loc) · 1.57 KB
/
leetcode-getGraphql.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
}