url_jx.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package dyjx
  2. import (
  3. "dysy/apis"
  4. "dysy/models"
  5. "dysy/models/mysql"
  6. "dysy/tools"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "github.com/asmcos/requests"
  11. "github.com/gin-gonic/gin"
  12. "io/ioutil"
  13. "net/http"
  14. "strings"
  15. )
  16. func DouYinUrlJieXi(c *gin.Context) {
  17. //前端给端视频连接
  18. var inData models.DyJieXiInput
  19. var outData models.DyJieXiOutput
  20. err := c.ShouldBindJSON(&inData)
  21. if err != nil {
  22. outData.Code = apis.Code400
  23. outData.Message = apis.InputFail
  24. c.JSON(http.StatusOK, outData)
  25. return
  26. }
  27. err, outData = DeWater(inData)
  28. if err != nil {
  29. outData.Code = apis.Code500
  30. outData.Message = err.Error()
  31. c.JSON(http.StatusOK, outData)
  32. return
  33. }
  34. outData.Code = apis.Code200
  35. outData.Message = apis.Success
  36. c.JSON(http.StatusOK, outData)
  37. }
  38. func DeWater(in models.DyJieXiInput) (error, models.DyJieXiOutput) {
  39. var out models.DyJieXiOutput
  40. var dysy mysql.DysyUrlInfo
  41. var p []byte
  42. str := ""
  43. baseHost := in.Url
  44. client := &http.Client{
  45. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  46. return http.ErrUseLastResponse
  47. },
  48. }
  49. res, err := client.Get(baseHost)
  50. if err != nil {
  51. return errors.New(apis.ClientGetFail), out
  52. }
  53. n, err := res.Body.Read(p)
  54. fmt.Println("n=", n)
  55. str = res.Header.Get("Location")
  56. //fmt.Println("res.Body = ", string(p))
  57. comma := strings.Index(str, "/video/")
  58. pos := strings.Index(str, "/?")
  59. //fmt.Println(comma, pos, str[comma+7:pos])
  60. var inData models.DyJieXi
  61. baseHost = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + str[comma+7:pos]
  62. //fmt.Println("baseHost = ", baseHost)
  63. req := requests.Requests()
  64. resp, err := req.Get(baseHost)
  65. if err != nil {
  66. return errors.New(apis.RedirectUrlFail), out
  67. }
  68. err = json.Unmarshal([]byte(resp.Text()), &inData)
  69. if err != nil {
  70. return errors.New(apis.JsonUnmarshalFail), out
  71. //fmt.Println()
  72. }
  73. //视频url
  74. var videoUrl = inData.ItemList[0].Video.PlayAddr.URLList[0]
  75. out.Data.VideoUrl = strings.Replace(videoUrl, "playwm", "play", 1)
  76. //音频url
  77. out.Data.AudioUrl = inData.ItemList[0].Music.PlayURL.URI
  78. out.Data.VideoPic = inData.ItemList[0].Video.DynamicCover.URLList[0]
  79. out.Data.Desc = inData.ItemList[0].Desc
  80. //数据库创建记录
  81. dysy.Desc = out.Data.Desc
  82. dysy.VideoPic = out.Data.VideoPic
  83. dysy.AudioURL = out.Data.AudioUrl
  84. dysy.DouYinShortURL = in.Url
  85. dysy.DeWaterVideoURL = out.Data.VideoUrl
  86. dysy.DouYinBaseHost = baseHost
  87. dysy.Number = 1
  88. dysy.UpdateTime = tools.GetCurrntTimeStr()
  89. dysy.CreateTime = tools.GetCurrntTimeStr()
  90. dysy.Create(dysy.DeWaterVideoURL)
  91. fmt.Println("out.Data.VideoUrl = ", out.Data.VideoUrl)
  92. client1 := &http.Client{
  93. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  94. return http.ErrUseLastResponse
  95. },
  96. }
  97. //client.Head()
  98. res1, err := client1.Get(out.Data.VideoUrl)
  99. if err != nil {
  100. return errors.New(apis.ClientGetFail), out
  101. }
  102. ss, err := ioutil.ReadAll(res1.Body)
  103. //fmt.Println(string(ss))
  104. videoStr := string(ss)
  105. //截取字符串
  106. comma = strings.Index(videoStr, "\"")
  107. pos = strings.Index(videoStr[comma+1:], "\">")
  108. //fmt.Println(videoStr[comma+1 : pos+comma+1])
  109. out.Data.VideoUrl = strings.Replace(videoStr[comma+1:pos+comma+1], "http", "https", 1)
  110. out.Data.VideoUrl = out.Data.VideoUrl + ".mp4"
  111. //截取
  112. comma = strings.Index(out.Data.VideoUrl, "https://")
  113. pos = strings.Index(out.Data.VideoUrl[comma+1:], ".com")
  114. //fmt.Println(out.Data.VideoUrl[comma : pos+comma+4+1])
  115. var twoUrl mysql.DysyTwoUrl
  116. twoUrl.CreateTime = tools.GetCurrntTimeStr()
  117. twoUrl.UpdateTime = tools.GetCurrntTimeStr()
  118. twoUrl.TwoURL = out.Data.VideoUrl[comma:pos+comma+4+1] + ";"
  119. twoUrl.Number = 1
  120. twoUrl.Create(twoUrl.TwoURL)
  121. return nil, out
  122. }
  123. func ubaRequestHeader(body string) (header requests.Header, err error) {
  124. // 正确返回
  125. return requests.Header{
  126. "Content-Type": "text/html; charset=utf-8",
  127. "User-Agent": "Mozilla/5.0 (Linux; Android 9.0; Z832 Build/MMB29M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Mobile Safari/537.36",
  128. }, nil
  129. }