bot.go 12KB

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