google_translate.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "hongze/hongze_open_api/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "strconv"
  10. )
  11. var (
  12. googleTranslateUrl = "http://47.254.37.124:8396/api/google/translate"
  13. googleSingleTranslateUrl = "http://47.254.37.124:8396/api/google/translate/single"
  14. )
  15. type GoogleApiResp struct {
  16. Code int `json:"code"`
  17. Msg string `json:"msg"`
  18. ErrMsg string `json:"err_msg"`
  19. Data GoogleApiRespData `json:"data"`
  20. }
  21. type GoogleApiRespData struct {
  22. ContentEn string `json:"content_en"`
  23. }
  24. // content 为json字符串
  25. func GoogleTranslateApi(content string) (contentEnMap map[string]string, err error, errMsg string) {
  26. params := make(map[string]interface{})
  27. params["content"] = content
  28. bytesData, err := json.Marshal(params)
  29. if err != nil {
  30. err = fmt.Errorf("json.Marshal Err:" + err.Error())
  31. return
  32. }
  33. utils.FileLog.Info("googleTranslateData:" + string(bytesData))
  34. // 将解析之后的数据转为*Reader类型
  35. reader := bytes.NewReader(bytesData)
  36. resp, err := http.Post(googleTranslateUrl, "application/json", reader)
  37. if err != nil {
  38. errMsg = err.Error()
  39. err = fmt.Errorf("调用谷歌翻译接口失败")
  40. return
  41. }
  42. defer resp.Body.Close()
  43. if resp.StatusCode != 200 {
  44. err = fmt.Errorf("调用谷歌翻译接口失败")
  45. errMsg = "Status Code: " + strconv.Itoa(resp.StatusCode)
  46. return
  47. }
  48. body, _ := ioutil.ReadAll(resp.Body)
  49. //fmt.Println(string(body))
  50. var ret GoogleApiResp
  51. err = json.Unmarshal(body, &ret)
  52. if err != nil {
  53. errMsg = "解析谷歌翻译接口返回值失败 json.Unmarshal Err " + err.Error()
  54. err = fmt.Errorf("解析谷歌翻译接口返回值失败")
  55. return
  56. }
  57. if ret.Code != 200 {
  58. err = fmt.Errorf("翻译失败 GoogleTranslateApi:" + ret.Msg)
  59. errMsg = "ret code:" + strconv.Itoa(ret.Code) + ret.ErrMsg
  60. return
  61. }
  62. contentEnMap = make(map[string]string, 0)
  63. err = json.Unmarshal([]byte(ret.Data.ContentEn), &contentEnMap)
  64. if err != nil {
  65. errMsg = "解析谷歌翻译接口返回值失败2 json.Unmarshal Err " + err.Error()
  66. err = fmt.Errorf("解析谷歌翻译接口返回值失败")
  67. return
  68. }
  69. return
  70. }
  71. // GoogleSingleTranslateApi content 为简单字符串
  72. func GoogleSingleTranslateApi(content string) (contentEn string, err error, errMsg string) {
  73. params := make(map[string]interface{})
  74. params["content"] = content
  75. bytesData, err := json.Marshal(params)
  76. if err != nil {
  77. err = fmt.Errorf("json.Marshal Err:" + err.Error())
  78. return
  79. }
  80. // 将解析之后的数据转为*Reader类型
  81. reader := bytes.NewReader(bytesData)
  82. resp, err := http.Post(googleSingleTranslateUrl, "application/json", reader)
  83. if err != nil {
  84. errMsg = err.Error()
  85. err = fmt.Errorf("调用谷歌翻译接口失败")
  86. return
  87. }
  88. defer resp.Body.Close()
  89. if resp.StatusCode != 200 {
  90. err = fmt.Errorf("调用谷歌翻译接口失败")
  91. errMsg = "Status Code: " + strconv.Itoa(resp.StatusCode)
  92. return
  93. }
  94. body, _ := ioutil.ReadAll(resp.Body)
  95. //fmt.Println(string(body))
  96. var ret GoogleApiResp
  97. err = json.Unmarshal(body, &ret)
  98. if err != nil {
  99. errMsg = "解析谷歌翻译接口返回值失败 json.Unmarshal Err " + err.Error()
  100. err = fmt.Errorf("解析谷歌翻译接口返回值失败")
  101. return
  102. }
  103. if ret.Code != 200 {
  104. err = fmt.Errorf("翻译失败:" + ret.Msg)
  105. errMsg = "ret code:" + strconv.Itoa(ret.Code) + ret.ErrMsg
  106. return
  107. }
  108. contentEn = ret.Data.ContentEn
  109. return
  110. }