bot.go 11KB

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