forked from cnbattle/douyin
-
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
6 changed files
with
221 additions
and
204 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
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,59 +1,7 @@ | ||
package adb | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cnbattle/douyin/config" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"time" | ||
) | ||
import "github.com/cnbattle/douyin/internal/adb" | ||
|
||
func Run() { | ||
START: | ||
start := time.Now().Unix() | ||
runApp() | ||
defer closeApp() | ||
for { | ||
now := time.Now().Unix() | ||
if now > start+config.V.GetInt64("app.restart") { | ||
time.Sleep(config.V.GetDuration("app.sleep") * time.Second) | ||
goto START | ||
} | ||
swipe() | ||
time.Sleep(config.V.GetDuration("swipe.sleep") * time.Millisecond) | ||
} | ||
} | ||
|
||
func runApp() { | ||
closeApp() | ||
cmd := exec.Command(getRunCli(), "shell", "am", "start", "-n", fmt.Sprintf("%v/%v", | ||
config.V.GetString("app.packageName"), config.V.GetString("app.startPath"), | ||
)) | ||
cmd.Stdout = os.Stdout | ||
_ = cmd.Run() | ||
} | ||
|
||
func swipe() { | ||
cmd := exec.Command(getRunCli(), "shell", "input", "swipe", | ||
config.V.GetString("swipe.startX"), | ||
config.V.GetString("swipe.startY"), | ||
config.V.GetString("swipe.endX"), | ||
config.V.GetString("swipe.endY"), | ||
) | ||
cmd.Stdout = os.Stdout | ||
_ = cmd.Run() | ||
} | ||
|
||
func closeApp() { | ||
cmd := exec.Command(getRunCli(), "shell", "am", "force-stop", config.V.GetString("app.packageName")) | ||
cmd.Stdout = os.Stdout | ||
_ = cmd.Run() | ||
} | ||
|
||
func getRunCli() string { | ||
if runtime.GOOS == "windows" { | ||
return "./static/adb/adb.exe" | ||
} | ||
return "adb" | ||
func main() { | ||
adb.Start() | ||
} |
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,141 +1,9 @@ | ||
package web | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/cnbattle/douyin/config" | ||
"github.com/cnbattle/douyin/database" | ||
"github.com/cnbattle/douyin/model" | ||
"github.com/cnbattle/douyin/utils" | ||
"github.com/gin-gonic/gin" | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"github.com/cnbattle/douyin/internal/web" | ||
) | ||
|
||
func Run() { | ||
gin.SetMode(config.V.GetString("gin.model")) | ||
r := gin.Default() | ||
r.POST("/", handle) | ||
|
||
_ = r.Run(":" + strconv.Itoa(config.V.GetInt("gin.addr"))) | ||
} | ||
|
||
func handle(ctx *gin.Context) { | ||
body := ctx.DefaultPostForm("json", "null") | ||
status := 0 | ||
if body == "null" { | ||
status = 1 | ||
} | ||
|
||
var data model.Data | ||
err := json.Unmarshal([]byte(body), &data) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
go handleJson(data) | ||
ctx.JSON(200, gin.H{ | ||
"status": status, | ||
"message": "success", | ||
}) | ||
} | ||
|
||
func handleJson(data model.Data) { | ||
for _, item := range data.AwemeList { | ||
// 判断是否是广告 点赞数是否大于设定值 | ||
if item.IsAds == true || item.Statistics.DiggCount < config.V.GetInt("smallLike") { | ||
continue | ||
} | ||
log.Println("开始处理数据:", item.Desc) | ||
|
||
isDownload := config.V.GetInt("isDownload") | ||
var localAvatar, localCover, localVideo string | ||
var err error | ||
coverUrl, videoUrl := getCoverVideo(&item) | ||
if isDownload == 1 { | ||
// 下载封面图 视频 头像图 | ||
localAvatar, localCover, localVideo, err = downloadHttpFile(item.Author.AvatarThumb.UrlList[0], videoUrl, coverUrl) | ||
if err != nil { | ||
log.Println("下载封面图 视频 头像图失败:", err) | ||
continue | ||
} | ||
} else { | ||
localAvatar, localCover, localVideo = item.Author.AvatarThumb.UrlList[0], coverUrl, videoUrl | ||
} | ||
// 写入数据库 | ||
var video model.Video | ||
video.AwemeId = item.AwemeId | ||
video.AuthorId = item.Author.Uid | ||
video.Nickname = item.Author.Nickname | ||
video.Avatar = localAvatar | ||
video.Desc = item.Desc | ||
video.DiggCount = string(item.Statistics.DiggCount) | ||
video.CommentCount = string(item.Statistics.CommentCount) | ||
video.ShareUrl = item.ShareInfo.ShareUrl | ||
video.CoverPath = localCover | ||
video.VideoPath = localVideo | ||
video.IsDownload = isDownload | ||
database.Local.Create(&video) | ||
} | ||
} | ||
|
||
// downloadHttpFile 下载远程图片 | ||
func downloadHttpFile(avatarUrl, videoUrl string, coverUrl string) (string, string, string, error) { | ||
var localAvatar, localCover, localVideo string | ||
localAvatar = "download/avatar/" + utils.Md5(avatarUrl) + ".jpeg" | ||
localVideo = "download/video/" + utils.Md5(videoUrl) + ".mp4" | ||
localCover = "download/cover/" + utils.Md5(coverUrl) + ".jpeg" | ||
err := download(avatarUrl, localAvatar) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
err = download(videoUrl, localVideo) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
err = download(coverUrl, localCover) | ||
if err != nil { | ||
return "", "", "", err | ||
} | ||
return localAvatar, localCover, localVideo, nil | ||
} | ||
|
||
// getCoverVideo 获取封面图视频地址 | ||
func getCoverVideo(item *model.Item) (coverUrl, videoUrl string) { | ||
isDownload := config.V.GetInt("isDownload") | ||
coverUrl = item.Video.Cover.UrlList[0] | ||
if isDownload == 1 { | ||
videoUrl = item.Video.PlayAddr.UrlList[0] | ||
return | ||
} | ||
videoUrl = item.Video.PlayAddr.UrlList[len(item.Video.PlayAddr.UrlList)-1] | ||
return | ||
} | ||
|
||
// download 下载文件 | ||
func download(url, saveFile string) error { | ||
client := &http.Client{} | ||
req, err := http.NewRequest("GET", url, nil) | ||
req.Header.Add("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1") | ||
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3") | ||
req.Header.Add("Accept-Encoding", "gzip, deflate, br") | ||
req.Header.Add("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8") | ||
res, err := client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer res.Body.Close() | ||
f, err := os.Create(saveFile) | ||
if err != nil { | ||
_ = os.Remove(saveFile) | ||
return err | ||
} | ||
_, err = io.Copy(f, res.Body) | ||
if err != nil { | ||
_ = os.Remove(saveFile) | ||
return err | ||
} | ||
return nil | ||
func main() { | ||
web.Start() | ||
} |
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,58 @@ | ||
package adb | ||
|
||
import ( | ||
"fmt" | ||
"github.com/cnbattle/douyin/config" | ||
"os" | ||
"os/exec" | ||
"runtime" | ||
"time" | ||
) | ||
|
||
func Start() { | ||
START: | ||
start := time.Now().Unix() | ||
RunApp() | ||
for { | ||
now := time.Now().Unix() | ||
if now > start+config.V.GetInt64("app.restart") { | ||
time.Sleep(config.V.GetDuration("app.sleep") * time.Second) | ||
goto START | ||
} | ||
Swipe() | ||
time.Sleep(config.V.GetDuration("swipe.sleep") * time.Millisecond) | ||
} | ||
} | ||
|
||
func RunApp() { | ||
CloseApp() | ||
cmd := exec.Command(GetRunCli(), "shell", "am", "start", "-n", fmt.Sprintf("%v/%v", | ||
config.V.GetString("app.packageName"), config.V.GetString("app.startPath"), | ||
)) | ||
cmd.Stdout = os.Stdout | ||
_ = cmd.Run() | ||
} | ||
|
||
func Swipe() { | ||
cmd := exec.Command(GetRunCli(), "shell", "input", "swipe", | ||
config.V.GetString("swipe.startX"), | ||
config.V.GetString("swipe.startY"), | ||
config.V.GetString("swipe.endX"), | ||
config.V.GetString("swipe.endY"), | ||
) | ||
cmd.Stdout = os.Stdout | ||
_ = cmd.Run() | ||
} | ||
|
||
func CloseApp() { | ||
cmd := exec.Command(GetRunCli(), "shell", "am", "force-stop", config.V.GetString("app.packageName")) | ||
cmd.Stdout = os.Stdout | ||
_ = cmd.Run() | ||
} | ||
|
||
func GetRunCli() string { | ||
if runtime.GOOS == "windows" { | ||
return "./static/adb/adb.exe" | ||
} | ||
return "adb" | ||
} |
Oops, something went wrong.