-
Notifications
You must be signed in to change notification settings - Fork 940
/
Copy pathyoutube.go
193 lines (159 loc) · 5.29 KB
/
youtube.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package youtube
import (
"context"
"fmt"
"net/http"
"net/url"
"sync"
"time"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/config"
"github.com/jonas747/yagpdb/common/mqueue"
"github.com/jonas747/yagpdb/premium"
"google.golang.org/api/youtube/v3"
)
const (
RedisChannelsLockKey = "youtube_subbed_channel_lock"
RedisKeyWebSubChannels = "youtube_registered_websub_channels"
GoogleWebsubHub = "https://pubsubhubbub.appspot.com/subscribe"
)
var (
confWebsubVerifytoken = config.RegisterOption("yagpdb.youtube.verify_token", "Youtube websub push verify token, set it to a random string and never change it", "asdkpoasdkpaoksdpako")
logger = common.GetPluginLogger(&Plugin{})
)
func KeyLastVidTime(channel string) string { return "youtube_last_video_time:" + channel }
func KeyLastVidID(channel string) string { return "youtube_last_video_id:" + channel }
type Plugin struct {
YTService *youtube.Service
Stop chan *sync.WaitGroup
}
func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "Youtube",
SysName: "youtube",
Category: common.PluginCategoryFeeds,
}
}
func RegisterPlugin() {
p := &Plugin{}
err := p.SetupClient()
if err != nil {
logger.WithError(err).Error("Failed setting up youtube plugin, youtube plugin will not be enabled.")
return
}
common.GORM.AutoMigrate(ChannelSubscription{}, YoutubePlaylistID{})
common.RegisterPlugin(p)
mqueue.RegisterSource("youtube", p)
}
type ChannelSubscription struct {
common.SmallModel
GuildID string
ChannelID string
YoutubeChannelID string
YoutubeChannelName string
MentionEveryone bool
}
func (c *ChannelSubscription) TableName() string {
return "youtube_channel_subscriptions"
}
type YoutubePlaylistID struct {
ChannelID string `gorm:"primary_key"`
CreatedAt time.Time
PlaylistID string
}
var _ mqueue.PluginWithSourceDisabler = (*Plugin)(nil)
// Remove feeds if they don't point to a proper channel
func (p *Plugin) DisableFeed(elem *mqueue.QueuedElement, err error) {
// Remove it
err = common.GORM.Where("channel_id = ?", elem.Channel).Delete(ChannelSubscription{}).Error
if err != nil {
logger.WithError(err).Error("failed removing nonexistant channel")
} else {
logger.WithField("channel", elem.Channel).Info("Removed youtube feed to nonexistant channel")
}
}
func (p *Plugin) WebSubSubscribe(ytChannelID string) error {
// hub.callback:https://testing.yagpdb.xyz/yt_new_upload
// hub.topic:https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCt-ERbX-2yA6cAqfdKOlUwQ
// hub.verify:sync
// hub.mode:subscribe
// hub.verify_token:hmmmmmmmmwhatsthis
// hub.secret:
// hub.lease_seconds:
values := url.Values{
"hub.callback": {"https://" + common.ConfHost.GetString() + "/yt_new_upload/" + confWebsubVerifytoken.GetString()},
"hub.topic": {"https://www.youtube.com/xml/feeds/videos.xml?channel_id=" + ytChannelID},
"hub.verify": {"sync"},
"hub.mode": {"subscribe"},
"hub.verify_token": {confWebsubVerifytoken.GetString()},
// "hub.lease_seconds": {"60"},
}
resp, err := http.PostForm(GoogleWebsubHub, values)
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("Go bad status code: %d (%s)", resp.StatusCode, resp.Status)
}
logger.Info("Websub: Subscribed to channel ", ytChannelID)
return nil
}
func (p *Plugin) WebSubUnsubscribe(ytChannelID string) error {
// hub.callback:https://testing.yagpdb.xyz/yt_new_upload
// hub.topic:https://www.youtube.com/xml/feeds/videos.xml?channel_id=UCt-ERbX-2yA6cAqfdKOlUwQ
// hub.verify:sync
// hub.mode:subscribe
// hub.verify_token:hmmmmmmmmwhatsthis
// hub.secret:
// hub.lease_seconds:
values := url.Values{
"hub.callback": {"https://" + common.ConfHost.GetString() + "/yt_new_upload/" + confWebsubVerifytoken.GetString()},
"hub.topic": {"https://www.youtube.com/xml/feeds/videos.xml?channel_id=" + ytChannelID},
"hub.verify": {"sync"},
"hub.mode": {"unsubscribe"},
"hub.verify_token": {confWebsubVerifytoken.GetString()},
}
resp, err := http.PostForm(GoogleWebsubHub, values)
if err != nil {
return err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("Go bad status code: %d (%s)", resp.StatusCode, resp.Status)
}
logger.Info("Websub: UnSubscribed to channel ", ytChannelID)
return nil
}
type XMLFeed struct {
Xmlns string `xml:"xmlns,attr"`
Link []Link `xml:"link"`
ChannelID string `xml:"entry>channelId"`
Published string `xml:"entry>published"`
VideoId string `xml:"entry>videoId"`
Yt string `xml:"yt,attr"`
LinkEntry Link `xml:"entry>link"`
AuthorUri string `xml:"entry>author>uri"`
AuthorName string `xml:"entry>author>name"`
UpdatedEntry string `xml:"entry>updated"`
Title string `xml:"title"`
TitleEntry string `xml:"entry>title"`
Id string `xml:"entry>id"`
Updated string `xml:"updated"`
}
type Link struct {
Href string `xml:"href,attr"`
Rel string `xml:"rel,attr"`
}
type LinkEntry struct {
Href string `xml:"href,attr"`
Rel string `xml:"rel,attr"`
}
const (
GuildMaxFeeds = 50
GuildMaxFeedsPremium = 250
)
func MaxFeedsForContext(ctx context.Context) int {
if premium.ContextPremium(ctx) {
return GuildMaxFeedsPremium
}
return GuildMaxFeeds
}