/
title.go
53 lines (42 loc) · 1.47 KB
/
title.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
package scrape
import (
"encoding/json"
"net/http"
"strings"
)
type Title struct {
Data struct {
Question QuestionTitle `json:"question"`
} `json:"data"`
}
type QuestionTitle struct {
QuestionID string `json:"questionId"`
QuestionFrontendID string `json:"questionFrontendId"`
Title string `json:"title"`
TitleSlug string `json:"titleSlug"`
IsPaidOnly bool `json:"isPaidOnly"`
Difficulty string `json:"difficulty"`
Likes int `json:"likes"`
Dislikes int `json:"dislikes"`
CategoryTitle string `json:"categoryTitle"`
}
func GetQuestionTitle(titleSlug string) (QuestionTitle, error) {
t := &Title{}
client := &http.Client{}
var data = strings.NewReader(`{"query":"\n query questionTitle($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n title\n titleSlug\n isPaidOnly\n difficulty\n likes\n dislikes\n categoryTitle\n }\n}\n ","variables":{"titleSlug":"` + titleSlug + `"},"operationName":"questionTitle"}`)
req, err := http.NewRequest("POST", "https://leetcode.com/graphql/", data)
if err != nil {
return t.Data.Question, err
}
req.Header.Set("content-type", "application/json")
resp, err := client.Do(req)
if err != nil {
return t.Data.Question, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(t)
if err != nil {
return t.Data.Question, err
}
return t.Data.Question, nil
}