@@ -37,12 +37,20 @@ var (
3737 }
3838)
3939
40- func getAllPloblem () ([]byte , error ) {
40+ type Leetcode struct {
41+ Config * config.Config
42+ }
43+
44+ func NewLeetcode (config * config.Config ) * Leetcode {
45+ return & Leetcode {Config : config }
46+ }
47+
48+ func (l * Leetcode ) getAllProblem () ([]byte , error ) {
4149 req , err := http .NewRequest (http .MethodGet , "https://leetcode-cn.com/api/problems/all/" , nil )
4250 if err != nil {
4351 return nil , err
4452 }
45- req .Header .Set ("Cookie" , config . GetCookie () )
53+ req .Header .Set ("Cookie" , l . Config . Cookie )
4654 resp , err := http .DefaultClient .Do (req )
4755 if err != nil {
4856 return nil , err
@@ -51,11 +59,11 @@ func getAllPloblem() ([]byte, error) {
5159 return ioutil .ReadAll (resp .Body )
5260}
5361
54- func findPloblemSlugByNumber ( ploblems []byte , number string ) string {
55- return gjson .GetBytes (ploblems , fmt .Sprintf ("stat_status_pairs.#(stat.frontend_question_id=\" %s\" ).stat.question__title_slug" , number )).String ()
62+ func ( l * Leetcode ) findProblemSlugByNumber ( problems []byte , number string ) string {
63+ return gjson .GetBytes (problems , fmt .Sprintf ("stat_status_pairs.#(stat.frontend_question_id=\" %s\" ).stat.question__title_slug" , number )).String ()
5664}
5765
58- func getDetail (slug string ) (* Meta , error ) {
66+ func ( l * Leetcode ) getDetail (slug string ) (* Meta , error ) {
5967 if slug == "" {
6068 return nil , nil
6169 }
@@ -76,7 +84,6 @@ func getDetail(slug string) (*Meta, error) {
7684 if err != nil {
7785 return nil , err
7886 }
79- //fmt.Println(string(content))
8087 tagsResult := gjson .GetBytes (content , "data.question.topicTags.#.slug" ).Array ()
8188 tags := make ([]string , len (tagsResult ))
8289 for i , t := range tagsResult {
@@ -85,10 +92,6 @@ func getDetail(slug string) (*Meta, error) {
8592
8693 codeSnippets := gjson .GetBytes (content , "data.question.codeSnippets" ).String ()
8794
88- //for _, v := range gjson.GetBytes(content, "data.question.codeSnippets.#.lang").Array() {
89- // println(v.String())
90- //}
91-
9295 return & Meta {
9396 Index : gjson .GetBytes (content , "data.question.questionId" ).String (),
9497 Title : gjson .GetBytes (content , "data.question.translatedTitle" ).String (),
@@ -101,17 +104,16 @@ func getDetail(slug string) (*Meta, error) {
101104 }, nil
102105}
103106
104- func GetMetaByNumber (number string ) (* Meta , error ) {
105- //ploblems, err := ioutil.ReadFile("./solve/a.json")
106- ploblems , err := getAllPloblem ()
107+ func (l * Leetcode ) GetMetaByNumber (number string ) (* Meta , error ) {
108+ problems , err := l .getAllProblem ()
107109 if err != nil {
108110 return nil , err
109111 }
110- slug := findPloblemSlugByNumber ( ploblems , number )
111- return getDetail (slug )
112+ slug := l . findProblemSlugByNumber ( problems , number )
113+ return l . getDetail (slug )
112114}
113115
114- func GetTags () ([]Tag , error ) {
116+ func ( l * Leetcode ) GetTags () ([]Tag , error ) {
115117 resp , err := http .Get ("https://leetcode-cn.com/problems/api/tags/" )
116118 if err != nil {
117119 return nil , err
0 commit comments