url_jx.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. fmt.Print(err.Error())
  52. return errors.New(apis.ClientGetFail), out
  53. }
  54. n, err := res.Body.Read(p)
  55. fmt.Println("n=", n)
  56. str = res.Header.Get("Location")
  57. fmt.Println("res.Body = ", string(p))
  58. fmt.Print("str = ", str)
  59. comma := strings.Index(str, "/video/")
  60. pos := strings.Index(str, "/?")
  61. //fmt.Println(comma, pos, str[comma+7:pos])
  62. var inData models.DyJieXi
  63. baseHost = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=" + str[comma+7:pos]
  64. //fmt.Println("baseHost = ", baseHost)
  65. req := requests.Requests()
  66. resp, err := req.Get(baseHost)
  67. if err != nil {
  68. return errors.New(apis.RedirectUrlFail), out
  69. }
  70. err = json.Unmarshal([]byte(resp.Text()), &inData)
  71. if err != nil {
  72. return errors.New(apis.JsonUnmarshalFail), out
  73. //fmt.Println()
  74. }
  75. //视频url
  76. var videoUrl = inData.ItemList[0].Video.PlayAddr.URLList[0]
  77. out.Data.VideoUrl = strings.Replace(videoUrl, "playwm", "play", 1)
  78. //音频url
  79. out.Data.AudioUrl = inData.ItemList[0].Music.PlayURL.URI
  80. out.Data.VideoPic = inData.ItemList[0].Video.DynamicCover.URLList[0]
  81. out.Data.Desc = inData.ItemList[0].Desc
  82. //数据库创建记录
  83. dysy.Desc = out.Data.Desc
  84. dysy.VideoPic = out.Data.VideoPic
  85. dysy.AudioURL = out.Data.AudioUrl
  86. dysy.DouYinShortURL = in.Url
  87. dysy.DeWaterVideoURL = out.Data.VideoUrl
  88. dysy.DouYinBaseHost = baseHost
  89. dysy.Number = 1
  90. dysy.UpdateTime = tools.GetCurrntTimeStr()
  91. dysy.CreateTime = tools.GetCurrntTimeStr()
  92. dysy.Create(dysy.DeWaterVideoURL)
  93. fmt.Println("out.Data.VideoUrl = ", out.Data.VideoUrl)
  94. client1 := &http.Client{
  95. CheckRedirect: func(req *http.Request, via []*http.Request) error {
  96. return http.ErrUseLastResponse
  97. },
  98. }
  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. ss, err := ioutil.ReadAll(res1.Body)
  107. //fmt.Println(string(ss))
  108. videoStr := string(ss)
  109. //截取字符串
  110. comma = strings.Index(videoStr, "\"")
  111. pos = strings.Index(videoStr[comma+1:], "\">")
  112. //fmt.Println(videoStr[comma+1 : pos+comma+1])
  113. out.Data.VideoUrl = strings.Replace(videoStr[comma+1:pos+comma+1], "http", "https", 1)
  114. out.Data.VideoUrl = out.Data.VideoUrl + ".mp4"
  115. //截取
  116. comma = strings.Index(out.Data.VideoUrl, "https://")
  117. pos = strings.Index(out.Data.VideoUrl[comma+1:], ".com")
  118. //fmt.Println(out.Data.VideoUrl[comma : pos+comma+4+1])
  119. var twoUrl mysql.DysyTwoUrl
  120. twoUrl.CreateTime = tools.GetCurrntTimeStr()
  121. twoUrl.UpdateTime = tools.GetCurrntTimeStr()
  122. twoUrl.TwoURL = out.Data.VideoUrl[comma:pos+comma+4+1] + ";"
  123. twoUrl.Number = 1
  124. twoUrl.Create(twoUrl.TwoURL)
  125. return nil, out
  126. }
  127. func ubaRequestHeader(body string) (header requests.Header, err error) {
  128. // 正确返回
  129. return requests.Header{
  130. "Content-Type": "text/html; charset=utf-8",
  131. "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",
  132. }, nil
  133. }