|
@@ -0,0 +1,235 @@
|
|
1
|
+package main
|
|
2
|
+
|
|
3
|
+import (
|
|
4
|
+ "container/heap"
|
|
5
|
+ "encoding/json"
|
|
6
|
+ "errors"
|
|
7
|
+ "fmt"
|
|
8
|
+ "github.com/antonholmquist/jason"
|
|
9
|
+ "github.com/jasonlvhit/gocron"
|
|
10
|
+ "io"
|
|
11
|
+ "io/ioutil"
|
|
12
|
+ "log"
|
|
13
|
+ "os"
|
|
14
|
+ "regexp"
|
|
15
|
+ "strconv"
|
|
16
|
+ "strings"
|
|
17
|
+ "time"
|
|
18
|
+)
|
|
19
|
+
|
|
20
|
+var songs SongPriorityQueue
|
|
21
|
+
|
|
22
|
+var credentials struct {
|
|
23
|
+ APIURL string
|
|
24
|
+ UserName string
|
|
25
|
+ Password string
|
|
26
|
+}
|
|
27
|
+
|
|
28
|
+func appendSong(wikiText string, author string, newSong string) string {
|
|
29
|
+ const AUTHOR_MARK = "<!-- Lisääjä -->"
|
|
30
|
+ const SONG_MARK = "<!-- Kappale -->"
|
|
31
|
+ lines := strings.Split(wikiText, "\n")
|
|
32
|
+ authorPrevIndex := -2
|
|
33
|
+
|
|
34
|
+ changedLines := make([]string, 0, len(lines))
|
|
35
|
+ for index, line := range lines {
|
|
36
|
+ if strings.Index(line, AUTHOR_MARK) != -1 && strings.Index(line, author) != -1 {
|
|
37
|
+ authorPrevIndex = index
|
|
38
|
+ }
|
|
39
|
+ if authorPrevIndex == (index-1) && strings.Index(line, SONG_MARK) != -1 {
|
|
40
|
+ changedLines = append(changedLines, "| "+SONG_MARK+" "+newSong)
|
|
41
|
+ } else {
|
|
42
|
+ changedLines = append(changedLines, line)
|
|
43
|
+ }
|
|
44
|
+ }
|
|
45
|
+ return strings.Join(changedLines, "\n")
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+func addSong(title string, week int, author string, song string) (bool, error) {
|
|
49
|
+ wiki := CreateWikiClient(credentials.APIURL, credentials.UserName, credentials.Password)
|
|
50
|
+
|
|
51
|
+ sections, err := wiki.GetWikiPageSections(title)
|
|
52
|
+ if err != nil {
|
|
53
|
+ return false, err
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ numberReg, _ := regexp.Compile("\\d+")
|
|
57
|
+ for _, section := range sections {
|
|
58
|
+ weekStr := numberReg.FindString(section.title)
|
|
59
|
+ if weekStr != "" {
|
|
60
|
+ weekNumber, _ := strconv.Atoi(weekStr)
|
|
61
|
+ if weekNumber == week {
|
|
62
|
+ wikiText, err := wiki.GetWikiPageSectionText(title, section.index)
|
|
63
|
+ if err != nil {
|
|
64
|
+ return false, err
|
|
65
|
+ }
|
|
66
|
+ changedWikiText := appendSong(wikiText, author, song)
|
|
67
|
+
|
|
68
|
+ return wiki.EditWikiPageSection(title, section.index, changedWikiText,
|
|
69
|
+ fmt.Sprintf("Added week %d song for %s", week, author))
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+ }
|
|
73
|
+ return false, errors.New("Could not find matching section")
|
|
74
|
+}
|
|
75
|
+
|
|
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
|
+func songSynced(syncedWeek int) error {
|
|
87
|
+ var v SongsFile
|
|
88
|
+ f, err := os.Open("songs.json")
|
|
89
|
+ if err != nil {
|
|
90
|
+ return err
|
|
91
|
+ }
|
|
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
|
+
|
|
109
|
+ synced := false
|
|
110
|
+ for _, song := range v.Songs {
|
|
111
|
+ if song.Week == syncedWeek {
|
|
112
|
+ song.Sync = true
|
|
113
|
+ synced = true
|
|
114
|
+ jsonValue, err := json.Marshal(v)
|
|
115
|
+ if err != nil {
|
|
116
|
+ log.Fatal(err)
|
|
117
|
+ return err
|
|
118
|
+ }
|
|
119
|
+ err = ioutil.WriteFile("songs.json", jsonValue, 0644)
|
|
120
|
+ return err
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+ if !synced {
|
|
124
|
+ return errors.New("No week matched from JSON for synced song")
|
|
125
|
+ }
|
|
126
|
+ return errors.New("Week not found")
|
|
127
|
+}
|
|
128
|
+
|
|
129
|
+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)
|
|
136
|
+ if err != nil {
|
|
137
|
+ return nil, err
|
|
138
|
+ }
|
|
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
|
+
|
|
167
|
+ target := yearStart.AddDate(0, 0, (week-1)*7)
|
|
168
|
+ songs[index] = &Song{
|
|
169
|
+ time: target,
|
|
170
|
+ song: "[" + url + " " + artist + " - " + title + "]",
|
|
171
|
+ week: week,
|
|
172
|
+ sync: sync,
|
|
173
|
+ index: index,
|
|
174
|
+ }
|
|
175
|
+ }
|
|
176
|
+ heap.Init(&songs)
|
|
177
|
+ for len(songs) > 0 && songs[0].sync {
|
|
178
|
+ heap.Pop(&songs)
|
|
179
|
+ }
|
|
180
|
+ return songs, nil
|
|
181
|
+}
|
|
182
|
+
|
|
183
|
+func task() {
|
|
184
|
+ now := time.Now()
|
|
185
|
+ if len(songs) > 0 && songs[0].time.Before(now) {
|
|
186
|
+ fmt.Println("Time has passed for " + songs[0].song)
|
|
187
|
+ success, err := addSong("Levyraati 2018", songs[0].week, "Lamperi", songs[0].song)
|
|
188
|
+ if err != nil {
|
|
189
|
+ log.Fatal(err)
|
|
190
|
+ }
|
|
191
|
+ if success {
|
|
192
|
+ err := songSynced(songs[0].week)
|
|
193
|
+ if err == nil {
|
|
194
|
+ heap.Pop(&songs)
|
|
195
|
+ } else {
|
|
196
|
+ fmt.Println("Error received:", err)
|
|
197
|
+ }
|
|
198
|
+ }
|
|
199
|
+ }
|
|
200
|
+}
|
|
201
|
+
|
|
202
|
+func initCreds() error {
|
|
203
|
+ f, err := os.Open("credentials.json")
|
|
204
|
+ if err != nil {
|
|
205
|
+ return err
|
|
206
|
+ }
|
|
207
|
+ defer f.Close()
|
|
208
|
+
|
|
209
|
+ if err != nil {
|
|
210
|
+ log.Fatal(err)
|
|
211
|
+ return err
|
|
212
|
+ }
|
|
213
|
+ dec := json.NewDecoder(f)
|
|
214
|
+ for {
|
|
215
|
+ if err := dec.Decode(&credentials); err == io.EOF {
|
|
216
|
+ break
|
|
217
|
+ } else if err != nil {
|
|
218
|
+ log.Fatal(err)
|
|
219
|
+ }
|
|
220
|
+ }
|
|
221
|
+ return nil
|
|
222
|
+}
|
|
223
|
+
|
|
224
|
+func main() {
|
|
225
|
+ err := initCreds()
|
|
226
|
+ if err != nil {
|
|
227
|
+ panic(err)
|
|
228
|
+ }
|
|
229
|
+ songs, err = getSongs()
|
|
230
|
+ if err != nil {
|
|
231
|
+ panic(err)
|
|
232
|
+ }
|
|
233
|
+ gocron.Every(1).Second().Do(task)
|
|
234
|
+ <-gocron.Start()
|
|
235
|
+}
|