Skip to content

Commit

Permalink
Big refactor, make everything organized
Browse files Browse the repository at this point in the history
Plugins were separated to manager
  • Loading branch information
NyaMisty committed Jun 27, 2020
1 parent afaf8d8 commit fe32f30
Show file tree
Hide file tree
Showing 29 changed files with 559 additions and 301 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ builds:
- linux
goarch:
- amd64
- arm64
- arm64`
goarm:
- 7
archives:
Expand Down
8 changes: 4 additions & 4 deletions config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"Module": [
{
"Name": "Youtube",
"Enable": true,
"Enable": false,
"EnableTemp": false,
"Users": [
{
Expand All @@ -22,7 +22,7 @@
},
{
"Name": "Twitcasting",
"Enable": true,
"Enable": false,
"Users": [
{
"TargetId": "natsuiromatsuri",
Expand All @@ -36,8 +36,8 @@
"Enable": true,
"Users": [
{
"TargetId": "336731767",
"Name": "natsuiromatsuri",
"TargetId": "4634167",
"Name": "tamaki",
"NeedDownload": true
}
]
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ require (
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/fzxiao233/Go-Emoji-Utils v0.0.0-20200305114615-005e99b02c2f
github.com/go-redis/redis v6.15.7+incompatible
github.com/gofrs/flock v0.7.1 // indirect
github.com/mitchellh/mapstructure v1.1.2
github.com/onsi/ginkgo v1.12.0 // indirect
github.com/onsi/gomega v1.9.0 // indirect
github.com/spf13/viper v1.6.2
Expand Down
22 changes: 22 additions & 0 deletions live/interfaces/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package interfaces

import (
"github.com/fzxiao233/Vtb_Record/utils"
)

type VideoInfo struct {
Title string
Date string
Target string
Provider string
FileName string
FilePath string
StreamingLink string
UsersConfig utils.UsersConfig
TransRecordPath string
}

type LiveStatus struct {
IsLive bool
Video *VideoInfo
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package structUtils
package interfaces

import (
"github.com/fzxiao233/Vtb_Record/utils"
Expand Down Expand Up @@ -42,7 +42,6 @@ func TestVideoInfo_CreateLiveMsg(t *testing.T) {
CQBotMsg: tt.fields.CQBotMsg,
TransRecordPath: tt.fields.TransRecordPath,
}
v.CreateLiveMsg()
})
}
}
Expand Down Expand Up @@ -84,7 +83,6 @@ func TestVideoInfo_CreateNoticeMsg(t *testing.T) {
CQBotMsg: tt.fields.CQBotMsg,
TransRecordPath: tt.fields.TransRecordPath,
}
v.CreateNoticeMsg()
})
}
}
19 changes: 14 additions & 5 deletions plugins/monitor/bilibili.go → live/monitor/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@ package monitor

import (
"github.com/bitly/go-simplejson"
"github.com/fzxiao233/Vtb_Record/plugins/structUtils"
"github.com/fzxiao233/Vtb_Record/live/interfaces"
. "github.com/fzxiao233/Vtb_Record/utils"
"log"
)

type Bilibili struct {
BaseMonitor
TargetId string
Title string
isLive bool
streamingLink string
}

func (b *Bilibili) getVideoInfo() error {
rawInfoJSON, err := HttpGet("https://api.live.bilibili.com/room/v1/Room/getRoomInfoOld?mid="+b.TargetId, map[string]string{})
_url, ok := b.ctx.ExtraModConfig["ApiHostUrl"]
var url string
if ok {
url = _url.(string)
} else {
url = "https://api.live.bilibili.com"
}
rawInfoJSON, err := b.ctx.HttpGet(url+"/room/v1/Room/getRoomInfoOld?mid="+b.TargetId, map[string]string{})
if err != nil {
return err
}
Expand All @@ -26,16 +35,15 @@ func (b *Bilibili) getVideoInfo() error {
//log.Printf("%+v", b)
}

func (b *Bilibili) CreateVideo(usersConfig UsersConfig) *structUtils.VideoInfo {
v := &structUtils.VideoInfo{
func (b *Bilibili) CreateVideo(usersConfig UsersConfig) *interfaces.VideoInfo {
v := &interfaces.VideoInfo{
Title: b.Title,
Date: GetTimeNow(),
Target: b.streamingLink,
Provider: "Bilibili",
StreamingLink: b.streamingLink,
UsersConfig: usersConfig,
}
v.CreateNoticeMsg()
return v
}

Expand All @@ -44,6 +52,7 @@ func (b *Bilibili) CheckLive(usersConfig UsersConfig) bool {
err := b.getVideoInfo()
if err != nil {
b.isLive = false
log.Print(err)
}
if !b.isLive {
NoLiving("Bilibili", usersConfig.Name)
Expand Down
6 changes: 3 additions & 3 deletions plugins/monitor/mock.go → live/monitor/mock.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package monitor

import (
"github.com/fzxiao233/Vtb_Record/plugins/structUtils"
"github.com/fzxiao233/Vtb_Record/live/interfaces"
"github.com/fzxiao233/Vtb_Record/utils"
)

type Mock struct {
Video *structUtils.VideoInfo
Video *interfaces.VideoInfo
IsLive bool
}

func (m *Mock) CheckLive(usersConfig utils.UsersConfig) bool {
return m.IsLive
}
func (m *Mock) CreateVideo(usersConfig utils.UsersConfig) *structUtils.VideoInfo {
func (m *Mock) CreateVideo(usersConfig utils.UsersConfig) *interfaces.VideoInfo {
return m.Video
}
95 changes: 95 additions & 0 deletions live/monitor/monitorUtils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package monitor

import (
. "github.com/fzxiao233/Vtb_Record/live/interfaces"
"github.com/fzxiao233/Vtb_Record/utils"
"log"
"net/http"
"net/url"
"time"
)

type MonitorCtx struct {
Client *http.Client
ExtraModConfig map[string]interface{}
}

func (c *MonitorCtx) HttpGet(url string, header map[string]string) ([]byte, error) {
return utils.HttpGet(c.Client, url, header)
}

func (c *MonitorCtx) GetProxy() (string, bool) {
enableProxy, ok1 := c.ExtraModConfig["EnableProxy"]
proxy, ok2 := c.ExtraModConfig["Proxy"]
if ok1 && ok2 && enableProxy == true {
return proxy.(string), true
} else {
return "", false
}
}

func createMonitorCtx(module utils.ModuleConfig) MonitorCtx {
ctx := MonitorCtx{ExtraModConfig: module.ExtraConfig}
var client *http.Client
proxy, ok := ctx.GetProxy()
if ok && proxy != "" {
proxyUrl, _ := url.Parse("socks5://" + proxy)
transport := &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
}

//adding the Transport object to the http Client
client = &http.Client{
Transport: transport,
Timeout: 60 * time.Second,
}
} else {
client = http.DefaultClient
}
ctx.Client = client
return ctx
}

type BaseMonitor struct {
ctx MonitorCtx
}

func (b *BaseMonitor) CreateVideo(usersConfig utils.UsersConfig) *VideoInfo {
return nil
}
func (b *BaseMonitor) CheckLive(usersConfig utils.UsersConfig) bool {
return false
}

func (b *BaseMonitor) GetCtx() *MonitorCtx {
return &b.ctx
}

type VideoMonitor interface {
CheckLive(usersConfig utils.UsersConfig) bool
CreateVideo(usersConfig utils.UsersConfig) *VideoInfo
GetCtx() *MonitorCtx
}

type LiveTrace func(monitor VideoMonitor, usersConfig utils.UsersConfig) *LiveStatus

func CreateVideoMonitor(module utils.ModuleConfig) VideoMonitor {
var monitor VideoMonitor
//var monitor *BaseMonitor
ctx := createMonitorCtx(module)
switch module.Name {
case "Youtube":
monitor = &Youtube{BaseMonitor: BaseMonitor{ctx}}
case "Twitcasting":
monitor = &Twitcasting{BaseMonitor: BaseMonitor{ctx}}
case "Bilibili":
monitor = &Bilibili{BaseMonitor: BaseMonitor{ctx}}
default:
return nil
}
return monitor
}

func NoLiving(Provide string, Name string) {
log.Printf("%s|%s|is not living\n", Provide, Name)
}
File renamed without changes.
10 changes: 5 additions & 5 deletions plugins/monitor/twitcasting.go → live/monitor/twitcasting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package monitor

import (
"github.com/bitly/go-simplejson"
"github.com/fzxiao233/Vtb_Record/plugins/structUtils"
"github.com/fzxiao233/Vtb_Record/live/interfaces"
. "github.com/fzxiao233/Vtb_Record/utils"
"strconv"
)

type Twitcasting struct {
BaseMonitor
TargetId string
twitcastingVideoInfo
}
Expand All @@ -18,7 +19,7 @@ type twitcastingVideoInfo struct {
}

func (t *Twitcasting) getVideoInfo() error {
rawInfoJSON, err := HttpGet("https://twitcasting.tv/streamserver.php?target="+t.TargetId+"&mode=client", map[string]string{})
rawInfoJSON, err := t.ctx.HttpGet("https://twitcasting.tv/streamserver.php?target="+t.TargetId+"&mode=client", map[string]string{})
if err != nil {
return err
}
Expand All @@ -29,17 +30,16 @@ func (t *Twitcasting) getVideoInfo() error {
return nil
//log.Printf("%+v", t)
}
func (t *Twitcasting) CreateVideo(usersConfig UsersConfig) *structUtils.VideoInfo {
func (t *Twitcasting) CreateVideo(usersConfig UsersConfig) *interfaces.VideoInfo {
videoTitle := t.TargetId + "#" + t.Vid
v := &structUtils.VideoInfo{
v := &interfaces.VideoInfo{
Title: videoTitle,
Date: GetTimeNow(),
Target: t.StreamingLink,
Provider: "Twitcasting",
StreamingLink: t.StreamingLink,
UsersConfig: usersConfig,
}
v.CreateLiveMsg()
return v
}
func (t *Twitcasting) CheckLive(usersConfig UsersConfig) bool {
Expand Down
3 changes: 2 additions & 1 deletion plugins/monitor/twitch.go → live/monitor/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package monitor
import "github.com/fzxiao233/Vtb_Record/utils"

type Twitch struct {
ctx MonitorCtx
APIUrl string
}

func (t Twitch) getLiveStatus() error {
_, err := utils.HttpGet(t.APIUrl, map[string]string{})
_, err := t.ctx.HttpGet(t.APIUrl, map[string]string{})
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions plugins/monitor/youtube.go → live/monitor/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package monitor
import (
"fmt"
"github.com/bitly/go-simplejson"
"github.com/fzxiao233/Vtb_Record/plugins/structUtils"
"github.com/fzxiao233/Vtb_Record/live/interfaces"
. "github.com/fzxiao233/Vtb_Record/utils"
"regexp"
)
Expand All @@ -14,12 +14,13 @@ type yfConfig struct {
Target string
}
type Youtube struct {
BaseMonitor
yfConfig
Url string
}

func (y *Youtube) getVideoInfo() error {
htmlBody, err := HttpGet(y.Url, map[string]string{})
htmlBody, err := y.ctx.HttpGet(y.Url, map[string]string{})
if err != nil {
return err
}
Expand All @@ -43,19 +44,18 @@ func (y *Youtube) getVideoInfo() error {
return nil
//log.Printf("%+v", y)
}
func (y *Youtube) CreateVideo(usersConfig UsersConfig) *structUtils.VideoInfo {
func (y *Youtube) CreateVideo(usersConfig UsersConfig) *interfaces.VideoInfo {
if !y.yfConfig.IsLive {
return &structUtils.VideoInfo{}
return &interfaces.VideoInfo{}
}
v := &structUtils.VideoInfo{
v := &interfaces.VideoInfo{
Title: y.Title,
Date: GetTimeNow(),
Target: y.Target,
Provider: "Youtube",
StreamingLink: "",
UsersConfig: usersConfig,
}
v.CreateLiveMsg()
return v
}
func (y *Youtube) CheckLive(usersConfig UsersConfig) bool {
Expand Down

0 comments on commit fe32f30

Please sign in to comment.