Skip to content

Commit

Permalink
feat: provide simple endpoint for cache data for apple music
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Gleich <[email protected]>
  • Loading branch information
gleich committed Jan 3, 2025
1 parent d9fc06e commit dfa4aba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
51 changes: 39 additions & 12 deletions internal/apis/applemusic/applemusic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package applemusic

import (
"encoding/json"
"net/http"
"time"

"github.com/gleich/lcp-v2/internal/cache"
Expand All @@ -26,17 +28,17 @@ func cacheUpdate() (cacheData, error) {
"p.LV0PX3EIl0EpDLW", // jazz
"p.AWXoZoxHLrvpJlY", // chill
"p.V7VYVB0hZo53MQv", // old man
// "p.qQXLxPLtA75zg8e", // 80s
// "p.LV0PXNoCl0EpDLW", // divorced dad
// "p.AWXoXPYSLrvpJlY", // alt
// "p.QvDQE5RIVbAeokL", // PARTY
// "p.LV0PXL3Cl0EpDLW", // bops
// "p.6xZaArOsvzb5OML", // focus
// "p.O1kz7EoFVmvz704", // funk
// "p.qQXLxPpFA75zg8e", // RAHHHHHHHH
// "p.qQXLxpDuA75zg8e", // ROCK
// "p.O1kz7zbsVmvz704", // country
// "p.QvDQEN0IVbAeokL", // fall
"p.qQXLxPLtA75zg8e", // 80s
"p.LV0PXNoCl0EpDLW", // divorced dad
"p.AWXoXPYSLrvpJlY", // alt
"p.QvDQE5RIVbAeokL", // PARTY
"p.LV0PXL3Cl0EpDLW", // bops
"p.6xZaArOsvzb5OML", // focus
"p.O1kz7EoFVmvz704", // funk
"p.qQXLxPpFA75zg8e", // RAHHHHHHHH
"p.qQXLxpDuA75zg8e", // ROCK
"p.O1kz7zbsVmvz704", // country
"p.QvDQEN0IVbAeokL", // fall
// "p.ZOAXAMZF4KMD6ob", // sad girl music
// "p.QvDQEebsVbAeokL", // christmas
}
Expand All @@ -62,9 +64,34 @@ func Setup(router *chi.Mux) {
}

applemusicCache := cache.New("applemusic", data)
router.Get("/applemusic", applemusicCache.ServeHTTP())
router.Get("/applemusic", serveHTTP(applemusicCache))
router.Get("/applemusic/playlists/{id}", playlistEndpoint(applemusicCache))
router.Handle("/applemusic/ws", applemusicCache.ServeWS())
go applemusicCache.UpdatePeriodically(cacheUpdate, 30*time.Second)
lumber.Done("setup apple music cache")
}

func serveHTTP(c *cache.Cache[cacheData]) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
c.DataMutex.RLock()

data := struct {
PlaylistSummaries []playlistSummary `json:"playlist_summaries"`
RecentlyPlayed []song `json:"recently_played"`
}{}
for _, p := range c.Data.Playlists {
data.PlaylistSummaries = append(
data.PlaylistSummaries,
playlistSummary{Name: p.Name, ID: p.ID, TrackCount: len(p.Tracks)},
)
}

err := json.NewEncoder(w).Encode(data)
c.DataMutex.RUnlock()
if err != nil {
lumber.Error(err, "failed to write json data to request")
w.WriteHeader(http.StatusInternalServerError)
}
})
}
6 changes: 6 additions & 0 deletions internal/apis/applemusic/playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ import (
"github.com/go-chi/chi/v5"
)

type playlistSummary struct {
Name string `json:"name"`
TrackCount int `json:"track_count"`
ID string `json:"id"`
}

type playlist struct {
Name string `json:"name"`
Tracks []song `json:"tracks"`
Expand Down

0 comments on commit dfa4aba

Please sign in to comment.