Skip to content

Commit

Permalink
adb和web服务重构,可单独运行,自定义重写某一服务
Browse files Browse the repository at this point in the history
  • Loading branch information
cnbattle committed Sep 1, 2019
1 parent 87aaeb0 commit 91bcf5e
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 204 deletions.
18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@

## 使用

1. 安装anyproxy,详细请自己google
1 安装anyproxy,详细请自己google

1. 使用android虚拟机或真机,安装抖音,配置anyproxy https代理
2 使用android虚拟机或真机,安装抖音,配置anyproxy https代理

1. 修改anyproxy配置文件,详见 angproxy目录下文件,具体看`beforeSendRequest` `beforeSendResponse` 函数代码
3 修改anyproxy配置文件,详见 angproxy目录下文件,具体看`beforeSendRequest` `beforeSendResponse` 函数代码

1. 启动anyproxy(用pm2管理启动最佳)
4 启动anyproxy(用pm2管理启动最佳)

1. 复制 `config.example.toml``config.toml`,并根据自己需求修改参数
5 复制 `config.example.toml``config.toml`,并根据自己需求修改参数

1. 运行 本项目程序 `go run main.go` 或 编辑运行也可
6 运行 本项目程序
- 一起运行 `adb``web` 服务 :`go run main.go` 或 编辑运行也可
- 单独运行:`go run apps/adb/adb.go``go run apps/web/web.go` 或 编辑运行也可

1. 若开启下载会生成一个 `database.db`的sqlite3数据库文件,字符详见`model/videos.go`文件,静态文件(用户头像,视频封面图,视频文件)将放在`download/[avatar,cover,video]`目录下
7 若开启下载会生成一个 `database.db`的sqlite3数据库文件,字符详见`model/videos.go`文件,静态文件(用户头像,视频封面图,视频文件)将放在`download/[avatar,cover,video]`目录下

1. 最后根据个人需要上传处理即可
8 最后根据个人需要上传处理即可

## 待优化的地方

Expand Down
60 changes: 4 additions & 56 deletions apps/adb/adb.go
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()
}
140 changes: 4 additions & 136 deletions apps/web/web.go
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()
}
58 changes: 58 additions & 0 deletions internal/adb/adb.go
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"
}
Loading

0 comments on commit 91bcf5e

Please sign in to comment.