chaojiying.go 8.5 KB

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