-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
187 lines (162 loc) · 4.75 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"strconv"
"strings"
"time"
leetcode "github.com/jbltx/go-training/graphql-gen/examples/leetcode-client"
)
const (
submissionsQuery = `
query SubmissionsQuery($offset: Int, $limit: Int) {
submissionList(
offset:$offset
limit:$limit
) {
# lastKey
hasNext
submissions {
title
}
}
}
`
questionsQuery = `
query {
allQuestions(containMain: true) {
questionFrontendId
questionTitle
questionTitleSlug
}
}
`
)
type SubmissionsListNodeResponse struct {
Data *struct {
SubmissionList *leetcode.SubmissionListNode `json:"submissionList"`
} `json:"data"`
Errors []*leetcode.GraphQLError `json:"errors"`
}
type AllQuestionsResponse struct {
Data *struct {
AllQuestions []*leetcode.QuestionNode `json:"allQuestions"`
} `json:"data"`
Errors []*leetcode.GraphQLError `json:"errors"`
}
func main() {
// parse arguments from command-line
var jwt string
flag.StringVar(&jwt, "jwt", "", "JSON Web Token")
flag.Parse()
// delete submission entries in the README
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
readme, err := ioutil.ReadFile(path.Join(cwd, "README.md"))
if err != nil {
panic(err)
}
readmeStr := string(readme)
separator1 := "**Submission List**"
separator2 := "---"
idx1 := strings.Index(readmeStr, separator1)
idx2 := strings.Index(readmeStr, separator2)
newReadmeStr := readmeStr[:idx1+len(separator1)] + "\n"
// configure GraphQL client
gql := leetcode.NewGraphQLClient()
cookie := &http.Cookie{
Name: "LEETCODE_SESSION",
Value: jwt,
Path: "/",
Domain: gql.Endpoint.Hostname(),
}
gql.AddCookie(cookie)
// get all available questions first
rawResponse, err := gql.Query(questionsQuery, nil)
if err != nil {
panic(err)
}
var res AllQuestionsResponse
if err = json.Unmarshal(rawResponse, &res); err != nil {
panic(err)
}
// put the list of questions in a hashmap for convenience
questionsMap := make(map[string]*leetcode.QuestionNode)
if res.Data.AllQuestions != nil {
for _, question := range res.Data.AllQuestions {
questionsMap[question.QuestionTitle] = question
}
}
// get all submissions, and filter it by storing most recent ones in a hash map
filteredSubmissions := make(map[string]*leetcode.SubmissionDumpNodeLegacy)
submissionsVars := make(map[string]string)
submissionsOffset := 0
submissionsLimit := 20
submissionsVars["offset"] = strconv.Itoa(submissionsOffset)
submissionsVars["limit"] = strconv.Itoa(submissionsLimit)
hasNext := true
for hasNext {
var submissionList leetcode.SubmissionListNodeLegacy
time.Sleep(1 * time.Second) // little hack to not get banned
// For now I use the legacy API for submissions, because I didn't find a way to get the submitted
// code using the SubmissionDumpNode of GraphQL API... I suspect Leetcode to not using GraphQL that much :S
content, err := gql.Get("https://leetcode.com/api/submissions/", submissionsVars)
if err != nil {
panic(err)
}
if err = json.Unmarshal(content, &submissionList); err != nil {
panic(err)
}
if submissionList.Submissions != nil {
for _, submission := range submissionList.Submissions {
if submission != nil && submission.Lang == "golang" {
if _, ok := filteredSubmissions[submission.Title]; !ok {
// create the directory for the submission
dirName := fmt.Sprintf("%s-%s", questionsMap[submission.Title].QuestionFrontendID, questionsMap[submission.Title].QuestionTitleSlug)
dirPath := path.Join(cwd, dirName)
info, err := os.Stat(dirPath)
if err != nil || !info.IsDir() {
os.Mkdir(dirPath, 755)
}
// write submission code in a go file
filename := questionsMap[submission.Title].QuestionTitleSlug + ".go"
codeFilepath := path.Join(dirPath, filename)
f, err := os.Create(codeFilepath)
if err != nil {
panic(err)
}
f.WriteString(fmt.Sprintf("package main\n\n%s", submission.Code))
if err = f.Close(); err != nil {
panic(err)
}
// put an entry in the README
newReadmeStr += fmt.Sprintf(
"* %s-%s [Submission](/%s/%s) - [Leetcode Link](https://leetcode.com/problems/%s)\n",
questionsMap[submission.Title].QuestionFrontendID,
questionsMap[submission.Title].QuestionTitle,
dirName,
filename,
questionsMap[submission.Title].QuestionTitleSlug,
)
// store the submission in the map
filteredSubmissions[submission.Title] = submission
}
}
}
}
submissionsOffset += submissionsLimit
submissionsVars["offset"] = strconv.Itoa(submissionsOffset)
hasNext = submissionList.HasNext
}
newReadmeStr += readmeStr[idx2:]
if err = ioutil.WriteFile(path.Join(cwd, "README.md"), []byte(newReadmeStr), 755); err != nil {
panic(err)
}
}