diff --git a/cmd/cmd.go b/cmd/cmd.go index 3062c04..09308a6 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -3,10 +3,12 @@ package main import ( "fmt" "log" + "math/rand" "net/url" "os" "strings" "sync" + "time" "github.com/nanlei2000/douyin_download/pkg/douyin" "github.com/urfave/cli/v2" @@ -86,6 +88,9 @@ func main() { <-c }() + ran := rand.Int31n(100) + time.Sleep(time.Duration(ran) * time.Millisecond) + video, err := dy.Get(douyin.Source{ Type: douyin.SourceType_VideoID, Content: id, diff --git a/pkg/douyin/post.go b/pkg/douyin/post.go index dc655dc..00376fd 100644 --- a/pkg/douyin/post.go +++ b/pkg/douyin/post.go @@ -38,6 +38,8 @@ func (d *DouYin) GetAllVideoIDList(secUid string) ([]string, error) { } defer resp.Body.Close() + d.printf("resp status: %s", resp.Status) + body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Printf("fail to read body, secUid: %s, err: %s", secUid, err) diff --git a/pkg/douyin/post_test.go b/pkg/douyin/post_test.go index 7fbd902..83767ff 100644 --- a/pkg/douyin/post_test.go +++ b/pkg/douyin/post_test.go @@ -6,7 +6,9 @@ import ( ) func TestGetAllVideoIDList(t *testing.T) { - dy := DouYin{} + dy := NewDouYin() + dy.isDebug = true + list, err := dy.GetAllVideoIDList("MS4wLjABAAAAF0HqDm-8U9TiT_9AfqqPGiNbrP0c93AdB3_oRG7Em_Q") if err != nil { diff --git a/pkg/douyin/video.go b/pkg/douyin/video.go index 90ec38c..21ab2fc 100644 --- a/pkg/douyin/video.go +++ b/pkg/douyin/video.go @@ -78,14 +78,14 @@ func (v *Video) Download(filename string) (string, error) { dir := filepath.Dir(filename) if _, err := os.Stat(dir); os.IsNotExist(err) { - if err := os.MkdirAll(dir, 0655); err != nil { + if err := os.MkdirAll(dir, os.ModePerm); err != nil { return "", err } } //如果是图片类,则将图片下载到指定目录 if v.VideoType == ImagePlayType { imagePath := filepath.Join(dir, v.VideoId) - if err := os.MkdirAll(imagePath, 0655); err != nil { + if err := os.MkdirAll(imagePath, os.ModePerm); err != nil { log.Printf("创建目录失败 [path=%s]", imagePath) } for _, image := range v.Images { @@ -122,7 +122,7 @@ func (v *Video) Download(filename string) (string, error) { continue } _ = resp.Body.Close() - err = ioutil.WriteFile(imageName, b, 0655) + err = ioutil.WriteFile(imageName, b, os.ModePerm) if err != nil { log.Printf("保存图像出错 -> [play_id=%s] [image_url=%s]", v.PlayId, image.ImageUrl) continue