bot.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "flag"
  6. "fmt"
  7. "github.com/jasonlvhit/gocron"
  8. "github.com/zmb3/spotify"
  9. "io"
  10. "log"
  11. "os"
  12. "reflect"
  13. "regexp"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type App struct {
  19. db *DB
  20. credentials Credentials
  21. spotify *AppSpotify
  22. }
  23. type AppSpotify struct {
  24. auth *spotify.Authenticator
  25. client *spotify.Client
  26. }
  27. type Credentials struct {
  28. APIURL string
  29. UserName string
  30. Password string
  31. SpotifyClientID string
  32. SpotifyClientSecret string
  33. SpotifyUser string
  34. SpotifyCallback string
  35. ListenAddr string
  36. }
  37. func (app *App) CreateSpotifyAuthenticator() *spotify.Authenticator {
  38. auth := spotify.NewAuthenticator(app.credentials.SpotifyCallback, spotify.ScopePlaylistModifyPublic)
  39. auth.SetAuthInfo(app.credentials.SpotifyClientID, app.credentials.SpotifyClientSecret)
  40. return &auth
  41. }
  42. func (app *App) LaunchWeb() {
  43. app.spotify.auth = app.CreateSpotifyAuthenticator()
  44. go func() {
  45. webStart(app.credentials.ListenAddr, app.db, app.spotify)
  46. }()
  47. }
  48. func appendSong(wikiText string, author string, newSong string) string {
  49. const AUTHOR_MARK = "<!-- Lisääjä -->"
  50. const SONG_MARK = "<!-- Kappale -->"
  51. lines := strings.Split(wikiText, "\n")
  52. authorPrevIndex := -2
  53. changedLines := make([]string, 0, len(lines))
  54. for index, line := range lines {
  55. if strings.Index(line, AUTHOR_MARK) != -1 && strings.Index(line, author) != -1 {
  56. authorPrevIndex = index
  57. }
  58. if authorPrevIndex == (index-1) && strings.Index(line, SONG_MARK) != -1 {
  59. changedLines = append(changedLines, "| "+SONG_MARK+" "+newSong)
  60. } else {
  61. changedLines = append(changedLines, line)
  62. }
  63. }
  64. return strings.Join(changedLines, "\n")
  65. }
  66. func (app *App) wikiClient() *WikiClient {
  67. wiki := CreateWikiClient(app.credentials.APIURL, app.credentials.UserName, app.credentials.Password)
  68. return wiki
  69. }
  70. func (app *App) AddSong(updateTitle, updateSection, author, song string) (bool, error) {
  71. wiki := app.wikiClient()
  72. sections, err := wiki.GetWikiPageSections(updateTitle)
  73. if err != nil {
  74. return false, err
  75. }
  76. for _, section := range sections {
  77. if updateSection == section.title {
  78. wikiText, err := wiki.GetWikiPageSectionText(updateTitle, section.index)
  79. if err != nil {
  80. return false, err
  81. }
  82. changedWikiText := appendSong(wikiText, author, song)
  83. if false {
  84. // Stub
  85. log.Println("Pretend to update wiki text to ", updateTitle, section.index, changedWikiText,
  86. fmt.Sprintf("Added %s song for %s", updateSection, author))
  87. return true, nil
  88. }
  89. return wiki.EditWikiPageSection(updateTitle, section.index, changedWikiText,
  90. fmt.Sprintf("Added %s song for %s", updateSection, author))
  91. }
  92. }
  93. return false, errors.New("Could not find matching section")
  94. }
  95. func (app *App) SongSynced(userId, roundId int) error {
  96. _, err := app.db.EntrySynced(userId, roundId)
  97. return err
  98. }
  99. func (app *App) SubmitSongs() {
  100. entries, err := app.db.FindEntriesToSync()
  101. if err != nil {
  102. log.Println("Error while finding entries to sync:", err)
  103. return
  104. }
  105. for _, entry := range entries {
  106. song := songWikiText(entry.spotifyURL, entry.artist, entry.title)
  107. log.Println("Time has passed for " + song)
  108. success, err := app.AddSong(entry.article, entry.section, entry.username, song)
  109. if err != nil {
  110. log.Println("Error while adding song:", err)
  111. }
  112. if success {
  113. err = app.SongSynced(entry.userId, entry.roundId)
  114. if err != nil {
  115. log.Println("Error received:", err)
  116. }
  117. }
  118. }
  119. }
  120. func isCurrentAuthor(line, author string) bool {
  121. authorIndex := strings.Index(line, author)
  122. endIndex := strings.Index(line, "-->")
  123. return authorIndex != -1 && authorIndex < endIndex
  124. }
  125. func parseScore(line string) string {
  126. parts := strings.Split(line, "-->")
  127. if len(parts) < 2 {
  128. return ""
  129. }
  130. score := strings.TrimRight(strings.Trim(parts[1], " \t\n"), "p")
  131. if score == "" {
  132. return score
  133. }
  134. rating, _ := regexp.Compile("^\\{\\{Rating\\|\\d([.,]\\d)?\\|5\\}\\}$")
  135. if rating.MatchString(score) {
  136. score = strings.Replace(score, "{{Rating|", "", 1)
  137. score = strings.Replace(score, "|5}}", "", 1)
  138. }
  139. number, _ := regexp.Compile("^\\d([.,]\\d)?$")
  140. if number.MatchString(score) {
  141. return strings.Replace(score, ",", ".", 1)
  142. }
  143. numberHalf, _ := regexp.Compile("^\\d½$")
  144. if numberHalf.MatchString(score) {
  145. return fmt.Sprintf("%c.5", score[0])
  146. }
  147. stars, _ := regexp.Compile("^\\*+$")
  148. if stars.MatchString(score) {
  149. return fmt.Sprintf("%d", len(score))
  150. }
  151. imageStars, _ := regexp.Compile("^(\\[\\[Image:[01]\\.png\\]\\]){5}$")
  152. if imageStars.MatchString(score) {
  153. return fmt.Sprintf("%d", strings.Count(score, "1"))
  154. }
  155. quarterScore, _ := regexp.Compile("^\\d-\\d½?$")
  156. if quarterScore.MatchString(score) {
  157. return fmt.Sprintf("%c.25", score[0])
  158. }
  159. thirdQuarterScore, _ := regexp.Compile("^\\d½?-\\d$")
  160. if thirdQuarterScore.MatchString(score) {
  161. return fmt.Sprintf("%c.75", score[0])
  162. }
  163. log.Printf("Could not match '%s'\n", score)
  164. return ""
  165. }
  166. func appendAverages(wikiText string) string {
  167. const AUTHOR_MARK = "<!-- Lisääjä -->"
  168. const SONG_MARK = "<!-- Kappale -->"
  169. const AVERAGE_MARK = "<!-- KA -->"
  170. lines := strings.Split(wikiText, "\n")
  171. isScore := false
  172. scores := make([]string, 0)
  173. count := 0
  174. currentAuthor := ""
  175. changedLines := make([]string, 0, len(lines))
  176. for _, line := range lines {
  177. if strings.Index(line, AUTHOR_MARK) != -1 {
  178. currentAuthor = strings.Trim(strings.Split(line, AUTHOR_MARK)[1], " \t")
  179. } else if strings.Index(line, SONG_MARK) != -1 {
  180. isScore = true
  181. scores = make([]string, 0)
  182. count = 0
  183. } else if isScore && strings.Index(line, AVERAGE_MARK) == -1 {
  184. if !isCurrentAuthor(line, currentAuthor) {
  185. score := parseScore(line)
  186. if score != "" {
  187. scores = append(scores, score)
  188. count += 1
  189. } else {
  190. scores = append(scores, "0")
  191. }
  192. }
  193. }
  194. if strings.Index(line, AVERAGE_MARK) != -1 && count > 2 {
  195. expression := fmt.Sprintf("'''{{#expr:(%s)/%d round 2}}'''", strings.Join(scores, "+"), count)
  196. newLine := "| " + AVERAGE_MARK + " " + expression
  197. changedLines = append(changedLines, newLine)
  198. if newLine != line {
  199. log.Printf("Difference for %s\n%s\n%s\n", currentAuthor, newLine, line)
  200. }
  201. } else {
  202. changedLines = append(changedLines, line)
  203. }
  204. }
  205. return strings.Join(changedLines, "\n")
  206. }
  207. func findPlaylist(wikiText string) (spotify.ID, []spotify.ID) {
  208. const SONG_MARK = "<!-- Kappale -->"
  209. const SPOTIFY_MARK = "https://open.spotify.com/track/"
  210. const SPOTIFY_PLAYLIST_MARK = "/playlist/"
  211. const PLAYLIST_MARK = " Spotify-soittolista]"
  212. lines := strings.Split(wikiText, "\n")
  213. var playlistId spotify.ID
  214. tracks := make([]spotify.ID, 0)
  215. for _, line := range lines {
  216. if strings.Index(line, SONG_MARK) != -1 {
  217. i := strings.Index(line, SPOTIFY_MARK)
  218. if i != -1 {
  219. j := strings.Index(line[i:], " ")
  220. if j != -1 {
  221. j += i
  222. }
  223. trackId := line[i+len(SPOTIFY_MARK) : j]
  224. tracks = append(tracks, spotify.ID(trackId))
  225. }
  226. } else if strings.Index(line, SPOTIFY_PLAYLIST_MARK) != -1 && strings.Index(line, PLAYLIST_MARK) != -1 {
  227. i := strings.Index(line, SPOTIFY_PLAYLIST_MARK)
  228. j := strings.Index(line[i:], PLAYLIST_MARK)
  229. playlist := line[i+len(SPOTIFY_PLAYLIST_MARK) : i+j]
  230. q := strings.Index(playlist, "?")
  231. if q != -1 {
  232. playlist = playlist[:q]
  233. }
  234. playlistId = spotify.ID(playlist)
  235. }
  236. }
  237. log.Printf("Found playlist %s and tracks %s\n", playlistId, tracks)
  238. return spotify.ID(playlistId), tracks
  239. }
  240. func appendPlaylist(wikiText string, playlist *spotify.FullPlaylist) string {
  241. changedText := wikiText + `
  242. [` + playlist.ExternalURLs["spotify"] + ` Spotify-soittolista]
  243. `
  244. return changedText
  245. }
  246. func (app *App) AutomateSection(title string) error {
  247. wiki := app.wikiClient()
  248. sections, err := wiki.GetWikiPageSections(title)
  249. if err != nil {
  250. log.Println("Error while reading page sections")
  251. return err
  252. }
  253. _, currentWeek := time.Now().ISOWeek()
  254. numberReg, _ := regexp.Compile("\\d+")
  255. for _, section := range sections {
  256. weekStr := numberReg.FindString(section.title)
  257. if weekStr != "" {
  258. weekNumber, _ := strconv.Atoi(weekStr)
  259. if weekNumber < currentWeek-1 {
  260. continue
  261. }
  262. if weekNumber > currentWeek {
  263. break
  264. }
  265. log.Println("Checking section", section.title)
  266. wikiText, err := wiki.GetWikiPageSectionText(title, section.index)
  267. if err != nil {
  268. log.Println("Error reading text from wiki")
  269. return err
  270. }
  271. message := ""
  272. changedWikiText := appendAverages(wikiText)
  273. if changedWikiText != wikiText {
  274. message = message + fmt.Sprintf("Calculate averages for week %d. ", weekNumber)
  275. }
  276. if app.spotify.client != nil {
  277. token, err := app.spotify.client.Token()
  278. if err != nil {
  279. log.Println("No token available")
  280. } else if token.Expiry.Before(time.Now()) {
  281. log.Println("Token has expired")
  282. } else {
  283. log.Println("Checking if playlist needs updating")
  284. playlistId, tracks := findPlaylist(changedWikiText)
  285. currentTracks, err := app.db.FindPlaylistBySection(section.title)
  286. if len(tracks) > 0 && (err != nil || reflect.DeepEqual(currentTracks, tracks)) {
  287. if playlistId == "" {
  288. info, err := app.spotify.client.CreatePlaylistForUser(app.credentials.SpotifyUser, title+" "+section.title, true)
  289. if err != nil {
  290. log.Println("Error creating playlist to Spotify")
  291. return err
  292. }
  293. playlistId = info.ID
  294. changedWikiText = appendPlaylist(changedWikiText, info)
  295. message = message + fmt.Sprintf("Added link to Spotify playlist for week %d.", weekNumber)
  296. }
  297. err := app.spotify.client.ReplacePlaylistTracks(app.credentials.SpotifyUser, playlistId, tracks...)
  298. if err != nil {
  299. log.Println("Error updating playlist to Spotify")
  300. return err
  301. }
  302. stringTracks := make([]string, len(tracks))
  303. for i, t := range tracks {
  304. stringTracks[i] = string(t)
  305. }
  306. _, err = app.db.UpdatePlaylistBySection(section.title, stringTracks)
  307. if err != nil {
  308. log.Println("Error updating playlist to DB")
  309. return err
  310. }
  311. }
  312. }
  313. }
  314. if message != "" {
  315. _, err := wiki.EditWikiPageSection(title, section.index, changedWikiText,
  316. fmt.Sprintf("Calculate averages for week %d", weekNumber))
  317. //err = errors.New("foo")
  318. if err != nil {
  319. return err
  320. }
  321. }
  322. }
  323. }
  324. return nil
  325. }
  326. func (app *App) AutomateSectionTask() {
  327. panels, err := app.db.FindAllPanels()
  328. if err != nil {
  329. log.Println("Error while checking db for panels:", err)
  330. return
  331. }
  332. for _, panel := range panels {
  333. log.Println("Checking panel", panel)
  334. err := app.AutomateSection(panel)
  335. if err != nil {
  336. log.Println("Error while processing panel:", err)
  337. }
  338. }
  339. }
  340. func initCreds() (Credentials, error) {
  341. var credsFile string
  342. flag.StringVar(&credsFile, "credentials", "credentials.json", "JSON config to hold app credentials")
  343. flag.Parse()
  344. var credentials Credentials
  345. f, err := os.Open(credsFile)
  346. if err != nil {
  347. return credentials, err
  348. }
  349. defer f.Close()
  350. if err != nil {
  351. log.Fatal(err)
  352. return credentials, err
  353. }
  354. dec := json.NewDecoder(f)
  355. for {
  356. if err := dec.Decode(&credentials); err == io.EOF {
  357. break
  358. } else if err != nil {
  359. log.Fatal(err)
  360. }
  361. }
  362. return credentials, nil
  363. }
  364. func songWikiText(url string, artist string, title string) string {
  365. return "[" + url + " " + artist + " - " + title + "]"
  366. }
  367. func main() {
  368. creds, err := initCreds()
  369. if err != nil {
  370. panic(err)
  371. }
  372. a := App{InitDatabase(), creds, &AppSpotify{}}
  373. a.LaunchWeb()
  374. gocron.Every(1).Minute().Do(a.AutomateSectionTask)
  375. gocron.Every(1).Second().Do(a.SubmitSongs)
  376. <-gocron.Start()
  377. }