Skip to content

Commit

Permalink
使用 Douyin_TikTok_Download_API 项目实现抖音视频解析接口
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeilin01 committed Aug 20, 2023
1 parent ba4f7a2 commit 7859f75
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 65 deletions.
8 changes: 7 additions & 1 deletion admin/controllers/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package controllers

import (
"strings"
"sync"

"github.com/beego/beego/v2/server/web"
"github.com/lifei6671/douyinbot/admin/structs"
"github.com/lifei6671/douyinbot/douyin"
)

var douYin = douyin.NewDouYin(web.AppConfig.DefaultString("douyinproxy", ""))
var douYin *douyin.DouYin
var once sync.Once

var (
videoHtml = `<video controls="controls" autoplay="autoplay" width="100%"><source src="{{__VIDEO__}}" type="video/mp4"></video>`
)
Expand All @@ -18,6 +21,9 @@ type HomeController struct {
}

func (c *HomeController) Index() {
once.Do(func() {
douYin = douyin.NewDouYin(web.AppConfig.DefaultString("douyinproxy", ""))
})
if c.Ctx.Input.IsGet() {
c.TplName = "home/index.gohtml"
} else {
Expand Down
121 changes: 58 additions & 63 deletions admin/service/crontab.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,76 @@ package service

import (
"context"
"errors"
"github.com/beego/beego/v2/client/orm"
"github.com/beego/beego/v2/core/logs"
"github.com/beego/beego/v2/server/web"
"github.com/lifei6671/douyinbot/admin/models"
"github.com/lifei6671/douyinbot/douyin"
"github.com/lifei6671/douyinbot/internal/utils"
"golang.org/x/time/rate"
"strings"
"sync"
"time"
)

var _cronCh = make(chan string, 100)

// RunCron 运行定时任务
func RunCron(ctx context.Context) {
go func() {
once := sync.Once{}
timer := time.NewTicker(time.Second * 1)
defer timer.Stop()
for {
select {
case <-timer.C:
coverList, err := models.NewDouYinCover().GetExpireList()
if err != nil && !errors.Is(err, orm.ErrNoRows) {
logs.Error("查询过期列表失败 : %+v", err)
}
logs.Info("cover len", len(coverList))
if err == nil {
for _, cover := range coverList {
if cover.Expires > 0 {
_cronCh <- cover.VideoId
}
}
}
once.Do(func() {
timer.Reset(time.Minute * 30)
})
case <-ctx.Done():
return
}
}

}()
go func() {
var limiter = rate.NewLimiter(rate.Every(time.Second*2), 1)
for {
func() {
waitCtx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
//等待1s
_ = limiter.Wait(waitCtx)
limiter.Allow()
}()

select {
case videoId, ok := <-_cronCh:
if !ok {
return
}

err := syncCover(videoId)
if err != nil {
logs.Error("更新封面失败: 【%s】 %+v", videoId, err)
} else {
logs.Info("更新封面成功: %s", videoId)
}
break

case <-ctx.Done():
return
}
}
}()
//go func() {
// once := sync.Once{}
// timer := time.NewTicker(time.Second * 1)
// defer timer.Stop()
// for {
// select {
// case <-timer.C:
// coverList, err := models.NewDouYinCover().GetExpireList()
// if err != nil && !errors.Is(err, orm.ErrNoRows) {
// logs.Error("查询过期列表失败 : %+v", err)
// }
// logs.Info("cover len", len(coverList))
// if err == nil {
// for _, cover := range coverList {
// if cover.Expires > 0 {
// _cronCh <- cover.VideoId
// }
// }
// }
// once.Do(func() {
// timer.Reset(time.Minute * 30)
// })
// case <-ctx.Done():
// return
// }
// }
//
//}()
//go func() {
// var limiter = rate.NewLimiter(rate.Every(time.Second*2), 1)
// for {
// func() {
// waitCtx, cancel := context.WithTimeout(context.Background(), time.Second)
// defer cancel()
// //等待1s
// _ = limiter.Wait(waitCtx)
// limiter.Allow()
// }()
//
// select {
// case videoId, ok := <-_cronCh:
// if !ok {
// return
// }
//
// err := syncCover(videoId)
// if err != nil {
// logs.Error("更新封面失败: 【%s】 %+v", videoId, err)
// } else {
// logs.Info("更新封面成功: %s", videoId)
// }
// break
//
// case <-ctx.Done():
// return
// }
// }
//}()
}

// syncCover 同步过期的封面
Expand Down
3 changes: 2 additions & 1 deletion douyin/douyin.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,11 @@ func (d *DouYin) GetDetailUrlByVideoId(videoId string) (string, error) {
func (d *DouYin) parseShareUrl(shareUrl string) (string, error) {
proxyURL := d.proxy + "?url=" + shareUrl
client := resty.New()
client.SetRedirectPolicy(resty.NoRedirectPolicy())
//client.SetRedirectPolicy(resty.NoRedirectPolicy())
res, err := client.R().
SetHeader("User-Agent", DefaultUserAgent).
Get(proxyURL)

// 这里会返回err, auto redirect is disabled
if err != nil {
return "", err
Expand Down

0 comments on commit 7859f75

Please sign in to comment.