|
@@ -5,10 +5,8 @@ import (
|
5
|
5
|
"encoding/json"
|
6
|
6
|
"errors"
|
7
|
7
|
"fmt"
|
8
|
|
- "github.com/antonholmquist/jason"
|
9
|
8
|
"github.com/jasonlvhit/gocron"
|
10
|
9
|
"io"
|
11
|
|
- "io/ioutil"
|
12
|
10
|
"log"
|
13
|
11
|
"os"
|
14
|
12
|
"regexp"
|
|
@@ -73,51 +71,21 @@ func addSong(title string, week int, author string, song string) (bool, error) {
|
73
|
71
|
return false, errors.New("Could not find matching section")
|
74
|
72
|
}
|
75
|
73
|
|
76
|
|
-type SongsFile struct {
|
77
|
|
- Songs []*struct {
|
78
|
|
- Week int
|
79
|
|
- Title string
|
80
|
|
- Artist string
|
81
|
|
- URL string
|
82
|
|
- Sync bool
|
83
|
|
- }
|
84
|
|
-}
|
85
|
|
-
|
86
|
74
|
func songSynced(syncedWeek int) error {
|
87
|
|
- var v SongsFile
|
88
|
|
- f, err := os.Open("songs.json")
|
|
75
|
+ v, err := loadDb()
|
89
|
76
|
if err != nil {
|
90
|
77
|
return err
|
91
|
78
|
}
|
92
|
|
- defer f.Close()
|
93
|
|
-
|
94
|
|
- if err != nil {
|
95
|
|
- log.Fatal(err)
|
96
|
|
- return nil
|
97
|
|
- }
|
98
|
|
- dec := json.NewDecoder(f)
|
99
|
|
- dec.UseNumber()
|
100
|
|
- for {
|
101
|
|
- if err := dec.Decode(&v); err == io.EOF {
|
102
|
|
- break
|
103
|
|
- } else if err != nil {
|
104
|
|
- log.Fatal(err)
|
105
|
|
- return err
|
106
|
|
- }
|
107
|
|
- }
|
108
|
79
|
|
109
|
80
|
synced := false
|
110
|
81
|
for _, song := range v.Songs {
|
111
|
82
|
if song.Week == syncedWeek {
|
112
|
83
|
song.Sync = true
|
113
|
84
|
synced = true
|
114
|
|
- jsonValue, err := json.Marshal(v)
|
|
85
|
+ err := saveDb(v)
|
115
|
86
|
if err != nil {
|
116
|
|
- log.Fatal(err)
|
117
|
87
|
return err
|
118
|
88
|
}
|
119
|
|
- err = ioutil.WriteFile("songs.json", jsonValue, 0644)
|
120
|
|
- return err
|
121
|
89
|
}
|
122
|
90
|
}
|
123
|
91
|
if !synced {
|
|
@@ -127,49 +95,18 @@ func songSynced(syncedWeek int) error {
|
127
|
95
|
}
|
128
|
96
|
|
129
|
97
|
func getSongs() (SongPriorityQueue, error) {
|
130
|
|
- f, err := os.Open("songs.json")
|
131
|
|
- if err != nil {
|
132
|
|
- return nil, err
|
133
|
|
- }
|
134
|
|
- defer f.Close()
|
135
|
|
- v, err := jason.NewObjectFromReader(f)
|
|
98
|
+ dbSongs, err := loadDb()
|
136
|
99
|
if err != nil {
|
137
|
100
|
return nil, err
|
138
|
101
|
}
|
139
|
|
- songsArr, err := v.GetObjectArray("Songs")
|
140
|
|
- if err != nil {
|
141
|
|
- return nil, err
|
142
|
|
- }
|
143
|
|
-
|
144
|
|
- yearStart, _ := time.Parse(time.RFC3339, "2018-01-01T00:00:00+02:00")
|
145
|
|
-
|
146
|
|
- songs := make(SongPriorityQueue, len(songsArr))
|
147
|
|
- for index, songObj := range songsArr {
|
148
|
|
- title, err := songObj.GetString("Title")
|
149
|
|
- if err != nil {
|
150
|
|
- return nil, err
|
151
|
|
- }
|
152
|
|
- artist, err := songObj.GetString("Artist")
|
153
|
|
- if err != nil {
|
154
|
|
- return nil, err
|
155
|
|
- }
|
156
|
|
- url, err := songObj.GetString("URL")
|
157
|
|
- if err != nil {
|
158
|
|
- return nil, err
|
159
|
|
- }
|
160
|
|
- week64, err := songObj.GetInt64("Week")
|
161
|
|
- if err != nil {
|
162
|
|
- return nil, err
|
163
|
|
- }
|
164
|
|
- week := int(week64)
|
165
|
|
- sync, _ := songObj.GetBoolean("Sync")
|
166
|
102
|
|
167
|
|
- target := yearStart.AddDate(0, 0, (week-1)*7)
|
|
103
|
+ songs := make(SongPriorityQueue, len(dbSongs.Songs))
|
|
104
|
+ for index, songObj := range dbSongs.Songs {
|
168
|
105
|
songs[index] = &Song{
|
169
|
|
- time: target,
|
170
|
|
- song: "[" + url + " " + artist + " - " + title + "]",
|
171
|
|
- week: week,
|
172
|
|
- sync: sync,
|
|
106
|
+ time: targetTime(songObj),
|
|
107
|
+ song: songEntryWikiText(songObj),
|
|
108
|
+ week: songObj.Week,
|
|
109
|
+ sync: songObj.Sync,
|
173
|
110
|
index: index,
|
174
|
111
|
}
|
175
|
112
|
}
|
|
@@ -221,6 +158,20 @@ func initCreds() error {
|
221
|
158
|
return nil
|
222
|
159
|
}
|
223
|
160
|
|
|
161
|
+func targetTime(entry *SongEntry) time.Time {
|
|
162
|
+ yearStart, _ := time.Parse(time.RFC3339, "2018-01-01T00:00:00+02:00")
|
|
163
|
+ target := yearStart.AddDate(0, 0, (entry.Week-1)*7)
|
|
164
|
+ return target
|
|
165
|
+}
|
|
166
|
+
|
|
167
|
+func songEntryWikiText(entry *SongEntry) string {
|
|
168
|
+ return songWikiText(entry.URL, entry.Artist, entry.Title)
|
|
169
|
+}
|
|
170
|
+
|
|
171
|
+func songWikiText(url string, artist string, title string) string {
|
|
172
|
+ return "[" + url + " " + artist + " - " + title + "]"
|
|
173
|
+}
|
|
174
|
+
|
224
|
175
|
func main() {
|
225
|
176
|
err := initCreds()
|
226
|
177
|
if err != nil {
|
|
@@ -230,6 +181,42 @@ func main() {
|
230
|
181
|
if err != nil {
|
231
|
182
|
panic(err)
|
232
|
183
|
}
|
|
184
|
+
|
|
185
|
+ modifiedSongChan := make(chan *SongEntry)
|
|
186
|
+
|
|
187
|
+ go func() {
|
|
188
|
+ webStart(modifiedSongChan)
|
|
189
|
+ }()
|
|
190
|
+
|
|
191
|
+ go func() {
|
|
192
|
+ for {
|
|
193
|
+ newSong := <-modifiedSongChan
|
|
194
|
+ matched := false
|
|
195
|
+ for _, song := range songs {
|
|
196
|
+ if song.week == newSong.Week {
|
|
197
|
+ song.song = songEntryWikiText(newSong)
|
|
198
|
+ song.sync = newSong.Sync
|
|
199
|
+ matched = true
|
|
200
|
+ log.Printf("Updated song for week %d, artist: %s, title: %s, URL: %s, time: %v",
|
|
201
|
+ newSong.Week, newSong.Artist, newSong.Title, newSong.URL, song.time)
|
|
202
|
+ }
|
|
203
|
+ }
|
|
204
|
+ if !matched {
|
|
205
|
+ song := &Song{
|
|
206
|
+ time: targetTime(newSong),
|
|
207
|
+ song: songEntryWikiText(newSong),
|
|
208
|
+ week: newSong.Week,
|
|
209
|
+ sync: newSong.Sync,
|
|
210
|
+ index: len(songs),
|
|
211
|
+ }
|
|
212
|
+ heap.Push(&songs, song)
|
|
213
|
+ log.Printf("Added song for week %d, artist: %s, title: %s, URL: %s, time: %v",
|
|
214
|
+ newSong.Week, newSong.Artist, newSong.Title, newSong.URL, song.time)
|
|
215
|
+ }
|
|
216
|
+ }
|
|
217
|
+ }()
|
|
218
|
+
|
233
|
219
|
gocron.Every(1).Second().Do(task)
|
234
|
220
|
<-gocron.Start()
|
|
221
|
+
|
235
|
222
|
}
|