package main import ( "encoding/json" "errors" "flag" "fmt" "github.com/jasonlvhit/gocron" "github.com/zmb3/spotify" "io" "log" "os" "regexp" "strconv" "strings" "time" ) type App struct { db *DB credentials Credentials spotify *AppSpotify } type AppSpotify struct { auth *spotify.Authenticator client *spotify.Client } type Credentials struct { APIURL string UserName string Password string SpotifyClientID string SpotifyClientSecret string SpotifyUser string SpotifyCallback string ListenAddr string } func (app *App) CreateSpotifyAuthenticator() *spotify.Authenticator { auth := spotify.NewAuthenticator(app.credentials.SpotifyCallback, spotify.ScopeUserReadPrivate, spotify.ScopePlaylistModifyPrivate, spotify.ScopePlaylistModifyPublic) auth.SetAuthInfo(app.credentials.SpotifyClientID, app.credentials.SpotifyClientSecret) return &auth } func (app *App) LaunchWeb() { app.spotify.auth = app.CreateSpotifyAuthenticator() go func() { webStart(app.credentials.ListenAddr, app.db, app.spotify) }() } func appendSong(wikiText string, author string, newSong string) string { const AUTHOR_MARK = "" const SONG_MARK = "" lines := strings.Split(wikiText, "\n") authorPrevIndex := -2 changedLines := make([]string, 0, len(lines)) for index, line := range lines { if strings.Index(line, AUTHOR_MARK) != -1 && strings.Index(line, author) != -1 { authorPrevIndex = index } if authorPrevIndex == (index-1) && strings.Index(line, SONG_MARK) != -1 { changedLines = append(changedLines, "| "+SONG_MARK+" "+newSong) } else { changedLines = append(changedLines, line) } } return strings.Join(changedLines, "\n") } func (app *App) wikiClient() *WikiClient { wiki := CreateWikiClient(app.credentials.APIURL, app.credentials.UserName, app.credentials.Password) return wiki } func (app *App) AddSong(updateTitle, updateSection, author, song string) (bool, error) { wiki := app.wikiClient() sections, err := wiki.GetWikiPageSections(updateTitle) if err != nil { return false, err } for _, section := range sections { if updateSection == section.title { wikiText, err := wiki.GetWikiPageSectionText(updateTitle, section.index) if err != nil { return false, err } changedWikiText := appendSong(wikiText, author, song) if false { // Stub log.Println("Pretend to update wiki text to ", updateTitle, section.index, changedWikiText, fmt.Sprintf("Added %s song for %s", updateSection, author)) return true, nil } return wiki.EditWikiPageSection(updateTitle, section.index, changedWikiText, fmt.Sprintf("Added %s song for %s", updateSection, author)) } } return false, errors.New("Could not find matching section") } func (app *App) SongSynced(userId, roundId int) error { _, err := app.db.EntrySynced(userId, roundId) return err } func (app *App) SubmitSongs() { entries, err := app.db.FindEntriesToSync() if err != nil { log.Println("Error while finding entries to sync:", err) return } for _, entry := range entries { song := songWikiText(entry.spotifyURL, entry.artist, entry.title) log.Println("Time has passed for " + song) success, err := app.AddSong(entry.article, entry.section, entry.username, song) if err != nil { log.Println("Error while adding song:", err) } if success { err = app.SongSynced(entry.userId, entry.roundId) if err != nil { log.Println("Error received:", err) } } } } func isCurrentAuthor(line, author string) bool { authorIndex := strings.Index(line, author) endIndex := strings.Index(line, "-->") return authorIndex != -1 && authorIndex < endIndex } func parseScore(line string) string { parts := strings.Split(line, "-->") if len(parts) < 2 { return "" } score := strings.TrimRight(strings.Trim(parts[1], " \t\n"), "p") if score == "" { return score } rating, _ := regexp.Compile("^\\{\\{Rating\\|\\d([.,]\\d)?\\|5\\}\\}$") if rating.MatchString(score) { score = strings.Replace(score, "{{Rating|", "", 1) score = strings.Replace(score, "|5}}", "", 1) } number, _ := regexp.Compile("^\\d([.,]\\d)?$") if number.MatchString(score) { return strings.Replace(score, ",", ".", 1) } numberHalf, _ := regexp.Compile("^\\d½$") if numberHalf.MatchString(score) { return fmt.Sprintf("%c.5", score[0]) } stars, _ := regexp.Compile("^\\*+$") if stars.MatchString(score) { return fmt.Sprintf("%d", len(score)) } imageStars, _ := regexp.Compile("^(\\[\\[Image:[01]\\.png\\]\\]){1,5}$") if imageStars.MatchString(score) { return fmt.Sprintf("%d", strings.Count(score, "1")) } quarterScore, _ := regexp.Compile("^\\d-\\d½?$") if quarterScore.MatchString(score) { return fmt.Sprintf("%c.25", score[0]) } thirdQuarterScore, _ := regexp.Compile("^\\d½?-\\d$") if thirdQuarterScore.MatchString(score) { return fmt.Sprintf("%c.75", score[0]) } log.Printf("Could not match '%s'\n", score) return "" } func appendAverages(wikiText string) string { const AUTHOR_MARK = "" const SONG_MARK = "" const AVERAGE_MARK = "" lines := strings.Split(wikiText, "\n") isScore := false scores := make([]string, 0) count := 0 currentAuthor := "" changedLines := make([]string, 0, len(lines)) for _, line := range lines { if strings.Index(line, AUTHOR_MARK) != -1 { currentAuthor = strings.Trim(strings.Split(line, AUTHOR_MARK)[1], " \t") } else if strings.Index(line, SONG_MARK) != -1 { isScore = true scores = make([]string, 0) count = 0 } else if isScore && strings.Index(line, AVERAGE_MARK) == -1 { if !isCurrentAuthor(line, currentAuthor) { score := parseScore(line) if score != "" { scores = append(scores, score) count += 1 } else { scores = append(scores, "0") } } } if strings.Index(line, AVERAGE_MARK) != -1 && count > 2 { expression := fmt.Sprintf("'''{{#expr:(%s)/%d round 2}}'''", strings.Join(scores, "+"), count) newLine := "| " + AVERAGE_MARK + " " + expression changedLines = append(changedLines, newLine) if newLine != line { log.Printf("Difference for %s\n%s\n%s\n", currentAuthor, newLine, line) } } else { changedLines = append(changedLines, line) } } return strings.Join(changedLines, "\n") } func findPlaylist(wikiText string) (spotify.ID, []spotify.ID) { const SONG_MARK = "" const SPOTIFY_MARK = "https://open.spotify.com/track/" const SPOTIFY_PLAYLIST_MARK = "/playlist/" const PLAYLIST_MARK = " Spotify-soittolista]" lines := strings.Split(wikiText, "\n") var playlistId spotify.ID tracks := make([]spotify.ID, 0) for _, line := range lines { if strings.Index(line, SONG_MARK) != -1 { i := strings.Index(line, SPOTIFY_MARK) if i != -1 { j := strings.Index(line[i:], " ") if j != -1 { j += i } trackId := line[i+len(SPOTIFY_MARK) : j] tracks = append(tracks, spotify.ID(chopArgs(trackId))) } } else if strings.Index(line, SPOTIFY_PLAYLIST_MARK) != -1 && strings.Index(line, PLAYLIST_MARK) != -1 { i := strings.Index(line, SPOTIFY_PLAYLIST_MARK) j := strings.Index(line[i:], PLAYLIST_MARK) playlist := line[i+len(SPOTIFY_PLAYLIST_MARK) : i+j] q := strings.Index(playlist, "?") if q != -1 { playlist = playlist[:q] } playlistId = spotify.ID(chopArgs(playlist)) } } log.Printf("Found playlist %s and tracks %s\n", playlistId, tracks) return spotify.ID(playlistId), tracks } func chopArgs(value string) string { q := strings.Index(value, "?") if q != -1 { value = value[:q] } return value } func appendPlaylist(wikiText string, playlist *spotify.FullPlaylist) string { changedText := wikiText + ` [` + playlist.ExternalURLs["spotify"] + ` Spotify-soittolista] ` return changedText } func tracksEqual(tracks1 []spotify.ID, tracks2 []string) bool { if len(tracks1) != len(tracks2) { return false } for i, track1 := range tracks1 { track2 := tracks2[i] if string(track1) != track2 { return false } } return true } func (app *App) AutomateSection(title string) error { wiki := app.wikiClient() sections, err := wiki.GetWikiPageSections(title) if err != nil { log.Println("Error while reading page sections") return err } _, currentWeek := time.Now().ISOWeek() numberReg, _ := regexp.Compile("\\d+") for _, section := range sections { weekStr := numberReg.FindString(section.title) if weekStr != "" { weekNumber, _ := strconv.Atoi(weekStr) if weekNumber < currentWeek-4 { continue } if weekNumber > currentWeek { break } log.Println("Checking section", section.title) wikiText, err := wiki.GetWikiPageSectionText(title, section.index) if err != nil { log.Println("Error reading text from wiki") return err } message := "" changedWikiText := appendAverages(wikiText) if changedWikiText != wikiText { message = message + fmt.Sprintf("Calculate averages for week %d. ", weekNumber) } if app.spotify.client != nil { token, err := app.spotify.client.Token() if err != nil { log.Println("No token available") } else if token.Expiry.Before(time.Now()) { log.Println("Token has expired") } else { playlistID, tracks := findPlaylist(changedWikiText) currentTracks, err := app.db.FindPlaylistBySection(section.title) if err != nil { log.Println("Failed to find tracks from DB", err) } log.Println("Checking if playlist needs updating", currentTracks, tracks, err) if app.ShouldUpdate(tracks, currentTracks, err != nil) { playlistID, changedWikiText, message, err = app.CreatePlaylist(title+" "+section.title, weekNumber, playlistID, changedWikiText, message) if err != nil { return err } err = app.UpdatePlaylist(section.title, playlistID, tracks, currentTracks) if err != nil { return err } } if weekNumber == currentWeek { // This playlist is same for every week // So people don't have to find the new ones from wiki playlistID = "4piDy2DQ35Y43yUaInyWfN" currentTracks, err = app.db.FindPlaylistBySection("current week") if err != nil { log.Println("Failed to find current week tracks from DB", err) } log.Println("Checking if playlist needs updating", currentTracks, tracks, err) if app.ShouldUpdate(tracks, currentTracks, err != nil) { err = app.UpdatePlaylist("current week", playlistID, tracks, currentTracks) if err != nil { return err } } } } } if message != "" { _, err := wiki.EditWikiPageSection(title, section.index, changedWikiText, fmt.Sprintf("Calculate averages for week %d", weekNumber)) //err = errors.New("foo") if err != nil { return err } } } } return nil } func (app *App) ShouldUpdate(tracks []spotify.ID, currentTracks []string, isNew bool) bool { return len(tracks) > 0 && (isNew || !tracksEqual(tracks, currentTracks)) } func (app *App) CreatePlaylist(title string, weekNumber int, playlistID spotify.ID, changedWikiText, message string) (spotify.ID, string, string, error) { if playlistID == "" { log.Println("Creating new playlist") info, err := app.spotify.client.CreatePlaylistForUser(app.credentials.SpotifyUser, title, true) if err != nil { log.Println("Error creating playlist to Spotify") return "", "", "", err } playlistID = info.ID changedWikiText = appendPlaylist(changedWikiText, info) message = message + fmt.Sprintf("Added link to Spotify playlist for week %d.", weekNumber) } return playlistID, changedWikiText, message, nil } func (app *App) UpdatePlaylist(section string, playlistID spotify.ID, tracks []spotify.ID, currentTracks []string) error { log.Printf("Updating playlist %s for %s with tracks %s\n", playlistID, app.credentials.SpotifyUser, tracks) err := app.spotify.client.ReplacePlaylistTracks(app.credentials.SpotifyUser, playlistID, tracks...) if err != nil { log.Println("Error updating playlist to Spotify") return err } stringTracks := make([]string, len(tracks)) for i, t := range tracks { stringTracks[i] = string(t) } _, err = app.db.UpdatePlaylistBySection(section, stringTracks) if err != nil { log.Println("Error updating playlist to DB") return err } return nil } func (app *App) AutomateSectionTask() { panels, err := app.db.FindAllPanels() if err != nil { log.Println("Error while checking db for panels:", err) return } for _, panel := range panels { log.Println("Checking panel", panel) err := app.AutomateSection(panel) if err != nil { log.Println("Error while processing panel:", err) } } } func initCreds() (Credentials, error) { var credsFile string flag.StringVar(&credsFile, "credentials", "credentials.json", "JSON config to hold app credentials") flag.Parse() var credentials Credentials f, err := os.Open(credsFile) if err != nil { return credentials, err } defer f.Close() if err != nil { log.Fatal(err) return credentials, err } dec := json.NewDecoder(f) for { if err := dec.Decode(&credentials); err == io.EOF { break } else if err != nil { log.Fatal(err) } } return credentials, nil } func songWikiText(url string, artist string, title string) string { return "[" + url + " " + artist + " - " + title + "]" } func main() { creds, err := initCreds() if err != nil { panic(err) } a := App{InitDatabase(), creds, &AppSpotify{}} a.LaunchWeb() gocron.Every(1).Minute().Do(a.AutomateSectionTask) gocron.Every(1).Second().Do(a.SubmitSongs) <-gocron.Start() }