bot.go 13KB

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