about summary refs log tree commit diff
path: root/tools/mpd-stats/cmd/mpd-scrobbler/main.go
blob: ba7bb05d8b2088ada086ef3343e51362605a355d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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 func() {
		if err := c.Close(); err != nil {
			log.Fatalf("failed to close the player: %v", err)
		}
	}()

	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
			}
		}
	}
}