-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
model.go
159 lines (139 loc) · 3.62 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package youtube
import (
"encoding/json"
"github.com/Mrs4s/MiraiGo/message"
"github.com/Sora233/DDBOT/concern"
"github.com/Sora233/DDBOT/proxy_pool"
localutils "github.com/Sora233/DDBOT/utils"
)
type UserInfo struct {
ChannelId string `json:"channel_id"`
ChannelName string `json:"channel_name"`
}
type EventType int64
const (
Video EventType = iota
Live
)
type ConcernEvent interface {
Type() EventType
}
// VideoInfo may be a video or a live, depend on the VideoType
type VideoInfo struct {
UserInfo
Cover string `json:"cover"`
VideoId string `json:"video_id"`
VideoTitle string `json:"video_title"`
VideoType VideoType `json:"video_type"`
VideoStatus VideoStatus `json:"video_status"`
VideoTimestamp int64 `json:"video_timestamp"`
}
func (v *VideoInfo) Type() EventType {
if v.IsLive() {
return Live
} else {
return Video
}
}
func (v *VideoInfo) IsLive() bool {
if v == nil {
return false
}
return v.VideoType == VideoType_FirstLive || v.VideoType == VideoType_Live
}
func (v *VideoInfo) IsLiving() bool {
if v == nil {
return false
}
return v.IsLive() && v.VideoStatus == VideoStatus_Living
}
func (v *VideoInfo) IsWaiting() bool {
if v == nil {
return false
}
return v.IsLive() && v.VideoStatus == VideoStatus_Waiting
}
func (v *VideoInfo) IsVideo() bool {
if v == nil {
return false
}
return v.VideoType == VideoType_Video
}
type Info struct {
VideoInfo []*VideoInfo `json:"video_info"`
UserInfo
}
func (i *Info) ToString() string {
if i == nil {
return ""
}
b, _ := json.Marshal(i)
return string(b)
}
func NewInfo(vinfo []*VideoInfo) *Info {
info := new(Info)
info.VideoInfo = vinfo
if len(vinfo) > 0 {
info.ChannelId = vinfo[0].ChannelId
info.ChannelName = vinfo[0].ChannelName
}
return info
}
type ConcernNotify struct {
VideoInfo
GroupCode int64 `json:"group_code"`
}
func (notify *ConcernNotify) ShouldSend() bool {
return true
}
func (notify *ConcernNotify) GetGroupCode() int64 {
return notify.GroupCode
}
func (notify *ConcernNotify) GetUid() interface{} {
return notify.ChannelId
}
func (notify *ConcernNotify) ToMessage() []message.IMessageElement {
var result []message.IMessageElement
if notify.IsLive() {
if notify.IsLiving() {
result = append(result, localutils.MessageTextf("YTB-%v正在直播:\n%v\n", notify.ChannelName, notify.VideoTitle))
} else {
result = append(result, localutils.MessageTextf("YTB-%v发布了直播预约:\n%v\n时间:%v\n", notify.ChannelName, notify.VideoTitle, localutils.TimestampFormat(notify.VideoTimestamp)))
}
} else if notify.IsVideo() {
result = append(result, localutils.MessageTextf("YTB-%s发布了新视频:\n%v\n", notify.ChannelName, notify.VideoTitle))
}
groupImg, err := localutils.UploadGroupImageByUrl(notify.GroupCode, notify.Cover, false, proxy_pool.PreferOversea)
if err != nil {
logger.WithField("channel_name", notify.ChannelName).
WithField("video_id", notify.VideoId).
WithField("group_code", notify.GroupCode).
Errorf("upload cover failed %v", err)
} else {
result = append(result, groupImg)
}
result = append(result, message.NewText(VideoViewUrl(notify.VideoId)+"\n"))
return result
}
func (notify *ConcernNotify) Type() concern.Type {
if notify.IsLive() {
return concern.YoutubeLive
} else {
return concern.YoutubeVideo
}
}
func NewConcernNotify(groupCode int64, info *VideoInfo) *ConcernNotify {
if info == nil {
return nil
}
return &ConcernNotify{
VideoInfo: *info,
GroupCode: groupCode,
}
}
func NewUserInfo(channelId, channelName string) *UserInfo {
return &UserInfo{
ChannelId: channelId,
ChannelName: channelName,
}
}