123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- package main
-
- import (
- "container/heap"
- "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 = "<!-- Lisääjä -->"
- const SONG_MARK = "<!-- Kappale -->"
- 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(title string, week int, author string, song string) (bool, error) {
- wiki := CreateWikiClient(credentials.APIURL, credentials.UserName, credentials.Password)
-
- sections, err := wiki.GetWikiPageSections(title)
- if err != nil {
- return false, err
- }
-
- numberReg, _ := regexp.Compile("\\d+")
- for _, section := range sections {
- weekStr := numberReg.FindString(section.title)
- if weekStr != "" {
- weekNumber, _ := strconv.Atoi(weekStr)
- if weekNumber == week {
- wikiText, err := wiki.GetWikiPageSectionText(title, section.index)
- if err != nil {
- return false, err
- }
- changedWikiText := appendSong(wikiText, author, song)
-
- return wiki.EditWikiPageSection(title, section.index, changedWikiText,
- fmt.Sprintf("Added week %d song for %s", week, author))
- }
- }
- }
- return false, errors.New("Could not find matching section")
- }
-
- func songSynced(syncedWeek int) error {
- v, err := loadDb()
- if err != nil {
- return err
- }
-
- synced := false
- for _, song := range v.Songs {
- if song.Week == syncedWeek {
- song.Sync = true
- synced = true
- err := saveDb(v)
- if err != nil {
- return err
- }
- }
- }
- if !synced {
- return errors.New("No week matched from JSON for synced song")
- }
- return errors.New("Week not found")
- }
-
- func getSongs() (SongPriorityQueue, error) {
- dbSongs, err := loadDb()
- if err != nil {
- return nil, err
- }
-
- songs := make(SongPriorityQueue, len(dbSongs.Songs))
- for index, songObj := range dbSongs.Songs {
- songs[index] = &Song{
- time: targetTime(songObj),
- song: songEntryWikiText(songObj),
- week: songObj.Week,
- sync: songObj.Sync,
- index: index,
- }
- }
- heap.Init(&songs)
- for len(songs) > 0 && songs[0].sync {
- heap.Pop(&songs)
- }
- return songs, nil
- }
-
- func submitSong() {
- now := time.Now()
- if len(songs) > 0 && songs[0].time.Before(now) {
- fmt.Println("Time has passed for " + songs[0].song)
- success, err := addSong("Levyraati 2018", songs[0].week, "Lamperi", songs[0].song)
- if err != nil {
- log.Println("Error while adding song:", err)
- }
- if success {
- err := songSynced(songs[0].week)
- if err == nil {
- heap.Pop(&songs)
- } else {
- fmt.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
- }
- 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 = "<!-- Lisääjä -->"
- const SONG_MARK = "<!-- Kappale -->"
- const AVERAGE_MARK = "<!-- KA -->"
- 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 targetTime(entry *SongEntry) time.Time {
- yearStart, _ := time.Parse(time.RFC3339, "2018-01-01T00:00:00+02:00")
- target := yearStart.AddDate(0, 0, (entry.Week-1)*7)
- return target
- }
-
- func songEntryWikiText(entry *SongEntry) string {
- return songWikiText(entry.URL, entry.Artist, entry.Title)
- }
-
- func songWikiText(url string, artist string, title string) string {
- return "[" + url + " " + artist + " - " + title + "]"
- }
-
- func main() {
- err := initCreds()
- if err != nil {
- panic(err)
- }
- songs, err = getSongs()
- if err != nil {
- panic(err)
- }
-
- modifiedSongChan := make(chan *SongEntry)
-
- spotifyClient := spotify.NewClient(credentials.SpotifyClientID, credentials.SpotifyClientSecret)
- go func() {
-
- webStart(credentials.ListenAddr, modifiedSongChan, spotifyClient)
- }()
-
- go func() {
- for {
- newSong := <-modifiedSongChan
- matched := false
- for _, song := range songs {
- if song.week == newSong.Week {
- song.song = songEntryWikiText(newSong)
- song.sync = newSong.Sync
- matched = true
- log.Printf("Updated song for week %d, artist: %s, title: %s, URL: %s, time: %v",
- newSong.Week, newSong.Artist, newSong.Title, newSong.URL, song.time)
- }
- }
- if !matched {
- song := &Song{
- time: targetTime(newSong),
- song: songEntryWikiText(newSong),
- week: newSong.Week,
- sync: newSong.Sync,
- index: len(songs),
- }
- heap.Push(&songs, song)
- log.Printf("Added song for week %d, artist: %s, title: %s, URL: %s, time: %v",
- newSong.Week, newSong.Artist, newSong.Title, newSong.URL, song.time)
- }
- }
- }()
-
- gocron.Every(1).Hour().Do(fixAveragesTask)
-
- gocron.Every(1).Second().Do(submitSong)
- <-gocron.Start()
-
- }
|