Skip to content

Commit

Permalink
完成
Browse files Browse the repository at this point in the history
  • Loading branch information
pwh-pwh committed Jul 9, 2022
1 parent 8454c68 commit 0a4bfaf
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# douyinDownload_go
基于go版本的抖音视频去水印下载

# eg. localhost:8080/download?url=https://v.douyin.com/JyCk5gy&fileName=abc.mp4

## 注意⚠️
- 本项目仅供学习参考,请勿用于非法用途,如有侵权,请联系我删除
106 changes: 106 additions & 0 deletions client/MyClient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package client

import (
"fmt"
"github.com/tidwall/gjson"
"io"
"log"
"net/http"
"os"
"regexp"
"strings"
)

const (
pattern = "video/(\\d+)?"
cVUrl = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids=%s"
)

var esc_re, _ = regexp.Compile(pattern)

func doGet(url string) (*http.Response, error) {
client := &http.Client{}
request, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
log.Fatal("err", err)
}
request.Header.Set("User-Agent", "Mozilla/5.0 (Linux; Android 11; SAMSUNG SM-G973U) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/14.2 Chrome/87.0.4280.141 Mobile Safari/537.36")
return client.Do(request)
}

func GetVideoId(url string) (s string, err error) {
resp, err := doGet(url)
if err != nil {
fmt.Println("get error", err)
return
}
defer resp.Body.Close()
s = resp.Request.URL.String()
s = esc_re.FindStringSubmatch(s)[1]
return
}

func GetVideoDlUrl(id string) (s string, err error) {
ts := fmt.Sprintf(cVUrl, id)
//fmt.Println(ts)
resp, err := doGet(ts)
if err != nil {
fmt.Println("get error", err)
return
}
defer resp.Body.Close()
var bytes = make([]byte, 128)
var builder strings.Builder
for {
rLen, err := resp.Body.Read(bytes)
if err == io.EOF {
break
}
if err != nil {
return "", err
}
builder.Write(bytes[:rLen])
}

if err != nil {
return
}
//fmt.Println(builder.String())
s = gjson.Get(builder.String(), "item_list.0.video.play_addr.url_list.0").String()
return
}

func DownloadVideo(url, fileName string) error {
resp, err := doGet(url)
if err != nil {
return nil
}
defer resp.Body.Close()
file, err := os.Create(fileName)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return err
}
return err
}

func GetBody(url string) (io.ReadCloser, error, int64) {
id, err := GetVideoId(url)
if err != nil {
return nil, err, 0
}
dlUrl, err := GetVideoDlUrl(id)
if err != nil {
return nil, err, 0
}
resp, err := doGet(dlUrl)
if err != nil {
return nil, err, 0
}
return resp.Body, nil, resp.ContentLength
}
12 changes: 12 additions & 0 deletions client/downloadVideo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package client

import (
"fmt"
"testing"
)

func TestDownloadVideo(t *testing.T) {
err := DownloadVideo("https://aweme.snssdk.com/aweme/v1/playwm/?video_id=v0200f230000btcaac52m1gham4830p0&ratio=720p&line=0",
"aaa.mp4")
fmt.Println(err)
}
14 changes: 14 additions & 0 deletions client/getBody_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package client

import (
"fmt"
"testing"
)

func TestGetBody(t *testing.T) {
body, err := GetBody("https://v.douyin.com/JyCk5gy/")
if err != nil {
fmt.Println(err)
}
fmt.Println(body)
}
11 changes: 11 additions & 0 deletions client/getVideoDlUrl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package client

import (
"fmt"
"testing"
)

func TestGetVideoDlUrl(t *testing.T) {
url, _ := GetVideoDlUrl("6870423037087436046")
fmt.Println(url)
}
16 changes: 16 additions & 0 deletions client/getvideoId_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package client

import (
"fmt"
"testing"
)

func TestGetVideoId(t *testing.T) {
id, err := GetVideoId("https://v.douyin.com/JyCk5gy/")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(id, "--")
}

}
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/pwh-pwh/douyinDownload_go/server"

func main() {
server.StartServer()
}
32 changes: 32 additions & 0 deletions server/douyinServer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package server

import (
"github.com/gin-gonic/gin"
"github.com/pwh-pwh/douyinDownload_go/client"
"net/http"
)

func StartServer() {
r := gin.Default()
r.GET("/download", downloadVideo)
r.Run(":8080")
}

func downloadVideo(c *gin.Context) {
url := c.Query("url")
fileName := c.Query("fileName")
body, err, length := client.GetBody(url)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": err.Error(),
})
}
if body != nil {
defer body.Close()
}
extraHeaders := map[string]string{
"Content-Disposition": `attachment; filename="` + fileName + `"`,
}
c.DataFromReader(http.StatusOK, length, "application/octet-stream",
body, extraHeaders)
}

0 comments on commit 0a4bfaf

Please sign in to comment.