xfyun.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package services
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "eta_gn/eta_api/models"
  6. "fmt"
  7. "github.com/rdlucklib/rdluck_tools/crypt"
  8. "net/url"
  9. "strings"
  10. "time"
  11. "eta_gn/eta_api/utils"
  12. "github.com/gorilla/websocket"
  13. )
  14. type XfParams struct {
  15. XfAPPID string
  16. XfAPIKey string
  17. XfAPISecret string
  18. }
  19. func GetXfVideo(body []byte, savePath string, req XfParams) (err error) {
  20. path, err := assembleAuthUrl(req)
  21. if err != nil {
  22. return
  23. }
  24. conn, _, err := websocket.DefaultDialer.Dial(path, nil)
  25. if err != nil {
  26. return
  27. }
  28. defer conn.Close()
  29. err = conn.WriteMessage(websocket.TextMessage, body)
  30. if err != nil {
  31. return
  32. }
  33. for {
  34. _, message, err := conn.ReadMessage()
  35. if err != nil {
  36. fmt.Println("ReadMessage Err:" + err.Error())
  37. return err
  38. }
  39. item := new(models.XfReciveResult)
  40. err = json.Unmarshal(message, &item)
  41. if err != nil {
  42. fmt.Println("json.Unmarshal Err:" + err.Error())
  43. return err
  44. }
  45. if item.Code != 0 {
  46. goto readWebSocketFail
  47. }
  48. if item.Code == 0 && item.Data != nil {
  49. if item.Data.Status == 1 {
  50. audio := item.Data.Audio
  51. err = utils.SaveBase64ToFileBySeek(audio, savePath)
  52. if err != nil {
  53. fmt.Println("文件保存失败", err.Error())
  54. goto readWebSocketFail
  55. }
  56. } else {
  57. audio := item.Data.Audio
  58. err = utils.SaveBase64ToFileBySeek(audio, savePath)
  59. if err != nil {
  60. fmt.Println("文件保存失败", err.Error())
  61. goto webSocketClose
  62. }
  63. fmt.Println("goto close")
  64. goto webSocketClose
  65. }
  66. }
  67. }
  68. readWebSocketFail:
  69. conn.Close()
  70. fmt.Println("goto readWebSocketFail")
  71. webSocketClose:
  72. conn.Close()
  73. fmt.Println("goto webSocketClose")
  74. return nil
  75. }
  76. func assembleAuthUrl(req XfParams) (callUrl string, err error) {
  77. ul, err := url.Parse(utils.XfHostUrl)
  78. if err != nil {
  79. return
  80. }
  81. date := time.Now().UTC().Format(time.RFC1123)
  82. signString := []string{"host: " + ul.Host, "date: " + date, "GET " + ul.Path + " HTTP/1.1"}
  83. sign := strings.Join(signString, "\n")
  84. fmt.Println("sign:", sign)
  85. sha := crypt.HmacSha256EncryptToBase64([]byte(sign), []byte(req.XfAPISecret))
  86. authUrl := fmt.Sprintf("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", req.XfAPIKey,
  87. "hmac-sha256", "host date request-line", sha)
  88. authorization := base64.StdEncoding.EncodeToString([]byte(authUrl))
  89. v := url.Values{}
  90. v.Add("host", ul.Host)
  91. v.Add("date", date)
  92. v.Add("authorization", authorization)
  93. callUrl = utils.XfHostUrl + "?" + v.Encode()
  94. return
  95. }