@@ -10,11 +10,12 @@ import (
1010 "sort"
1111 "strconv"
1212
13+ "github.com/openset/leetcode/internal/base"
1314 "github.com/openset/leetcode/internal/client"
1415)
1516
1617var (
17- initTags []tagType
18+ initTags []TagType
1819 tagsFile = path .Join ("tag" , "tags.json" )
1920)
2021
@@ -25,25 +26,27 @@ func init() {
2526 reg := regexp .MustCompile (`href="/tag/(\S+?)/"` )
2627 for _ , matches := range reg .FindAllStringSubmatch (string (html ), - 1 ) {
2728 if len (matches ) >= 2 {
28- initTags = append (initTags , tagType {Slug : matches [1 ]})
29+ initTags = append (initTags , TagType {Slug : matches [1 ]})
2930 }
3031 }
3132}
3233
33- func GetTags () (tags []tagType ) {
34+ func GetTags () (tags []TagType ) {
3435 cts := fileGetContents (tagsFile )
3536 jsonDecode (cts , & tags )
3637 tags = tagsUnique (tags )
3738 return
3839}
3940
40- func saveTags (tags []tagType ) {
41+ func saveTags (tags []TagType ) {
42+ base .Mutex .Lock ()
4143 tags = append (GetTags (), tags ... )
4244 filePutContents (tagsFile , jsonEncode (tagsUnique (tags )))
45+ base .Mutex .Unlock ()
4346}
4447
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
4750 tags = append (initTags , tags ... )
4851 var flag = make (map [string ]int )
4952 for _ , tag := range tags {
@@ -74,7 +77,7 @@ func GetTopicTag(slug string) (tt topicTagType) {
7477 "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"
7578 }`
7679 filename := fmt .Sprintf (topicTagFile , slugToSnake (slug ))
77- graphQLRequest (filename , 2 , jsonStr , & tt )
80+ graphQLRequest (graphQLCnUrl , jsonStr , filename , 2 , & tt )
7881 if tt .Data .TopicTag .Name == "" {
7982 _ = os .Remove (getCachePath (filename ))
8083 for _ , err := range tt .Errors {
@@ -84,7 +87,7 @@ func GetTopicTag(slug string) (tt topicTagType) {
8487 return
8588}
8689
87- type tagType struct {
90+ type TagType struct {
8891 Name string
8992 Slug string
9093 TranslatedName string
@@ -114,7 +117,7 @@ type ttQuestionType struct {
114117 TranslatedContent string `json:"translatedContent"`
115118 IsPaidOnly paidType `json:"isPaidOnly"`
116119 Difficulty string `json:"difficulty"`
117- TopicTags []tagType `json:"topicTags"`
120+ TopicTags []TagType `json:"topicTags"`
118121}
119122
120123func (question ttQuestionType ) TagsStr () string {
@@ -127,7 +130,7 @@ func (question ttQuestionType) TagsStr() string {
127130 return string (buf .Bytes ())
128131}
129132
130- func (tag tagType ) SaveContents () {
133+ func (tag TagType ) SaveContents () {
131134 questions := GetTopicTag (tag .Slug ).Data .TopicTag .Questions
132135 sort .Slice (questions , func (i , j int ) bool {
133136 m , _ := strconv .Atoi (questions [i ].QuestionFrontendId )
@@ -150,7 +153,7 @@ func (tag tagType) SaveContents() {
150153 filePutContents (filename , buf .Bytes ())
151154}
152155
153- func (tag tagType ) ShowName () string {
156+ func (tag TagType ) ShowName () string {
154157 if tag .TranslatedName != "" {
155158 return tag .TranslatedName
156159 }
0 commit comments