123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- package ocr
- import (
- "bufio"
- "encoding/base64"
- "encoding/json"
- "git.listensoft.net/tool/jspkit/logger"
- "io"
- "net/http"
- "net/url"
- "os"
- "strings"
- "time"
- )
- //https://www.chaojiying.com/api-41.html
- // application/x-www-form-urlencoded 用这个时要注意urlencode
- // POST /Upload/Processing.php HTTP/1.1
- // Cache-Control: no-cache
- // Connection: Keep-Alive
- // Content-Type: application/x-www-form-urlencoded
- // Accept: */*
- // User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
- // Content-Length: 2915
- // Host: upload.chaojiying.net
- //
- // user=userabc&pass=passpass&softid=96001&codetype=1902&file_base64=base64字符串
- type Result struct {
- ErrNo int `json:"err_no"`
- ErrStr string `json:"err_str"`
- PicID string `json:"pic_id"`
- PicStr string `json:"pic_str"`
- Md5 string `json:"md5"`
- }
- const (
- user string = "canyang"
- pass string = "Qzf@0911"
- softid string = "898872"
- codetype string = "1902"
- )
- // 超级鹰
- func GetCaptchaCode(imgPath string) string {
- var client = http.Client{}
- client.Timeout = time.Duration(time.Second * 15)
- tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
- bstr, err := base64Img(imgPath)
- if err != nil {
- return ""
- }
- params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=" + codetype + "&file_base64=" + url.QueryEscape(bstr)
- req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
- if err != nil {
- return ""
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return ""
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return ""
- }
- var rs Result
- err = json.Unmarshal(body, &rs)
- if err != nil {
- return ""
- }
- if rs.ErrNo != 0 {
- return ""
- }
- return rs.PicStr
- }
- // 超级鹰 计算题
- func GetCaptchaCodeJST(imgPath string) string {
- var client = http.Client{}
- client.Timeout = time.Duration(time.Second * 15)
- tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
- bstr, err := base64Img(imgPath)
- if err != nil {
- return ""
- }
- params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=6001&file_base64=" + url.QueryEscape(bstr)
- req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
- if err != nil {
- return ""
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return ""
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return ""
- }
- var rs Result
- err = json.Unmarshal(body, &rs)
- if err != nil {
- return ""
- }
- if rs.ErrNo != 0 {
- return ""
- }
- return rs.PicStr
- }
- // 超级鹰 坐标点选择
- func GetCaptchaCode9103(imgPath string) string {
- var client = http.Client{}
- client.Timeout = time.Duration(time.Second * 40)
- tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
- bstr, err := base64Img(imgPath)
- if err != nil {
- return ""
- }
- params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9103&file_base64=" + url.QueryEscape(bstr)
- req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
- if err != nil {
- return ""
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return ""
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return ""
- }
- var rs Result
- err = json.Unmarshal(body, &rs)
- if err != nil {
- return ""
- }
- if rs.ErrNo != 0 {
- return ""
- }
- logger.Info("图片地址:", imgPath, "返回值:", rs)
- return rs.PicStr
- }
- // 超级鹰 坐标点选择
- func GetCaptchaCode9004(imgPath string, comID uint, len int) string {
- var client = http.Client{}
- client.Timeout = time.Duration(time.Second * 60)
- tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
- bstr, err := base64Img(imgPath)
- if err != nil {
- return ""
- }
- params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9004&file_base64=" + url.QueryEscape(bstr)
- req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
- if err != nil {
- return ""
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return ""
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return ""
- }
- var rs Result
- err = json.Unmarshal(body, &rs)
- if err != nil {
- return ""
- }
- if rs.ErrNo != 0 {
- return ""
- }
- logger.Info(rs.PicStr + " 坐标接口")
- return rs.PicStr
- }
- // 超级鹰 坐标点选择
- func GetCaptchaCode9101(imgPath string) string {
- var client = http.Client{}
- client.Timeout = time.Duration(time.Second * 15)
- tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
- bstr, err := base64Img(imgPath)
- if err != nil {
- return ""
- }
- params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9101&file_base64=" + url.QueryEscape(bstr)
- req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
- if err != nil {
- return ""
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return ""
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return ""
- }
- var rs Result
- err = json.Unmarshal(body, &rs)
- if err != nil {
- return ""
- }
- if rs.ErrNo != 0 {
- return ""
- }
- logger.Info(rs.PicStr + " 坐标接口")
- return rs.PicStr
- }
- // 超级鹰 坐标点选择
- func GetCaptchaCode9101Err(imgPath string) (string, string) {
- var client = http.Client{}
- client.Timeout = time.Duration(time.Second * 15)
- tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
- bstr, err := base64Img(imgPath)
- if err != nil {
- return "", ""
- }
- params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9101&file_base64=" + url.QueryEscape(bstr)
- req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
- if err != nil {
- return "", ""
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return "", ""
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return "", ""
- }
- var rs Result
- err = json.Unmarshal(body, &rs)
- if err != nil {
- return "", ""
- }
- if rs.ErrNo != 0 {
- return "", ""
- }
- logger.Info(rs.PicStr + " 坐标接口")
- return rs.PicID, rs.PicStr
- }
- // 超级鹰 问答题
- func GetCaptchaCodeWDT(imgPath string, comID uint, len int) string {
- var client = http.Client{}
- client.Timeout = time.Duration(time.Second * 15)
- tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
- bstr, err := base64Img(imgPath)
- if err != nil {
- return ""
- }
- params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=6004&file_base64=" + url.QueryEscape(bstr)
- req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
- if err != nil {
- return ""
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return ""
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return ""
- }
- var rs Result
- err = json.Unmarshal(body, &rs)
- if err != nil {
- return ""
- }
- if rs.ErrNo != 0 {
- return ""
- }
- return rs.PicStr
- }
- func GetCaptchaCodeWDTBase64(bstr string) string {
- var client = http.Client{}
- client.Timeout = time.Duration(time.Second * 15)
- tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
- //bstr, err := base64Img(imgPath)
- //if err != nil {
- // return ""
- //}
- params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=6004&file_base64=" + url.QueryEscape(bstr)
- req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
- if err != nil {
- return ""
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return ""
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return ""
- }
- var rs Result
- err = json.Unmarshal(body, &rs)
- if err != nil {
- return ""
- }
- if rs.ErrNo != 0 {
- return ""
- }
- return rs.PicStr
- }
- func ReportError(imgid string) error {
- var client = http.Client{}
- client.Timeout = time.Duration(time.Second * 15)
- tyURL := "http://upload.chaojiying.net/Upload/ReportError.php"
- params := "user=" + user + "&pass=" + pass + "&id=" + imgid + "&softid=" + softid
- req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
- if err != nil {
- return err
- }
- req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
- resp, err := client.Do(req)
- if err != nil {
- return err
- }
- body, err := io.ReadAll(resp.Body)
- if err != nil {
- return err
- }
- logger.Info(string(body))
- return nil
- }
- func base64Img(path string) (string, error) {
- imgFile, err := os.Open(path)
- if err != nil {
- return "", err
- }
- defer imgFile.Close()
- fInfo, _ := imgFile.Stat()
- var size int64 = fInfo.Size()
- buf := make([]byte, size)
- fReader := bufio.NewReader(imgFile)
- fReader.Read(buf)
- imgBase64str := base64.StdEncoding.EncodeToString(buf)
- return imgBase64str, nil
- }
|