github.com/fastwego/offiaccount@v1.0.1/cmd/parseDocLink.go (about) 1 // Copyright 2020 FastWeGo 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "fmt" 19 "io/ioutil" 20 "net/http" 21 "net/url" 22 "regexp" 23 "strings" 24 25 "github.com/iancoleman/strcase" 26 ) 27 28 const ServerUrl = `https://developers.weixin.qq.com` 29 30 var apiUniqMap = map[string]bool{} 31 32 func run() { 33 34 file, err := ioutil.ReadFile("./data/doc_links.html") 35 if err != nil { 36 fmt.Println(err) 37 return 38 } 39 pattern := `href="(/doc/offiaccount/.+\.html)"` 40 reg := regexp.MustCompile(pattern) 41 matched := reg.FindAllStringSubmatch(string(file), -1) 42 43 for _, match := range matched { 44 //fmt.Println(match[1]) 45 46 link := ServerUrl + match[1] 47 resp, err := http.Get(link) 48 if err != nil { 49 continue 50 } 51 52 body, err := ioutil.ReadAll(resp.Body) 53 if err != nil { 54 continue 55 } 56 57 apiPattern := `http[s]*://api.weixin.qq.com/\S+` 58 reg := regexp.MustCompile(apiPattern) 59 apiMatched := reg.FindAllStringSubmatch(string(body), -1) 60 if apiMatched == nil { 61 continue 62 } 63 for _, apis := range apiMatched { 64 //fmt.Println(apis[0]) 65 66 parse, _ := url.Parse(apis[0]) 67 if _, ok := apiUniqMap[parse.Path]; !ok { 68 apiUniqMap[parse.Path] = true 69 } else { 70 continue 71 } 72 _NAME_ := parse.Path 73 _DESCRIPTION_ := "" 74 75 indexByte := strings.IndexByte(apis[0], '<') 76 if indexByte > 0 { 77 apis[0] = apis[0][0:indexByte] 78 } 79 _REQUEST_ := "POST " + apis[0] 80 _SEE_ := link 81 _FUNCNAME_ := "" 82 83 if strings.Contains(apis[0], "/cgi-bin/guide/") { 84 r := regexp.MustCompile(`shopping-guide\.(.*)\.html`) 85 found := r.FindStringSubmatch(link) 86 if found[1] != "" { 87 _FUNCNAME_ = strcase.ToCamel(found[1]) 88 } 89 90 r = regexp.MustCompile(`<h1 id="shopping-guide-.+</blockquote> <p>(\S+)</p>`) 91 found = r.FindStringSubmatch(string(body)) 92 if len(found) > 0 && found[1] != "" { 93 _NAME_ = found[1] 94 } 95 96 } 97 98 tpl := strings.ReplaceAll(itemTpl, "_NAME_", _NAME_) 99 tpl = strings.ReplaceAll(tpl, "_DESCRIPTION_", _DESCRIPTION_) 100 tpl = strings.ReplaceAll(tpl, "_REQUEST_", _REQUEST_) 101 tpl = strings.ReplaceAll(tpl, "_SEE_", _SEE_) 102 tpl = strings.ReplaceAll(tpl, "_FUNCNAME_", _FUNCNAME_) 103 104 fmt.Println(tpl) 105 } 106 107 fmt.Println(`-------------`) 108 109 //break 110 } 111 112 } 113 114 var itemTpl = `{ 115 Name: "_NAME_", 116 Description: "_DESCRIPTION_", 117 Request: "_REQUEST_", 118 See: "_SEE_", 119 FuncName: "_FUNCNAME_", 120 },`