-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change-Id: I825c92ae3520668fed369bc630e0bde8aaa0947d
- Loading branch information
1 parent
feff0b6
commit 7a54e95
Showing
4 changed files
with
338 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package earbug | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"time" | ||
|
||
"github.com/go-json-experiment/json" | ||
"github.com/zmb3/spotify/v2" | ||
"go.opentelemetry.io/otel/trace" | ||
"go.seankhliao.com/mono/cmd/moo/earbug/earbugv5" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func (a *App) FillAudioFeatures() error { | ||
wait := time.Minute | ||
for { | ||
time.Sleep(wait) | ||
wait = a.backfillAudioFeatures() | ||
} | ||
} | ||
|
||
func (a *App) backfillAudioFeatures() time.Duration { | ||
ctx := context.Background() | ||
ctx, span := a.o.T.Start(ctx, "audio features backfill") | ||
defer span.End() | ||
|
||
token := new(oauth2.Token) | ||
var backfillTrackIDs []spotify.ID | ||
a.o.Region(ctx, "check for backfill ids", func(ctx context.Context, span trace.Span) error { | ||
a.store.RDo(ctx, func(s *earbugv5.Store) { | ||
for _, t := range s.GetTracks() { | ||
if t.Features == nil { | ||
backfillTrackIDs = append(backfillTrackIDs, spotify.ID(t.GetId())) | ||
} | ||
if len(backfillTrackIDs) >= 100 { | ||
break | ||
} | ||
} | ||
if len(backfillTrackIDs) > 0 { | ||
for _, u := range s.Users { | ||
if rawToken := u.GetToken(); len(rawToken) > 0 { | ||
err := json.Unmarshal(rawToken, token) | ||
if err != nil { | ||
continue | ||
} | ||
break | ||
} | ||
} | ||
} | ||
}) | ||
return nil | ||
}) | ||
|
||
if len(backfillTrackIDs) == 0 { | ||
return time.Hour | ||
} | ||
|
||
err := a.o.Region(ctx, "backfill audio features", func(ctx context.Context, span trace.Span) error { | ||
if token == nil { | ||
return fmt.Errorf("no oauth2 token") | ||
} | ||
client := spotify.New(a.oauth2.Client(ctx, token)) | ||
|
||
trackFeatures, err := client.GetAudioFeatures(ctx, backfillTrackIDs...) | ||
if err != nil { | ||
return fmt.Errorf("get audio features: %w", err) | ||
} | ||
a.store.Do(ctx, func(s *earbugv5.Store) { | ||
for _, feat := range trackFeatures { | ||
track := s.Tracks[feat.ID.String()] | ||
track.Features = &earbugv5.AudioFeatures{ | ||
Acousticness: ptr(feat.Acousticness), | ||
Danceability: ptr(feat.Danceability), | ||
Energy: ptr(feat.Energy), | ||
Instrumentalness: ptr(feat.Instrumentalness), | ||
Key: ptr(int32(feat.Key)), | ||
Liveness: ptr(feat.Liveness), | ||
Loudness: ptr(feat.Loudness), | ||
Mode: ptr(int32(feat.Mode)), | ||
Speechiness: ptr(feat.Speechiness), | ||
Tempo: ptr(feat.Tempo), | ||
TimeSignature: ptr(int32(feat.TimeSignature)), | ||
Valence: ptr(feat.Valence), | ||
} | ||
s.Tracks[track.GetId()] = track | ||
} | ||
}) | ||
return nil | ||
}) | ||
if err != nil { | ||
a.o.Err(ctx, "backfill err", err) | ||
return 5 * time.Minute | ||
} | ||
|
||
a.o.L.LogAttrs(ctx, slog.LevelInfo, "backfill run", slog.Int("backfill.tracks", len(backfillTrackIDs))) | ||
if len(backfillTrackIDs) == 100 { | ||
return time.Minute | ||
} | ||
return time.Hour | ||
} |
Oops, something went wrong.