-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (110 loc) · 2.7 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"syscall"
"github.com/robfig/cron/v3"
"gopkg.in/yaml.v2"
)
var configfile = ".config"
var helpurl = "https://github.com/blueforesticarus/goontunes"
type State struct {
Discord *DiscordApp
Spotify *SpotifyApp
Youtube *YoutubeApp
Matrix *MatrixApp
Playlists []Playlist
CachePath string
em *EntryManager
lib *Library
plumber *Plumber
Manual map[string][]string
}
//Used by discord.go to know how far back to look for messages
func (global *State) Latest(platform string, channel string) string {
global.em.Lock.RLock()
defer global.em.Lock.RUnlock()
latest := Entry{Valid: false, MessageId: ""}
for _, entry := range global.em.Entries {
if (platform == "" || platform == entry.Platform) &&
(channel == "" || channel == entry.ChannelId) {
if !latest.Valid || latest.Date.Before(entry.Date) {
latest = entry
}
}
}
return latest.MessageId
}
func process_manual() {
for k, v := range global.Manual {
for i, s := range v {
var entry = Entry{
MessageId: fmt.Sprintf("manual+%s+%d", k, i),
Platform: "manual",
ChannelId: k,
Url: s,
}
process_entry(&entry)
if entry.Valid {
global.plumber.d_entry.Plumb(entry)
}
}
}
}
var global State
func main() {
bytes, err := ioutil.ReadFile(configfile)
if err != nil {
fmt.Printf("Error, cannot open config file %v\n", configfile)
return
}
err = yaml.UnmarshalStrict(bytes, &global)
if err != nil {
fmt.Printf("Could not parse config %v\n", err)
fmt.Printf("Follow instructions at %s\n", helpurl)
return
}
err = os.MkdirAll(global.CachePath, 0755)
if err != nil {
fmt.Printf("Could not create cachepath %s, %v\n", global.CachePath, err)
return
}
/* Doesnt work
runlock := global.CachePath + "/runlock"
_, err = net.Listen("unix", runlock)
if err != nil {
log.Fatalf("Program already running. (If it isn't, delete %s)\n", runlock)
}
*/
global.em = new_EntryManager()
global.em.cachepath = global.CachePath + "/entries"
global.em.load()
global.lib = new_Library()
global.lib.cachepath = global.CachePath + "/library"
global.lib.load()
global.plumber = new_Plumber()
for _, app := range []App{
global.Spotify,
global.Youtube,
global.Discord,
global.Matrix,
} {
//XXX is this allowed? how do interfaces work?
if ! app.IsNil() {
app.Connect()
}
}
go process_manual()
global.plumber.rescan() // no reason not to do this concurrent with Discord init, I think
c := cron.New()
c.AddFunc("@every 1h", func() {
fmt.Printf("1hr timer\n")
global.plumber.rescan()
})
c.Start()
exitSignal := make(chan os.Signal)
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
<-exitSignal
}