-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathquestion_data.go
313 lines (281 loc) · 9.74 KB
/
question_data.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package leetcode
import (
"bytes"
"fmt"
"log"
"os"
"path"
"regexp"
"strconv"
"strings"
"unicode"
)
func QuestionData(titleSlug string, isForce bool, graphQL ...string) (qd questionDataType) {
jsonStr := `{
"operationName": "questionData",
"variables": {
"titleSlug": "` + titleSlug + `"
},
"query": "query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {\n questionId\n questionFrontendId\n boundTopicId\n title\n titleSlug\n content\n translatedTitle\n translatedContent\n isPaidOnly\n difficulty\n likes\n dislikes\n isLiked\n similarQuestions\n contributors {\n username\n profileUrl\n avatarUrl\n __typename\n }\n langToValidPlayground\n topicTags {\n name\n slug\n translatedName\n __typename\n }\n companyTagStats\n codeSnippets {\n lang\n langSlug\n code\n __typename\n }\n stats\n hints\n solution {\n id\n canSeeDetail\n __typename\n }\n status\n sampleTestCase\n metaData\n judgerAvailable\n judgeType\n mysqlSchemas\n enableRunCode\n enableTestMode\n envInfo\n __typename\n }\n}\n"
}`
days := 3
if isForce {
days = 0
}
if len(graphQL) == 0 {
graphQL = []string{graphQLCnUrl}
}
filename := fmt.Sprintf(questionDataFile, slugToSnake(titleSlug))
graphQLRequest(graphQL[0], jsonStr, filename, days, &qd)
if qd.Data.Question.TitleSlug == "" {
_ = os.Remove(getCachePath(filename))
if graphQL[0] == graphQLCnUrl {
return QuestionData(titleSlug, isForce, graphQLUrl)
}
for _, err := range qd.Errors {
log.Println(titleSlug, err.Message)
}
}
return
}
type questionDataType struct {
Errors []errorType `json:"errors"`
Data dataType `json:"data"`
}
type errorType struct {
Message string `json:"message"`
}
type dataType struct {
Question questionType `json:"question"`
}
type questionType struct {
QuestionId string `json:"questionId"`
QuestionFrontendId string `json:"questionFrontendId"`
BoundTopicId int `json:"boundTopicId"`
Title string `json:"title"`
TitleSlug string `json:"titleSlug"`
Content string `json:"content"`
TranslatedTitle string `json:"translatedTitle"`
TranslatedContent string `json:"translatedContent"`
IsPaidOnly paidType `json:"isPaidOnly"`
Difficulty difficultyStrType `json:"difficulty"`
Likes int `json:"likes"`
Dislikes int `json:"dislikes"`
IsLiked int `json:"isLiked"`
SimilarQuestions string `json:"similarQuestions"`
TopicTags []TagType `json:"topicTags"`
CodeSnippets []codeSnippetsType `json:"codeSnippets"`
Hints []string `json:"hints"`
MysqlSchemas []string `json:"mysqlSchemas"`
}
type codeSnippetsType struct {
Lang string `json:"lang"`
LangSlug string `json:"langSlug"`
Code string `json:"code"`
}
type similarQuestionType struct {
Title string `json:"title"`
TitleSlug string `json:"titleSlug"`
Difficulty difficultyStrType `json:"difficulty"`
TranslatedTitle string `json:"translatedTitle"`
}
type difficultyStrType string
func (d difficultyStrType) Str() (s string) {
if d != "" {
s = fmt.Sprintf(" (%s)", d)
}
return
}
func (question questionType) SaveContent() {
if question.TitleSlug != "" {
fmt.Println(question.QuestionFrontendId, "\t"+question.Title, "saving...")
filePutContents(question.getFilePath("README.md"), question.getDescContent())
question.saveMysqlSchemas()
}
}
func (question questionType) getDescContent() []byte {
var buf bytes.Buffer
buf.WriteString(authInfo("description"))
buf.WriteString(question.getNavigation())
buf.WriteString(fmt.Sprintf("\n## %s. %s%s\n\n", question.QuestionFrontendId, question.Title, question.Difficulty.Str()))
cts := filterContents(question.Content)
// remove style
reg := regexp.MustCompile(`<style[\S\s]+?</style>`)
cts = reg.ReplaceAllString(cts, "")
cts = strings.ReplaceAll(cts, "\n\n\t", "\n\t")
cts = strings.TrimSpace(cts) + "\n"
buf.WriteString(cts)
buf.Write(question.getTopicTags())
buf.Write(question.getSimilarQuestion())
buf.Write(question.getHints())
return buf.Bytes()
}
func (question questionType) getNavigation() string {
nav, pre, next := "\n%s\n%s\n%s\n", "< Previous", "Next >"
problems := ProblemsAll().StatStatusPairs
if questionId, err := strconv.Atoi(question.QuestionId); err == nil {
format := `[%s](https://github.com/openset/leetcode/tree/master/problems/%s "%s")`
for i, problem := range problems {
if problem.Stat.QuestionId == questionId {
if i < len(problems)-1 {
pre = fmt.Sprintf(format, pre, problems[i+1].Stat.QuestionTitleSlug, problems[i+1].Stat.QuestionTitle)
}
if i > 0 {
next = fmt.Sprintf(format, next, problems[i-1].Stat.QuestionTitleSlug, problems[i-1].Stat.QuestionTitle)
}
break
}
}
}
return fmt.Sprintf(nav, pre, strings.Repeat(" ", 16), next)
}
func (question questionType) getTopicTags() []byte {
tags := question.TopicTags
var buf bytes.Buffer
if len(tags) > 0 {
buf.WriteString("\n### Related Topics\n")
}
format := " [[%s](https://github.com/openset/leetcode/tree/master/tag/%s/README.md)]\n"
for _, tag := range tags {
buf.WriteString(fmt.Sprintf(format, tag.Name, tag.Slug))
}
return buf.Bytes()
}
func (question questionType) GetSimilarQuestion() (sq []similarQuestionType) {
jsonDecode([]byte(question.SimilarQuestions), &sq)
return
}
func (question questionType) getSimilarQuestion() []byte {
sq := question.GetSimilarQuestion()
var buf bytes.Buffer
if len(sq) > 0 {
buf.WriteString("\n### Similar Questions\n")
}
format := " 1. [%s](https://github.com/openset/leetcode/tree/master/problems/%s)%s\n"
for _, q := range sq {
buf.WriteString(fmt.Sprintf(format, q.Title, q.TitleSlug, q.Difficulty.Str()))
}
return buf.Bytes()
}
func (question questionType) getHints() []byte {
hints := question.Hints
var buf bytes.Buffer
if len(hints) > 0 {
buf.WriteString("\n### Hints")
}
for i, hint := range hints {
buf.WriteString(fmt.Sprintf("\n<details>\n<summary>Hint %d</summary>\n%s\n</details>\n", i+1, filterContents(hint)))
}
return buf.Bytes()
}
func (question questionType) getFilePath(filename string) string {
return path.Join("problems", question.TitleSlug, filename)
}
func (question questionType) TitleSnake() string {
return slugToSnake(question.TitleSlug)
}
func (question questionType) PackageName() string {
snake := question.TitleSnake()
if snake != "" && unicode.IsNumber(rune(snake[0])) {
snake = "p_" + snake
}
return snake
}
func (question questionType) SaveCodeSnippet() {
if isLangMySQL(question.TitleSlug) {
filePutContents(question.getFilePath(question.TitleSnake()+".sql"), []byte("# Write your MySQL query statement below\n"))
}
langSupport := [...]struct {
slug string
handle func(questionType, codeSnippetsType)
}{
{"golang", handleCodeGolang},
{"python3", handleCodePython},
{"python", handleCodePython},
{"bash", handleCodeBash},
{"mysql", handleCodeSQL},
{"mssql", handleCodeSQL},
{"oraclesql", handleCodeSQL},
}
codeSet := make(map[string]codeSnippetsType)
for _, code := range question.CodeSnippets {
codeSet[code.LangSlug] = code
}
for _, lang := range langSupport {
if code, ok := codeSet[lang.slug]; ok {
lang.handle(question, code)
break
}
}
}
func (question questionType) saveCodeContent(content, ext string, permX ...bool) {
filePath := question.getFilePath(question.TitleSnake() + ext)
filePutContents(filePath, []byte(content))
if len(permX) > 0 && permX[0] == true {
_ = os.Chmod(filePath, 0755)
}
}
func (question questionType) saveMysqlSchemas() {
var buf bytes.Buffer
for _, s := range question.MysqlSchemas {
buf.WriteString(s + ";\n")
}
filePutContents(question.getFilePath("mysql_schemas.sql"), buf.Bytes())
}
func handleCodeGolang(question questionType, code codeSnippetsType) {
content := fmt.Sprintf("package %s\n\n", question.PackageName())
content += code.Code + "\n"
question.saveCodeContent(content, ".go")
testExt := "_test.go"
contents := fileGetContents(question.getFilePath(question.TitleSnake() + testExt))
if bytes.Count(contents, []byte("\n")) <= 2 {
// match function name
reg := regexp.MustCompile(`func (\w+?)\(`)
matches := reg.FindStringSubmatch(code.Code)
funcName := "Func"
if len(matches) >= 2 {
funcName = matches[1]
}
content = strings.NewReplacer(
"{{packageName}}", question.PackageName(),
"{{funcName}}", strings.Title(funcName),
).Replace(testTpl)
question.saveCodeContent(content, testExt)
}
}
func handleCodeBash(question questionType, code codeSnippetsType) {
question.saveCodeContent("#!/usr/bin/env bash\n\n"+code.Code, ".bash", true)
}
func handleCodePython(question questionType, code codeSnippetsType) {
question.saveCodeContent("#!/usr/bin/env python\n\n"+code.Code, ".py", true)
}
func handleCodeSQL(question questionType, code codeSnippetsType) {
question.saveCodeContent(code.Code, ".sql")
question.saveMysqlSchemas()
}
func filterContents(cts string) string {
cts = strings.ReplaceAll(cts, "\r", "")
cts = strings.ReplaceAll(cts, `src="/static`, `src="https://assets.leetcode.com/static_assets/public`)
return cts
}
const testTpl = `package {{packageName}}
import "testing"
type caseType struct {
input int
expected int
}
func Test{{funcName}}(t *testing.T) {
tests := [...]caseType{
{
input: 0,
expected: 0,
},
}
for _, tc := range tests {
output := 0
if output != tc.expected {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
}
}
`