@@ -10,11 +10,12 @@ import (
10
10
"sort"
11
11
"strconv"
12
12
13
+ "github.com/openset/leetcode/internal/base"
13
14
"github.com/openset/leetcode/internal/client"
14
15
)
15
16
16
17
var (
17
- initTags []tagType
18
+ initTags []TagType
18
19
tagsFile = path .Join ("tag" , "tags.json" )
19
20
)
20
21
@@ -25,25 +26,27 @@ func init() {
25
26
reg := regexp .MustCompile (`href="/tag/(\S+?)/"` )
26
27
for _ , matches := range reg .FindAllStringSubmatch (string (html ), - 1 ) {
27
28
if len (matches ) >= 2 {
28
- initTags = append (initTags , tagType {Slug : matches [1 ]})
29
+ initTags = append (initTags , TagType {Slug : matches [1 ]})
29
30
}
30
31
}
31
32
}
32
33
33
- func GetTags () (tags []tagType ) {
34
+ func GetTags () (tags []TagType ) {
34
35
cts := fileGetContents (tagsFile )
35
36
jsonDecode (cts , & tags )
36
37
tags = tagsUnique (tags )
37
38
return
38
39
}
39
40
40
- func saveTags (tags []tagType ) {
41
+ func saveTags (tags []TagType ) {
42
+ base .Mutex .Lock ()
41
43
tags = append (GetTags (), tags ... )
42
44
filePutContents (tagsFile , jsonEncode (tagsUnique (tags )))
45
+ base .Mutex .Unlock ()
43
46
}
44
47
45
- func tagsUnique (tags []tagType ) []tagType {
46
- rs , top := make ([]tagType , 0 , len (tags )), 1
48
+ func tagsUnique (tags []TagType ) []TagType {
49
+ rs , top := make ([]TagType , 0 , len (tags )), 1
47
50
tags = append (initTags , tags ... )
48
51
var flag = make (map [string ]int )
49
52
for _ , tag := range tags {
@@ -74,7 +77,7 @@ func GetTopicTag(slug string) (tt topicTagType) {
74
77
"query": "query getTopicTag($slug: String!) {\n topicTag(slug: $slug) {\n name\n translatedName\n questions {\n status\n questionId\n questionFrontendId\n title\n titleSlug\n translatedTitle\n stats\n difficulty\n isPaidOnly\n topicTags {\n name\n translatedName\n slug\n __typename\n }\n __typename\n }\n frequencies\n __typename\n }\n favoritesLists {\n publicFavorites {\n ...favoriteFields\n __typename\n }\n privateFavorites {\n ...favoriteFields\n __typename\n }\n __typename\n }\n}\n\nfragment favoriteFields on FavoriteNode {\n idHash\n id\n name\n isPublicFavorite\n viewCount\n creator\n isWatched\n questions {\n questionId\n title\n titleSlug\n __typename\n }\n __typename\n}\n"
75
78
}`
76
79
filename := fmt .Sprintf (topicTagFile , slugToSnake (slug ))
77
- graphQLRequest (filename , 2 , jsonStr , & tt )
80
+ graphQLRequest (graphQLCnUrl , jsonStr , filename , 2 , & tt )
78
81
if tt .Data .TopicTag .Name == "" {
79
82
_ = os .Remove (getCachePath (filename ))
80
83
for _ , err := range tt .Errors {
@@ -84,7 +87,7 @@ func GetTopicTag(slug string) (tt topicTagType) {
84
87
return
85
88
}
86
89
87
- type tagType struct {
90
+ type TagType struct {
88
91
Name string
89
92
Slug string
90
93
TranslatedName string
@@ -114,7 +117,7 @@ type ttQuestionType struct {
114
117
TranslatedContent string `json:"translatedContent"`
115
118
IsPaidOnly paidType `json:"isPaidOnly"`
116
119
Difficulty string `json:"difficulty"`
117
- TopicTags []tagType `json:"topicTags"`
120
+ TopicTags []TagType `json:"topicTags"`
118
121
}
119
122
120
123
func (question ttQuestionType ) TagsStr () string {
@@ -127,7 +130,7 @@ func (question ttQuestionType) TagsStr() string {
127
130
return string (buf .Bytes ())
128
131
}
129
132
130
- func (tag tagType ) SaveContents () {
133
+ func (tag TagType ) SaveContents () {
131
134
questions := GetTopicTag (tag .Slug ).Data .TopicTag .Questions
132
135
sort .Slice (questions , func (i , j int ) bool {
133
136
m , _ := strconv .Atoi (questions [i ].QuestionFrontendId )
@@ -150,7 +153,7 @@ func (tag tagType) SaveContents() {
150
153
filePutContents (filename , buf .Bytes ())
151
154
}
152
155
153
- func (tag tagType ) ShowName () string {
156
+ func (tag TagType ) ShowName () string {
154
157
if tag .TranslatedName != "" {
155
158
return tag .TranslatedName
156
159
}
0 commit comments