Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
cnbattle committed Aug 28, 2019
0 parents commit 13e917a
Show file tree
Hide file tree
Showing 17 changed files with 581 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea/
vendor/
52 changes: 52 additions & 0 deletions apps/adb/adb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package adb

import (
"fmt"
"os"
"os/exec"
"github.com/cnbattle/douyin/config"
"time"
)

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("./static/adb.exe", "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("./static/adb.exe", "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("./static/adb.exe", "shell", "am", "force-stop", config.V.GetString("app.packageName"))
cmd.Stdout = os.Stdout
cmd.Run()
}
119 changes: 119 additions & 0 deletions apps/web/web.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package web

import (
"encoding/json"
"fmt"
"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"
)

func Run() {
r := gin.Default()
r.POST("/", handle)
_ = r.Run() // listen and serve on 0.0.0.0:8080
}

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 < 10000 {
continue
}
log.Println("开始处理数据:", item.Desc)

coverUrl, videoUrl := getCoverVideo(&item)
// 下载封面图 视频 头像图
localAvatar, localCover, localVideo, err := downloadHttpFile(item.Author.AvatarThumb.UrlList[0], videoUrl, coverUrl)
if err != nil {
log.Println("下载封面图 视频 头像图失败:", err)
continue
}
// 写入数据库
var video model.Video
video.AwemeId = item.AwemeId
video.Nickname = item.Author.Nickname
video.Avatar = localAvatar
video.Desc = item.Desc
video.CoverPath = localCover
video.VideoPath = localVideo
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) (string, string) {
coverUrl := item.Video.Cover.UrlList[0]
videoUrl := item.Video.PlayAddr.UrlList[0]
// 是否是长视频
//if len(item.LongVideo) > 0 {
// coverUrl = item.LongVideo[0].Video.Cover.UrlList[0]
// videoUrl = item.LongVideo[0].Video.PlayAddr.UrlList[0]
//}
return coverUrl, videoUrl
}

// download 下载文件
func download(url, saveFile string) error {
res, err := http.Get(url)
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
}
1 change: 1 addition & 0 deletions body.json

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
title = "TOML Example"
[app]
packageName = "com.ss.android.ugc.aweme" # app包名
startPath = ".main.MainActivity" # 包 首页path
restart = 60 # 重新打开时间 单位秒
sleep = 60 # 重新打开睡眠时间 单位秒

[swipe]
# 滑动 开始和结束 像素坐标值
startX = 500
startY = 1000
endX = 489
endY = 123
sleep = 200 # 滑动等待时间 单位毫秒
17 changes: 17 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package config

import (
"github.com/spf13/viper"
)

var V *viper.Viper

func init() {
V = viper.New()
V.SetConfigName("config")
V.AddConfigPath("./")
V.SetConfigType("toml")
if err := V.ReadInConfig(); err != nil {
panic(err)
}
}
Binary file added database.db
Binary file not shown.
26 changes: 26 additions & 0 deletions database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package database

import (
"github.com/cnbattle/douyin/model"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"log"
)

var (
Local *gorm.DB
localDialect = "sqlite3"
localArgs = "./database.db"
)

func init() {
var err error
Local, err = gorm.Open(localDialect, localArgs)
if err != nil {
log.Panic(err)
}
Local.LogMode(false)
Local.DB().SetMaxOpenConns(10)
Local.DB().SetMaxIdleConns(20)
Local.AutoMigrate(&model.Video{})
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/cnbattle/douyin

go 1.12

require (
github.com/gin-gonic/gin v1.4.0
github.com/jinzhu/gorm v1.9.10
github.com/spf13/viper v1.4.0
)
Loading

0 comments on commit 13e917a

Please sign in to comment.