bot.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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.ScopeUserReadPrivate, spotify.ScopePlaylistModifyPrivate, 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(chopArgs(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(chopArgs(playlist))
  235. }
  236. }
  237. log.Printf("Found playlist %s and tracks %s\n", playlistId, tracks)
  238. return spotify.ID(playlistId), tracks
  239. }
  240. func chopArgs(value string) string {
  241. q := strings.Index(value, "?")
  242. if q != -1 {
  243. value = value[:q]
  244. }
  245. return value
  246. }
  247. func appendPlaylist(wikiText string, playlist *spotify.FullPlaylist) string {
  248. changedText := wikiText + `
  249. [` + playlist.ExternalURLs["spotify"] + ` Spotify-soittolista]
  250. `
  251. return changedText
  252. }
  253. func tracksEqual(tracks1 []spotify.ID, tracks2 []string) bool {
  254. if len(tracks1) != len(tracks2) {
  255. return false
  256. }
  257. for i, track1 := range tracks1 {
  258. track2 := tracks2[i]
  259. if string(track1) != track2 {
  260. return false
  261. }
  262. }
  263. return true
  264. }
  265. func (app *App) AutomateSection(title string) error {
  266. wiki := app.wikiClient()
  267. sections, err := wiki.GetWikiPageSections(title)
  268. if err != nil {
  269. log.Println("Error while reading page sections")
  270. return err
  271. }
  272. _, currentWeek := time.Now().ISOWeek()
  273. numberReg, _ := regexp.Compile("\\d+")
  274. for _, section := range sections {
  275. weekStr := numberReg.FindString(section.title)
  276. if weekStr != "" {
  277. weekNumber, _ := strconv.Atoi(weekStr)
  278. if weekNumber < currentWeek-1 {
  279. continue
  280. }
  281. if weekNumber > currentWeek {
  282. break
  283. }
  284. log.Println("Checking section", section.title)
  285. wikiText, err := wiki.GetWikiPageSectionText(title, section.index)
  286. if err != nil {
  287. log.Println("Error reading text from wiki")
  288. return err
  289. }
  290. message := ""
  291. changedWikiText := appendAverages(wikiText)
  292. if changedWikiText != wikiText {
  293. message = message + fmt.Sprintf("Calculate averages for week %d. ", weekNumber)
  294. }
  295. if app.spotify.client != nil {
  296. token, err := app.spotify.client.Token()
  297. if err != nil {
  298. log.Println("No token available")
  299. } else if token.Expiry.Before(time.Now()) {
  300. log.Println("Token has expired")
  301. } else {
  302. playlistID, tracks := findPlaylist(changedWikiText)
  303. currentTracks, err := app.db.FindPlaylistBySection(section.title)
  304. if err != nil {
  305. log.Println("Failed to find tracks from DB", err)
  306. }
  307. log.Println("Checking if playlist needs updating", currentTracks, tracks, err)
  308. if len(tracks) > 0 && (err != nil || !tracksEqual(currentTracks, tracks)) {
  309. if playlistID == "" {
  310. log.Println("Creating new playlist")
  311. info, err := app.spotify.client.CreatePlaylistForUser(app.credentials.SpotifyUser, title+" "+section.title, true)
  312. if err != nil {
  313. log.Println("Error creating playlist to Spotify")
  314. return err
  315. }
  316. playlistID = info.ID
  317. changedWikiText = appendPlaylist(changedWikiText, info)
  318. message = message + fmt.Sprintf("Added link to Spotify playlist for week %d.", weekNumber)
  319. }
  320. log.Printf("Updating playlist %s for %s with tracks %s\n", playlistID, app.credentials.SpotifyUser, tracks)
  321. err := app.spotify.client.ReplacePlaylistTracks(app.credentials.SpotifyUser, playlistID, tracks...)
  322. if err != nil {
  323. log.Println("Error updating playlist to Spotify")
  324. return err
  325. }
  326. stringTracks := make([]string, len(tracks))
  327. for i, t := range tracks {
  328. stringTracks[i] = string(t)
  329. }
  330. _, err = app.db.UpdatePlaylistBySection(section.title, stringTracks)
  331. if err != nil {
  332. log.Println("Error updating playlist to DB")
  333. return err
  334. }
  335. }
  336. }
  337. }
  338. if message != "" {
  339. _, err := wiki.EditWikiPageSection(title, section.index, changedWikiText,
  340. fmt.Sprintf("Calculate averages for week %d", weekNumber))
  341. //err = errors.New("foo")
  342. if err != nil {
  343. return err
  344. }
  345. }
  346. }
  347. }
  348. return nil
  349. }
  350. func (app *App) AutomateSectionTask() {
  351. panels, err := app.db.FindAllPanels()
  352. if err != nil {
  353. log.Println("Error while checking db for panels:", err)
  354. return
  355. }
  356. for _, panel := range panels {
  357. log.Println("Checking panel", panel)
  358. err := app.AutomateSection(panel)
  359. if err != nil {
  360. log.Println("Error while processing panel:", err)
  361. }
  362. }
  363. }
  364. func initCreds() (Credentials, error) {
  365. var credsFile string
  366. flag.StringVar(&credsFile, "credentials", "credentials.json", "JSON config to hold app credentials")
  367. flag.Parse()
  368. var credentials Credentials
  369. f, err := os.Open(credsFile)
  370. if err != nil {
  371. return credentials, err
  372. }
  373. defer f.Close()
  374. if err != nil {
  375. log.Fatal(err)
  376. return credentials, err
  377. }
  378. dec := json.NewDecoder(f)
  379. for {
  380. if err := dec.Decode(&credentials); err == io.EOF {
  381. break
  382. } else if err != nil {
  383. log.Fatal(err)
  384. }
  385. }
  386. return credentials, nil
  387. }
  388. func songWikiText(url string, artist string, title string) string {
  389. return "[" + url + " " + artist + " - " + title + "]"
  390. }
  391. func main() {
  392. creds, err := initCreds()
  393. if err != nil {
  394. panic(err)
  395. }
  396. a := App{InitDatabase(), creds, &AppSpotify{}}
  397. a.LaunchWeb()
  398. gocron.Every(1).Minute().Do(a.AutomateSectionTask)
  399. gocron.Every(1).Second().Do(a.SubmitSongs)
  400. <-gocron.Start()
  401. }