package main import ( "encoding/json" "errors" "fmt" "github.com/jasonlvhit/gocron" "github.com/lamperi/e4bot/spotify" "io" "log" "os" "regexp" "strconv" "strings" "time" ) var songs SongPriorityQueue var credentials struct { APIURL string UserName string Password string SpotifyClientID string SpotifyClientSecret string ListenAddr string } 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 addSong(updateTitle, updateSection, author, song string) (bool, error) { wiki := CreateWikiClient(credentials.APIURL, credentials.UserName, credentials.Password) 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 fmt.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 songSynced(userId, roundId int) error { query := `UPDATE public.entry SET synced = true WHERE user_id = $1 AND round_id = $2` res, err := getDb().Exec(query, userId, roundId) if err != nil { return err } affected, err := res.RowsAffected() if err != nil { return err } if affected != 1 { return errors.New("Unknown entry ID") } return nil } func submitSong() { query := ` SELECT e.user_id, e.round_id, e.artist, e.title, e.spotify_url, p.article, u.username, r.section FROM public.entry e JOIN public."user" u ON u.id = e.user_id JOIN public.round r ON r.id = e.round_id JOIN public.panel p ON p.id = r.panel_id WHERE r.start < current_timestamp AND e.synced = false` rows, err := getDb().Query(query) if err != nil { log.Println("Error while reading songs from database:", err) return } defer rows.Close() for rows.Next() { var ( userId, roundId int artist, title, spotifyURL, article, username, section string ) err := rows.Scan(&userId, &roundId, &artist, &title, &spotifyURL, &article, &username, §ion) if err != nil { log.Println("Error while scanning row:", err) continue } song := songWikiText(spotifyURL, artist, title) fmt.Println("Time has passed for " + song) success, err := addSong(article, section, username, song) if err != nil { log.Println("Error while adding song:", err) } if success { err = songSynced(userId, roundId) if err != nil { fmt.Println("Error received:", err) } } } err = rows.Err() if err != nil { log.Println("Error after reading cursor:", err) return } } 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 } 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\\]\\]){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]) } fmt.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 { expression := fmt.Sprintf("'''{{#expr:(%s)/%d round 2}}'''", strings.Join(scores, "+"), count) newLine := "| " + AVERAGE_MARK + " " + expression changedLines = append(changedLines, newLine) if newLine != line { fmt.Printf("Difference for %s\n%s\n%s\n", currentAuthor, newLine, line) } } else { changedLines = append(changedLines, line) } } return strings.Join(changedLines, "\n") } func fixAverages(title string) error { wiki := CreateWikiClient(credentials.APIURL, credentials.UserName, credentials.Password) sections, err := wiki.GetWikiPageSections(title) if err != nil { 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-1 { continue } if weekNumber > currentWeek { break } wikiText, err := wiki.GetWikiPageSectionText(title, section.index) if err != nil { return err } changedWikiText := appendAverages(wikiText) if changedWikiText != wikiText { //fmt.Println(wikiText) //fmt.Println(changedWikiText) _, err := wiki.EditWikiPageSection(title, section.index, changedWikiText, fmt.Sprintf("Calculate averages for week %d", weekNumber)) if err != nil { return err } } } } return nil } func fixAveragesTask() { err := fixAverages("Levyraati 2018") if err != nil { fmt.Println("Error while calculating averages:", err) } } func initCreds() error { f, err := os.Open("credentials.json") if err != nil { return err } defer f.Close() if err != nil { log.Fatal(err) return err } dec := json.NewDecoder(f) for { if err := dec.Decode(&credentials); err == io.EOF { break } else if err != nil { log.Fatal(err) } } return nil } func songWikiText(url string, artist string, title string) string { return "[" + url + " " + artist + " - " + title + "]" } func main() { err := initCreds() if err != nil { panic(err) } spotifyClient := spotify.NewClient(credentials.SpotifyClientID, credentials.SpotifyClientSecret) go func() { webStart(credentials.ListenAddr, spotifyClient) }() gocron.Every(1).Hour().Do(fixAveragesTask) gocron.Every(1).Second().Do(submitSong) <-gocron.Start() }