-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
## 注意⚠️ | ||
- 本项目仅供学习参考,请勿用于非法用途,如有侵权,请联系我删除 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "--") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |