k.zhang 4 years ago
parent
commit
db10161841
2 changed files with 126 additions and 14 deletions
  1. 78 0
      apis/dyjx/url2_test.go
  2. 48 14
      apis/dyjx/url_jx.go

+ 78 - 0
apis/dyjx/url2_test.go

@@ -0,0 +1,78 @@
+package dyjx
+
+import (
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"testing"
+)
+
+func TestUrl2(t *testing.T) {
+
+	//res, err := http.Get("https://aweme.snssdk.com/aweme/v1/play/?video_id=v0300fg10000c2pqsiu8valn90t2ugq0&ratio=720p&line=0")
+	//if err != nil {
+	//	fmt.Print(err.Error())
+	//}
+	//body, _ := ioutil.ReadAll(res.Body) //转换byte数组
+	////defer res.Body.Close()
+	////io.Copy(os.Stdout, res.Body)//写到输出流,
+	//bodystr := string(body) //转换字符串
+	//fmt.Print("body = ", bodystr)
+
+	//client := &http.Client{}
+	//reqest, _ := http.NewRequest("GET", "https://aweme.snssdk.com/aweme/v1/play/?video_id=v0300fg10000c2pqsiu8valn90t2ugq0&ratio=720p&line=0", nil)
+	//
+	//reqest.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
+	//reqest.Header.Set("Accept-Charset", "utf-8;q=0.7,*;q=0.3")
+	////reqest.Header.Set("Accept-Encoding", "gzip, default")//这个有乱码,估计是没有解密,或解压缩
+	//reqest.Header.Set("Accept-Encoding", "utf-8") //这就没有乱码了
+	//reqest.Header.Set("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3")
+	//reqest.Header.Set("Cache-Control", "max-age=0")
+	//reqest.Header.Set("Connection", "keep-alive")
+	//reqest.Header.Set("Host", "https://aweme.snssdk.com/aweme/v1/play/?video_id=v0300fg10000c2pqsiu8valn90t2ugq0&ratio=720p&line=0")
+	//reqest.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0")
+	//
+	//response, _ := client.Do(reqest)
+	//if response.StatusCode == 200 {
+	//	body, _ := ioutil.ReadAll(response.Body)
+	//	bodystr := string(body)
+	//	fmt.Print("body", bodystr)
+	//}
+
+	request, err := http.NewRequest("GET", "https://aweme.snssdk.com/aweme/v1/play/?video_id=v0300fg10000c2pqsiu8valn90t2ugq0&ratio=720p&line=0", nil)
+	if err != nil {
+		fmt.Print(err.Error())
+	}
+	// 设置请求投
+	//request.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
+	//request.Header.Add("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3")
+	//request.Header.Add("Connection", "keep-alive")
+	//request.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
+	//request.Header.Set("Accept-Encoding", "utf-8") //这就没有乱码了
+	client := &http.Client{
+		CheckRedirect: func(req *http.Request, via []*http.Request) error {
+			return http.ErrUseLastResponse
+		},
+	}
+	// Do sends an HTTP request and returns an HTTP response
+	// 发起一个HTTP请求,返回一个HTTP响应
+	//client.Do(request)
+
+	//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
+	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
+	response, err := client.Do(request)
+
+	if err != nil {
+		fmt.Print("err = ", err.Error())
+	}
+	fmt.Print("body = ", response.StatusCode)
+	fmt.Print("loc =", response.Header.Get("location"))
+	if response.StatusCode == 200 {
+		body, _ := ioutil.ReadAll(response.Body)
+		_ = string(body)
+		//fmt.Print("body", bodystr)
+	}
+
+	fmt.Print("body = ", response.StatusCode)
+
+}

+ 48 - 14
apis/dyjx/url_jx.go

@@ -10,7 +10,6 @@ import (
 	"fmt"
 	"github.com/asmcos/requests"
 	"github.com/gin-gonic/gin"
-	"io/ioutil"
 	"net/http"
 	"strings"
 )
@@ -112,30 +111,65 @@ func DeWater(in models.DyJieXiInput) (error, models.DyJieXiOutput) {
 
 	fmt.Println("out.Data.VideoUrl = ", out.Data.VideoUrl)
 
+	//client1 := &http.Client{
+	//	CheckRedirect: func(req *http.Request, via []*http.Request) error {
+	//		return http.ErrUseLastResponse
+	//	},
+	//}
+	//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
+
+	//client.Head()
+
+	//return nil, out
+
+	//res1, err := client1.Get(out.Data.VideoUrl)
+	//if err != nil {
+	//	fmt.Print(err.Error())
+	//	return errors.New(apis.ClientGetFail), out
+	//}
+	//
+	//ss, err := ioutil.ReadAll(res1.Body)
+	request1, err := http.NewRequest("GET", "https://aweme.snssdk.com/aweme/v1/play/?video_id=v0300fg10000c2pqsiu8valn90t2ugq0&ratio=720p&line=0", nil)
+	if err != nil {
+		fmt.Print(err.Error())
+	}
+
 	client1 := &http.Client{
 		CheckRedirect: func(req *http.Request, via []*http.Request) error {
 			return http.ErrUseLastResponse
 		},
 	}
-	//client.Head()
-
-	//return nil, out
+	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
+	response, err := client1.Do(request1)
 
-	res1, err := client1.Get(out.Data.VideoUrl)
 	if err != nil {
-		fmt.Print(err.Error())
-		return errors.New(apis.ClientGetFail), out
+		fmt.Print("err = ", err.Error())
+	}
+	loc := response.Header.Get("location")
+	if loc != "" {
+		out.Data.VideoUrl = loc
+	} else {
+		return errors.New("location error"), out
 	}
-	ss, err := ioutil.ReadAll(res1.Body)
-	fmt.Println("ss = ", string(ss))
-	videoStr := string(ss)
+	fmt.Print("body = ", response.StatusCode)
+	fmt.Print("loc =", response.Header.Get("location"))
+	//if response.StatusCode == 200 {
+	//	body, _ := ioutil.ReadAll(response.Body)
+	//	_ = string(body)
+	//	//fmt.Print("body", bodystr)
+	//}
+	//
+	//fmt.Print("body = ", response.StatusCode)
+
+	//fmt.Println("ss = ", string(ss))
+	//videoStr := string(ss)
 	//截取字符串
-	comma = strings.Index(videoStr, "\"")
-	pos = strings.Index(videoStr[comma+1:], "\">")
+	//comma = strings.Index(videoStr, "\"")
+	//pos = strings.Index(videoStr[comma+1:], "\">")
 
 	//fmt.Println(videoStr[comma+1 : pos+comma+1])
-	out.Data.VideoUrl = strings.Replace(videoStr[comma+1:pos+comma+1], "http", "https", 1)
-	out.Data.VideoUrl = out.Data.VideoUrl + ".mp4"
+	out.Data.VideoUrl = strings.Replace(out.Data.VideoUrl, "http", "https", 1)
+	//out.Data.VideoUrl = out.Data.VideoUrl + ".mp4"
 
 	//截取
 	comma = strings.Index(out.Data.VideoUrl, "https://")