aboutsummaryrefslogblamecommitdiff
path: root/cmd/mpd-scrobbler/main.go
blob: 39531232b46857198c3a4e2511cabcd72c1e89b4 (plain) (tree)
1
2
3
4
5
6
7





                                                 
                                                       





                                
                                          






                                                                



                                                






                                                                                 





















                                                                                                                                                                   


                 
package main

import (
	"log"

	"golang.fcuny.net/mpd-stats/internal/mpd"
	"golang.fcuny.net/mpd-stats/internal/scrobbler"
)

func main() {
	net := "tcp"
	addr := "localhost:6600"

	c, err := mpd.NewPlayer(net, addr)
	if err != nil {
		log.Fatalf("failed to create a client: %v", err)
	}

	defer c.Watcher.Close()
	defer c.Client.Close()

	var (
		currentRecord  *scrobbler.Record
		previousRecord *scrobbler.Record
	)
	for {
		e := <-c.Watcher.Event
		if e != "" {
			attrs, err := c.Client.CurrentSong()
			if err != nil {
				log.Fatalf("could not get current song: %v", err)
			}

			if currentRecord == nil {
				currentRecord, err = scrobbler.NewRecord(attrs)
				if err != nil {
					log.Fatalf("could not create a log: %v", err)
				}
				log.Printf("we're playing %s/%s/%s [%s]\n", currentRecord.Artist, currentRecord.Album, currentRecord.Title, currentRecord.Duration)
				previousRecord = currentRecord
				continue
			}

			if currentRecord.Title != attrs["Title"] || currentRecord.Artist != attrs["Artist"] || currentRecord.Album != attrs["Album"] {
				currentRecord, err = scrobbler.NewRecord(attrs)
				if err != nil {
					log.Fatalf("could not create a log: %v", err)
				}
			}

			if currentRecord.Id != previousRecord.Id {
				log.Printf("we're playing %s/%s/%s [%s]\n", currentRecord.Artist, currentRecord.Album, currentRecord.Title, currentRecord.Duration)
				previousRecord = currentRecord
			}
		}
	}
}