-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend.go
155 lines (135 loc) · 3.69 KB
/
backend.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package backend
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/mootslive/mono/backend/trace"
"time"
"github.com/jackc/pgx/v4"
"github.com/mootslive/mono/backend/db"
"github.com/segmentio/ksuid"
"github.com/zmb3/spotify/v2"
spotifyauth "github.com/zmb3/spotify/v2/auth"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"golang.org/x/exp/slog"
"golang.org/x/oauth2"
)
type SpotifyPoller struct {
queries db.TXQuerier
log *slog.Logger
}
func NewSpotifyPoller(log *slog.Logger, queries db.TXQuerier) *SpotifyPoller {
return &SpotifyPoller{
log: log,
queries: queries,
}
}
func (sp *SpotifyPoller) Run(ctx context.Context) error {
sp.log.Info("starting poller")
for {
sp.log.Info("running account scan")
accounts, err := sp.queries.GetSpotifyAccountsForScanning(ctx)
if err != nil {
return fmt.Errorf("fetching accounts: %w", err)
}
for _, account := range accounts {
if err := sp.ScanAccount(ctx, account.SpotifyUserID); err != nil {
return fmt.Errorf("scanning account: %w", err)
}
}
select {
// Simple ten second backoff
case <-time.After(time.Second * 10):
continue
case <-ctx.Done():
sp.log.Info("context cancelled, stopping poller")
return ctx.Err()
}
}
}
const (
sourceSpotify = "spotify"
)
func (sp *SpotifyPoller) ScanAccount(
ctx context.Context, spotifyUserID string,
) error {
ctx, span := trace.Start(ctx, "backend/SpotifyPoller.ScanAccount")
defer span.End()
commit, rollback, tx, err := sp.queries.BeginTx(ctx)
if err != nil {
return fmt.Errorf("opening tx: %w", err)
}
defer func() {
if err := rollback(context.Background()); err != nil {
if !errors.Is(err, pgx.ErrTxClosed) {
sp.log.Error("failed to rollback", err)
}
}
}()
account, err := tx.SelectSpotifyAccountForUpdate(ctx, spotifyUserID)
if err != nil {
return fmt.Errorf("locking account: %w", err)
}
var afterEpochMs int64 = 0
if account.LastListenedAt.Valid {
afterEpochMs = (account.LastListenedAt.Time.Add(time.Second).Unix()) * 1000
}
client := clientForSpotifyAccount(ctx, account)
played, err := client.PlayerRecentlyPlayedOpt(ctx, &spotify.RecentlyPlayedOptions{
Limit: 50,
AfterEpochMs: afterEpochMs,
})
if err != nil {
return fmt.Errorf("fetching recently played: %w", err)
}
var listenedAt *time.Time
for _, track := range played {
// TODO: Batch insert these :)
track := track
sp.log.Debug("recording listen", "user_id", account.UserID, "track_name", track.Track.Name, "listened_at", track.PlayedAt)
err := tx.CreateListen(ctx, db.CreateListenParams{
ID: ksuid.New().String(),
UserID: account.UserID,
CreatedAt: time.Now(),
Source: sourceSpotify,
Isrc: track.Track.ExternalIDs.ISRC,
ListenedAt: track.PlayedAt,
})
if err != nil {
return fmt.Errorf("recording listen: %w", err)
}
if listenedAt == nil {
listenedAt = &track.PlayedAt
}
}
if listenedAt != nil {
err := tx.UpdateSpotifyAccountListenedAt(ctx, db.UpdateSpotifyAccountListenedAtParams{
SpotifyUserID: account.SpotifyUserID,
LastListenedAt: sql.NullTime{
Valid: true,
Time: *listenedAt,
},
})
if err != nil {
return fmt.Errorf("updating listened at: %w", err)
}
}
if err := commit(ctx); err != nil {
return err
}
sp.log.Info("recorded listens for user",
slog.String("user_id", account.UserID),
slog.Int("count", len(played)),
)
return nil
}
func clientForSpotifyAccount(
ctx context.Context, account db.SpotifyAccount,
) *spotify.Client {
token := oauth2.Token(account.OauthToken)
httpClient := spotifyauth.New().Client(ctx, &token)
httpClient.Transport = otelhttp.NewTransport(httpClient.Transport)
client := spotify.New(httpClient)
return client
}