url_jx.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. "net/http"
  13. "strings"
  14. )
  15. func DouYinUrlJieXi(c *gin.Context) {
  16. //前端给端视频连接
  17. var inData models.DyJieXiInput
  18. var outData models.DyJieXiOutput
  19. err := c.ShouldBindJSON(&inData)
  20. if err != nil {
  21. outData.Code = apis.Code400
  22. outData.Message = apis.InputFail
  23. c.JSON(http.StatusOK, outData)
  24. return
  25. }
  26. err, outData = DeWater(inData)
  27. if err != nil {
  28. outData.Code = apis.Code500
  29. outData.Message = err.Error()
  30. c.JSON(http.StatusOK, outData)
  31. return
  32. }
  33. outData.Code = apis.Code200
  34. outData.Message = apis.Success
  35. c.JSON(http.StatusOK, outData)
  36. }
  37. func DeWater(in models.DyJieXiInput) (error, models.DyJieXiOutput) {
  38. var out models.DyJieXiOutput
  39. var dysy mysql.DysyUrlInfo
  40. var p []byte
  41. str := ""
  42. baseHost := in.Url
  43. client := &http.Client{
  44. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  45. return http.ErrUseLastResponse
  46. },
  47. }
  48. res, err := client.Get(baseHost)
  49. if err != nil {
  50. fmt.Print(err.Error())
  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. fmt.Print("str = ", str)
  58. comma := strings.Index(str, "/video/")
  59. pos := strings.Index(str, "/?")
  60. //fmt.Println(comma, pos, str[comma+7:pos])
  61. var inData models.DyJieXi
  62. baseHost = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + str[comma+7:pos]
  63. //fmt.Println("baseHost = ", baseHost)
  64. req := requests.Requests()
  65. resp, err := req.Get(baseHost)
  66. if err != nil {
  67. return errors.New(apis.RedirectUrlFail), out
  68. }
  69. err = json.Unmarshal([]byte(resp.Text()), &inData)
  70. if err != nil {
  71. return errors.New(apis.JsonUnmarshalFail), out
  72. //fmt.Println()
  73. }
  74. //视频url
  75. var videoUrl = inData.ItemList[0].Video.PlayAddr.URLList[0]
  76. out.Data.VideoUrl = strings.Replace(videoUrl, "playwm", "play", 1)
  77. //音频url
  78. out.Data.AudioUrl = inData.ItemList[0].Music.PlayURL.URI
  79. out.Data.VideoPic = inData.ItemList[0].Video.DynamicCover.URLList[0]
  80. out.Data.Desc = inData.ItemList[0].Desc
  81. //数据库创建记录
  82. dysy.Desc = out.Data.Desc
  83. dysy.VideoPic = out.Data.VideoPic
  84. dysy.AudioURL = out.Data.AudioUrl
  85. dysy.DouYinShortURL = in.Url
  86. dysy.DeWaterVideoURL = out.Data.VideoUrl
  87. dysy.DouYinBaseHost = baseHost
  88. dysy.Number = 1
  89. dysy.UpdateTime = tools.GetCurrntTimeStr()
  90. dysy.CreateTime = tools.GetCurrntTimeStr()
  91. dysy.Create(dysy.DeWaterVideoURL)
  92. fmt.Println("out.Data.VideoUrl = ", out.Data.VideoUrl)
  93. //client1 := &http.Client{
  94. // CheckRedirect: func(req *http.Request, via []*http.Request) error {
  95. // return http.ErrUseLastResponse
  96. // },
  97. //}
  98. //request.Header.Set("User-Agent", "Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Mobile Safari/537.36") //刚才找到的浏览器中User-agent
  99. //client.Head()
  100. //return nil, out
  101. //res1, err := client1.Get(out.Data.VideoUrl)
  102. //if err != nil {
  103. // fmt.Print(err.Error())
  104. // return errors.New(apis.ClientGetFail), out
  105. //}
  106. //
  107. //ss, err := ioutil.ReadAll(res1.Body)
  108. request1, err := http.NewRequest("GET", out.Data.VideoUrl, nil)
  109. if err != nil {
  110. fmt.Print(err.Error())
  111. }
  112. client1 := &http.Client{
  113. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  114. return http.ErrUseLastResponse
  115. },
  116. }
  117. request1.Header.Set("User-Agent", "Mozilla/5.0 (Linux; Android 6.0.1; Moto G (4)) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Mobile Safari/537.36") //刚才找到的浏览器中User-agent
  118. response, err := client1.Do(request1)
  119. if err != nil {
  120. fmt.Print("err = ", err.Error())
  121. }
  122. loc := response.Header.Get("location")
  123. if loc != "" {
  124. out.Data.VideoUrl = loc
  125. } else {
  126. return errors.New("location error"), out
  127. }
  128. fmt.Print("body = ", response.StatusCode)
  129. fmt.Print("loc =", response.Header.Get("location"))
  130. //if response.StatusCode == 200 {
  131. // body, _ := ioutil.ReadAll(response.Body)
  132. // _ = string(body)
  133. // //fmt.Print("body", bodystr)
  134. //}
  135. //
  136. //fmt.Print("body = ", response.StatusCode)
  137. //fmt.Println("ss = ", string(ss))
  138. //videoStr := string(ss)
  139. //截取字符串
  140. //comma = strings.Index(videoStr, "\"")
  141. //pos = strings.Index(videoStr[comma+1:], "\">")
  142. //fmt.Println(videoStr[comma+1 : pos+comma+1])
  143. out.Data.VideoUrl = strings.Replace(out.Data.VideoUrl, "http", "https", 1)
  144. //out.Data.VideoUrl = out.Data.VideoUrl + ".mp4"
  145. //截取
  146. comma = strings.Index(out.Data.VideoUrl, "https://")
  147. pos = strings.Index(out.Data.VideoUrl[comma+1:], ".com")
  148. //fmt.Println(out.Data.VideoUrl[comma : pos+comma+4+1])
  149. var twoUrl mysql.DysyTwoUrl
  150. twoUrl.CreateTime = tools.GetCurrntTimeStr()
  151. twoUrl.UpdateTime = tools.GetCurrntTimeStr()
  152. twoUrl.TwoURL = out.Data.VideoUrl[comma:pos+comma+4+1] + ";"
  153. twoUrl.Number = 1
  154. twoUrl.Create(twoUrl.TwoURL)
  155. return nil, out
  156. }
  157. func ubaRequestHeader(body string) (header requests.Header, err error) {
  158. // 正确返回
  159. return requests.Header{
  160. "Content-Type": "text/html; charset=utf-8",
  161. "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",
  162. }, nil
  163. }