chaojiying.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package ocr
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "encoding/json"
  6. "git.listensoft.net/tool/jspkit/logger"
  7. "io"
  8. "net/http"
  9. "net/url"
  10. "os"
  11. "strings"
  12. "time"
  13. )
  14. //https://www.chaojiying.com/api-41.html
  15. // application/x-www-form-urlencoded 用这个时要注意urlencode
  16. // POST /Upload/Processing.php HTTP/1.1
  17. // Cache-Control: no-cache
  18. // Connection: Keep-Alive
  19. // Content-Type: application/x-www-form-urlencoded
  20. // Accept: */*
  21. // User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)
  22. // Content-Length: 2915
  23. // Host: upload.chaojiying.net
  24. //
  25. // user=userabc&pass=passpass&softid=96001&codetype=1902&file_base64=base64字符串
  26. type Result struct {
  27. ErrNo int `json:"err_no"`
  28. ErrStr string `json:"err_str"`
  29. PicID string `json:"pic_id"`
  30. PicStr string `json:"pic_str"`
  31. Md5 string `json:"md5"`
  32. }
  33. const (
  34. user string = "canyang"
  35. pass string = "Qzf@0911"
  36. softid string = "898872"
  37. codetype string = "1902"
  38. )
  39. // 超级鹰
  40. func GetCaptchaCode(imgPath string) string {
  41. var client = http.Client{}
  42. client.Timeout = time.Duration(time.Second * 15)
  43. tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
  44. bstr, err := base64Img(imgPath)
  45. if err != nil {
  46. return ""
  47. }
  48. params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=" + codetype + "&file_base64=" + url.QueryEscape(bstr)
  49. req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
  50. if err != nil {
  51. return ""
  52. }
  53. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  54. resp, err := client.Do(req)
  55. if err != nil {
  56. return ""
  57. }
  58. body, err := io.ReadAll(resp.Body)
  59. if err != nil {
  60. return ""
  61. }
  62. var rs Result
  63. err = json.Unmarshal(body, &rs)
  64. if err != nil {
  65. return ""
  66. }
  67. if rs.ErrNo != 0 {
  68. return ""
  69. }
  70. return rs.PicStr
  71. }
  72. // 超级鹰 计算题
  73. func GetCaptchaCodeJST(imgPath string) string {
  74. var client = http.Client{}
  75. client.Timeout = time.Duration(time.Second * 15)
  76. tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
  77. bstr, err := base64Img(imgPath)
  78. if err != nil {
  79. return ""
  80. }
  81. params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=6001&file_base64=" + url.QueryEscape(bstr)
  82. req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
  83. if err != nil {
  84. return ""
  85. }
  86. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  87. resp, err := client.Do(req)
  88. if err != nil {
  89. return ""
  90. }
  91. body, err := io.ReadAll(resp.Body)
  92. if err != nil {
  93. return ""
  94. }
  95. var rs Result
  96. err = json.Unmarshal(body, &rs)
  97. if err != nil {
  98. return ""
  99. }
  100. if rs.ErrNo != 0 {
  101. return ""
  102. }
  103. return rs.PicStr
  104. }
  105. // 超级鹰 坐标点选择
  106. func GetCaptchaCode9103(imgPath string) string {
  107. var client = http.Client{}
  108. client.Timeout = time.Duration(time.Second * 40)
  109. tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
  110. bstr, err := base64Img(imgPath)
  111. if err != nil {
  112. return ""
  113. }
  114. params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9103&file_base64=" + url.QueryEscape(bstr)
  115. req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
  116. if err != nil {
  117. return ""
  118. }
  119. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  120. resp, err := client.Do(req)
  121. if err != nil {
  122. return ""
  123. }
  124. body, err := io.ReadAll(resp.Body)
  125. if err != nil {
  126. return ""
  127. }
  128. var rs Result
  129. err = json.Unmarshal(body, &rs)
  130. if err != nil {
  131. return ""
  132. }
  133. if rs.ErrNo != 0 {
  134. return ""
  135. }
  136. logger.Info("图片地址:", imgPath, "返回值:", rs)
  137. return rs.PicStr
  138. }
  139. // 超级鹰 坐标点选择
  140. func GetCaptchaCode9004(imgPath string, comID uint, len int) string {
  141. var client = http.Client{}
  142. client.Timeout = time.Duration(time.Second * 60)
  143. tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
  144. bstr, err := base64Img(imgPath)
  145. if err != nil {
  146. return ""
  147. }
  148. params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9004&file_base64=" + url.QueryEscape(bstr)
  149. req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
  150. if err != nil {
  151. return ""
  152. }
  153. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  154. resp, err := client.Do(req)
  155. if err != nil {
  156. return ""
  157. }
  158. body, err := io.ReadAll(resp.Body)
  159. if err != nil {
  160. return ""
  161. }
  162. var rs Result
  163. err = json.Unmarshal(body, &rs)
  164. if err != nil {
  165. return ""
  166. }
  167. if rs.ErrNo != 0 {
  168. return ""
  169. }
  170. logger.Info(rs.PicStr + " 坐标接口")
  171. return rs.PicStr
  172. }
  173. // 超级鹰 坐标点选择
  174. func GetCaptchaCode9101(imgPath string) string {
  175. var client = http.Client{}
  176. client.Timeout = time.Duration(time.Second * 15)
  177. tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
  178. bstr, err := base64Img(imgPath)
  179. if err != nil {
  180. return ""
  181. }
  182. params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9101&file_base64=" + url.QueryEscape(bstr)
  183. req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
  184. if err != nil {
  185. return ""
  186. }
  187. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  188. resp, err := client.Do(req)
  189. if err != nil {
  190. return ""
  191. }
  192. body, err := io.ReadAll(resp.Body)
  193. if err != nil {
  194. return ""
  195. }
  196. var rs Result
  197. err = json.Unmarshal(body, &rs)
  198. if err != nil {
  199. return ""
  200. }
  201. if rs.ErrNo != 0 {
  202. return ""
  203. }
  204. logger.Info(rs.PicStr + " 坐标接口")
  205. return rs.PicStr
  206. }
  207. // 超级鹰 坐标点选择
  208. func GetCaptchaCode9101Err(imgPath string) (string, string) {
  209. var client = http.Client{}
  210. client.Timeout = time.Duration(time.Second * 15)
  211. tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
  212. bstr, err := base64Img(imgPath)
  213. if err != nil {
  214. return "", ""
  215. }
  216. params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=9101&file_base64=" + url.QueryEscape(bstr)
  217. req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
  218. if err != nil {
  219. return "", ""
  220. }
  221. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  222. resp, err := client.Do(req)
  223. if err != nil {
  224. return "", ""
  225. }
  226. body, err := io.ReadAll(resp.Body)
  227. if err != nil {
  228. return "", ""
  229. }
  230. var rs Result
  231. err = json.Unmarshal(body, &rs)
  232. if err != nil {
  233. return "", ""
  234. }
  235. if rs.ErrNo != 0 {
  236. return "", ""
  237. }
  238. logger.Info(rs.PicStr + " 坐标接口")
  239. return rs.PicID, rs.PicStr
  240. }
  241. // 超级鹰 问答题
  242. func GetCaptchaCodeWDT(imgPath string, comID uint, len int) string {
  243. var client = http.Client{}
  244. client.Timeout = time.Duration(time.Second * 15)
  245. tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
  246. bstr, err := base64Img(imgPath)
  247. if err != nil {
  248. return ""
  249. }
  250. params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=6004&file_base64=" + url.QueryEscape(bstr)
  251. req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
  252. if err != nil {
  253. return ""
  254. }
  255. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  256. resp, err := client.Do(req)
  257. if err != nil {
  258. return ""
  259. }
  260. body, err := io.ReadAll(resp.Body)
  261. if err != nil {
  262. return ""
  263. }
  264. var rs Result
  265. err = json.Unmarshal(body, &rs)
  266. if err != nil {
  267. return ""
  268. }
  269. if rs.ErrNo != 0 {
  270. return ""
  271. }
  272. return rs.PicStr
  273. }
  274. func GetCaptchaCodeWDTBase64(bstr string) string {
  275. var client = http.Client{}
  276. client.Timeout = time.Duration(time.Second * 15)
  277. tyURL := "http://upload.chaojiying.net/Upload/Processing.php"
  278. //bstr, err := base64Img(imgPath)
  279. //if err != nil {
  280. // return ""
  281. //}
  282. params := "user=" + user + "&pass=" + pass + "&softid=" + softid + "&codetype=6004&file_base64=" + url.QueryEscape(bstr)
  283. req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
  284. if err != nil {
  285. return ""
  286. }
  287. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  288. resp, err := client.Do(req)
  289. if err != nil {
  290. return ""
  291. }
  292. body, err := io.ReadAll(resp.Body)
  293. if err != nil {
  294. return ""
  295. }
  296. var rs Result
  297. err = json.Unmarshal(body, &rs)
  298. if err != nil {
  299. return ""
  300. }
  301. if rs.ErrNo != 0 {
  302. return ""
  303. }
  304. return rs.PicStr
  305. }
  306. func ReportError(imgid string) error {
  307. var client = http.Client{}
  308. client.Timeout = time.Duration(time.Second * 15)
  309. tyURL := "http://upload.chaojiying.net/Upload/ReportError.php"
  310. params := "user=" + user + "&pass=" + pass + "&id=" + imgid + "&softid=" + softid
  311. req, err := http.NewRequest("POST", tyURL, strings.NewReader(params))
  312. if err != nil {
  313. return err
  314. }
  315. req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
  316. resp, err := client.Do(req)
  317. if err != nil {
  318. return err
  319. }
  320. body, err := io.ReadAll(resp.Body)
  321. if err != nil {
  322. return err
  323. }
  324. logger.Info(string(body))
  325. return nil
  326. }
  327. func base64Img(path string) (string, error) {
  328. imgFile, err := os.Open(path)
  329. if err != nil {
  330. return "", err
  331. }
  332. defer imgFile.Close()
  333. fInfo, _ := imgFile.Stat()
  334. var size int64 = fInfo.Size()
  335. buf := make([]byte, size)
  336. fReader := bufio.NewReader(imgFile)
  337. fReader.Read(buf)
  338. imgBase64str := base64.StdEncoding.EncodeToString(buf)
  339. return imgBase64str, nil
  340. }