-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
86 lines (71 loc) · 1.82 KB
/
notify.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
package main
import (
"fmt"
"log"
"time"
"github.com/philippgille/gokv"
)
var AnnounceNewEventsLock bool
func AnnounceNewEvents(store gokv.Store) {
if AnnounceNewEventsLock {
return
}
AnnounceNewEventsLock = true
for _, cv := range channels {
for ek, ev := range cv.Events {
kvItem := new(string)
found, err := store.Get(RemoveBaseUrlFromYouTubeLink(ev.Url), kvItem)
// If there is desync or any error, remove this event from RAM
if !found || err != nil {
delete(cv.Events, ek)
continue
}
// If this is already announced, skip this item
if *kvItem == "true" {
continue
}
// Announce this
var msg string
if ev.StartsAt == time.Unix(0, 0) {
msg = fmt.Sprintf("🆕 配信予定\n%v: %v\n🔗 %v\n⏰ 開始時刻未定", cv.Name, ev.Title, ev.Url)
} else {
msg = fmt.Sprintf("🆕 配信予定\n%v: %v\n🔗 %v\n⏰ %v 開始", cv.Name, ev.Title, ev.Url, FormatDateTime(ev.StartsAt))
}
err = PostToMastodon(msg)
if err != nil {
log.Println(err)
continue
}
// Mark this as already announced
err = store.Set(RemoveBaseUrlFromYouTubeLink(ev.Url), "true")
if err != nil {
log.Println(err)
}
}
}
AnnounceNewEventsLock = false
}
func AnnounceStarts(store gokv.Store) {
for _, cv := range channels {
for ek, ev := range cv.Events {
// Check approx. start time in order to reduce request frequency
if !(time.Now().After(ev.StartsAt)) {
continue
}
// Check date & time this starts
d, err := GetVideoDetails(ev.Url)
if err != nil {
continue
}
if !IsStarted(d) {
continue
}
// Announce this
msg := fmt.Sprintf("⏺️ 配信開始\n%v: %v\n🔗 %v", cv.Name, ev.Title, ev.Url)
PostToMastodon(msg)
// Delete this event from queue (on RAM & KV)
delete(cv.Events, ek)
store.Delete(ev.Url)
}
}
}