/
youtube.go
183 lines (147 loc) · 4.44 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
package generator
import (
"bytes"
"errors"
"log"
"math/rand"
"regexp"
"strings"
"text/template"
"time"
"github.com/dghubble/go-twitter/twitter"
"github.com/docker/go-units"
"github.com/xarantolus/rfa-launch-bot/collector"
"github.com/xarantolus/rfa-launch-bot/generator/scraper"
"github.com/xarantolus/rfa-launch-bot/util"
)
const (
rfaLiveURL = "https://www.youtube.com/channel/UC6PsS67tBgDr5w22ZZSgI9w/live"
)
// LiveStreamTweets tweets when RFA does a live stream
func LiveStreamTweets(client *twitter.Client) {
var (
lastTweetedURL string
lastTweetedUpcoming bool
lastTweetedStart time.Time
lastTweetTime time.Time
)
log.Println("[YouTube] Scraping RFA channel for live streams")
for {
liveStream := waitForLiveStream(rfaLiveURL)
currentURL := liveStream.URL()
var tweetText = describeLiveStream(&liveStream)
t, du, ok := liveStream.TimeUntil()
// Check if we already tweeted about this live stream within the last few minutes
if lastTweetedURL == currentURL && lastTweetedUpcoming == liveStream.IsUpcoming &&
lastTweetedStart.Equal(t) && (!ok || time.Since(lastTweetTime) < waitTime(du)) {
log.Println("[YouTube] Skipping live stream announcement since we tweeted about it recently.")
goto sleep
}
// Tweet the stream announcement
{
tweet, _, err := client.Statuses.Update(tweetText, nil)
if util.LogError(err, "tweeting live stream update") {
goto sleep
}
lastTweetedURL = currentURL
lastTweetedUpcoming = liveStream.IsUpcoming
lastTweetedStart = t
lastTweetTime = time.Now()
log.Println("[YouTube] Tweeted live stream announcement:", collector.TweetWrapper{Tweet: *tweet}.URL())
}
sleep:
time.Sleep(time.Minute + time.Duration(rand.Intn(60))*time.Second)
}
}
// waitTime defines how long we need to wait before sending another (same) live stream announcement
func waitTime(durationUntil time.Duration) time.Duration {
switch {
case durationUntil < 30*time.Minute:
return 15 * time.Minute
case durationUntil < 4*time.Hour:
return time.Hour
default:
return 4 * time.Hour
}
}
func waitForLiveStream(liveURL string) (lv scraper.LiveVideo) {
for {
liveVideo, err := scraper.YouTubeLive(liveURL)
if err != nil {
if !errors.Is(err, scraper.ErrNoVideo) {
log.Println("[YouTube] Unexpected error while scraping YouTube live:", err.Error())
}
goto sleep
}
// Validate that the video URL will make sense
if liveVideo.VideoID != "" && liveVideo.Title != "" {
return liveVideo
}
sleep:
time.Sleep(time.Minute + time.Duration(rand.Intn(60))*time.Second)
}
}
const streamTweetTemplate = `{{if .IsUpcoming}}Rocket Factory Augsburg live stream starts {{if .HaveStartTime}}in {{.TimeUntil | duration}}{{else}}soon{{end}}:{{else}}Rocket Factory Augsburg is now live on YouTube:{{end}}
{{.Title}}
{{$keywords := (keywords .Title .ShortDescription)}}{{with $keywords}}
{{hashtags $keywords}}{{end}}
{{.URL}}`
var (
tmplFuncs = map[string]interface{}{
"hashtags": util.HashTagText,
"keywords": extractKeywords,
"duration": func(d time.Duration) string {
return strings.ToLower(units.HumanDuration(d))
},
}
streamTweetTmpl = template.Must(template.New("streamTweetTemplate").Funcs(tmplFuncs).Parse(streamTweetTemplate))
)
func describeLiveStream(v *scraper.LiveVideo) string {
var b bytes.Buffer
t, dur, haveStartTime := v.TimeUntil()
var data = struct {
HaveStartTime bool
TimeUntil time.Duration
StartTime time.Time
*scraper.LiveVideo
}{
HaveStartTime: haveStartTime,
TimeUntil: dur,
StartTime: t,
LiveVideo: v,
}
err := streamTweetTmpl.Execute(&b, data)
if err != nil {
panic("executing Tweet template: " + err.Error())
}
return strings.TrimSpace(b.String())
}
var expectedRegexes = []*regexp.Regexp{
regexp.MustCompile(`\bRFA\b`),
regexp.MustCompile(`\bRocket Factory Augsburg\b`),
regexp.MustCompile(`\bLauncher\b`),
regexp.MustCompile(`\bRFA Launcher\b`),
regexp.MustCompile(`\bNewSpace\b`),
regexp.MustCompile(`\bAugsburg\b`),
}
func extractKeywords(title string, description string) (keywords []string) {
extr := title + "\n " + description
for _, matcher := range expectedRegexes {
res := matcher.FindAllString(extr, 1)
if len(res) == 0 {
continue
}
if !containsIgnoreCase(keywords, res[0]) {
keywords = append(keywords, res[0])
}
}
return
}
func containsIgnoreCase(s []string, e string) bool {
for _, a := range s {
if strings.EqualFold(a, e) {
return true
}
}
return false
}