bot.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package main
  2. import (
  3. "container/heap"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "github.com/antonholmquist/jason"
  8. "github.com/jasonlvhit/gocron"
  9. "io"
  10. "io/ioutil"
  11. "log"
  12. "os"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. var songs SongPriorityQueue
  19. var credentials struct {
  20. APIURL string
  21. UserName string
  22. Password string
  23. }
  24. func appendSong(wikiText string, author string, newSong string) string {
  25. const AUTHOR_MARK = "<!-- Lisääjä -->"
  26. const SONG_MARK = "<!-- Kappale -->"
  27. lines := strings.Split(wikiText, "\n")
  28. authorPrevIndex := -2
  29. changedLines := make([]string, 0, len(lines))
  30. for index, line := range lines {
  31. if strings.Index(line, AUTHOR_MARK) != -1 && strings.Index(line, author) != -1 {
  32. authorPrevIndex = index
  33. }
  34. if authorPrevIndex == (index-1) && strings.Index(line, SONG_MARK) != -1 {
  35. changedLines = append(changedLines, "| "+SONG_MARK+" "+newSong)
  36. } else {
  37. changedLines = append(changedLines, line)
  38. }
  39. }
  40. return strings.Join(changedLines, "\n")
  41. }
  42. func addSong(title string, week int, author string, song string) (bool, error) {
  43. wiki := CreateWikiClient(credentials.APIURL, credentials.UserName, credentials.Password)
  44. sections, err := wiki.GetWikiPageSections(title)
  45. if err != nil {
  46. return false, err
  47. }
  48. numberReg, _ := regexp.Compile("\\d+")
  49. for _, section := range sections {
  50. weekStr := numberReg.FindString(section.title)
  51. if weekStr != "" {
  52. weekNumber, _ := strconv.Atoi(weekStr)
  53. if weekNumber == week {
  54. wikiText, err := wiki.GetWikiPageSectionText(title, section.index)
  55. if err != nil {
  56. return false, err
  57. }
  58. changedWikiText := appendSong(wikiText, author, song)
  59. return wiki.EditWikiPageSection(title, section.index, changedWikiText,
  60. fmt.Sprintf("Added week %d song for %s", week, author))
  61. }
  62. }
  63. }
  64. return false, errors.New("Could not find matching section")
  65. }
  66. type SongsFile struct {
  67. Songs []*struct {
  68. Week int
  69. Title string
  70. Artist string
  71. URL string
  72. Sync bool
  73. }
  74. }
  75. func songSynced(syncedWeek int) error {
  76. var v SongsFile
  77. f, err := os.Open("songs.json")
  78. if err != nil {
  79. return err
  80. }
  81. defer f.Close()
  82. if err != nil {
  83. log.Fatal(err)
  84. return nil
  85. }
  86. dec := json.NewDecoder(f)
  87. dec.UseNumber()
  88. for {
  89. if err := dec.Decode(&v); err == io.EOF {
  90. break
  91. } else if err != nil {
  92. log.Fatal(err)
  93. return err
  94. }
  95. }
  96. synced := false
  97. for _, song := range v.Songs {
  98. if song.Week == syncedWeek {
  99. song.Sync = true
  100. synced = true
  101. jsonValue, err := json.Marshal(v)
  102. if err != nil {
  103. log.Fatal(err)
  104. return err
  105. }
  106. err = ioutil.WriteFile("songs.json", jsonValue, 0644)
  107. return err
  108. }
  109. }
  110. if !synced {
  111. return errors.New("No week matched from JSON for synced song")
  112. }
  113. return errors.New("Week not found")
  114. }
  115. func getSongs() (SongPriorityQueue, error) {
  116. f, err := os.Open("songs.json")
  117. if err != nil {
  118. return nil, err
  119. }
  120. defer f.Close()
  121. v, err := jason.NewObjectFromReader(f)
  122. if err != nil {
  123. return nil, err
  124. }
  125. songsArr, err := v.GetObjectArray("Songs")
  126. if err != nil {
  127. return nil, err
  128. }
  129. yearStart, _ := time.Parse(time.RFC3339, "2018-01-01T00:00:00+02:00")
  130. songs := make(SongPriorityQueue, len(songsArr))
  131. for index, songObj := range songsArr {
  132. title, err := songObj.GetString("Title")
  133. if err != nil {
  134. return nil, err
  135. }
  136. artist, err := songObj.GetString("Artist")
  137. if err != nil {
  138. return nil, err
  139. }
  140. url, err := songObj.GetString("URL")
  141. if err != nil {
  142. return nil, err
  143. }
  144. week64, err := songObj.GetInt64("Week")
  145. if err != nil {
  146. return nil, err
  147. }
  148. week := int(week64)
  149. sync, _ := songObj.GetBoolean("Sync")
  150. target := yearStart.AddDate(0, 0, (week-1)*7)
  151. songs[index] = &Song{
  152. time: target,
  153. song: "[" + url + " " + artist + " - " + title + "]",
  154. week: week,
  155. sync: sync,
  156. index: index,
  157. }
  158. }
  159. heap.Init(&songs)
  160. for len(songs) > 0 && songs[0].sync {
  161. heap.Pop(&songs)
  162. }
  163. return songs, nil
  164. }
  165. func task() {
  166. now := time.Now()
  167. if len(songs) > 0 && songs[0].time.Before(now) {
  168. fmt.Println("Time has passed for " + songs[0].song)
  169. success, err := addSong("Levyraati 2018", songs[0].week, "Lamperi", songs[0].song)
  170. if err != nil {
  171. log.Fatal(err)
  172. }
  173. if success {
  174. err := songSynced(songs[0].week)
  175. if err == nil {
  176. heap.Pop(&songs)
  177. } else {
  178. fmt.Println("Error received:", err)
  179. }
  180. }
  181. }
  182. }
  183. func initCreds() error {
  184. f, err := os.Open("credentials.json")
  185. if err != nil {
  186. return err
  187. }
  188. defer f.Close()
  189. if err != nil {
  190. log.Fatal(err)
  191. return err
  192. }
  193. dec := json.NewDecoder(f)
  194. for {
  195. if err := dec.Decode(&credentials); err == io.EOF {
  196. break
  197. } else if err != nil {
  198. log.Fatal(err)
  199. }
  200. }
  201. return nil
  202. }
  203. func main() {
  204. err := initCreds()
  205. if err != nil {
  206. panic(err)
  207. }
  208. songs, err = getSongs()
  209. if err != nil {
  210. panic(err)
  211. }
  212. gocron.Every(1).Second().Do(task)
  213. <-gocron.Start()
  214. }