Skip to content

Commit

Permalink
Merge pull request #69 from marcus-crane/new-playback
Browse files Browse the repository at this point in the history
Introduce new playback system
  • Loading branch information
marcus-crane authored Aug 11, 2024
2 parents cb4e790 + 9bfbc2a commit e2d5c03
Show file tree
Hide file tree
Showing 40 changed files with 1,596 additions and 2,962 deletions.
135 changes: 123 additions & 12 deletions anilist/anilist.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,133 @@
package anilist

import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"time"
"os"
"strings"

"github.com/marcus-crane/gunslinger/db"
"github.com/marcus-crane/gunslinger/playback"
"github.com/marcus-crane/gunslinger/utils"
)

const (
anilistGraphqlEndpoint = "https://graphql.anilist.co"
)

type Client struct {
APIKey string
BaseURL string
HTTPClient *http.Client
type AnilistResponse struct {
Data AnilistData `json:"data"`
}

type AnilistData struct {
Page Page `json:"Page"`
}

type Page struct {
Activities []Activity `json:"activities"`
}

type Activity struct {
Id int64 `json:"id"`
Status string `json:"status"`
Progress string `json:"progress"`
CreatedAt int64 `json:"createdAt"`
Media Manga `json:"media"`
}

type Manga struct {
Id int64 `json:"id"`
Title MangaTitle `json:"title"`
Chapters int `json:"chapters"`
CoverImage MangaCover `json:"coverImage"`
}

type MangaTitle struct {
UserPreferred string `json:"userPreferred"`
}

type MangaCover struct {
ExtraLarge string `json:"extraLarge"`
}

func NewClient(apiKey string) *Client {
return &Client{
APIKey: apiKey,
BaseURL: "https://graphql.anilist.co",
HTTPClient: &http.Client{
Timeout: 10 * time.Second,
},
func GetRecentlyReadManga(ps *playback.PlaybackSystem, store db.Store, client http.Client) {
payload := strings.NewReader("{\"query\":\"query Test {\\n Page(page: 1, perPage: 10) {\\n activities(\\n\\t\\t\\tuserId: 6111545\\n type: MANGA_LIST\\n sort: ID_DESC\\n ) {\\n ... on ListActivity {\\n id\\n status\\n\\t\\t\\t\\tprogress\\n createdAt\\n media {\\n chapters\\n id\\n title {\\n userPreferred\\n }\\n coverImage {\\n extraLarge\\n }\\n }\\n }\\n }\\n }\\n}\\n\",\"variables\":{}}")
req, err := http.NewRequest("POST", anilistGraphqlEndpoint, payload)
if err != nil {
slog.Error("Failed to build Anilist manga payload", slog.String("stack", err.Error()))
return
}
req.Header = http.Header{
"Accept": []string{"application/json"},
"Authorization": []string{fmt.Sprintf("Bearer %s", os.Getenv("ANILIST_TOKEN"))},
"Content-Type": []string{"application/json"},
"User-Agent": []string{utils.UserAgent},
}
res, err := client.Do(req)
if err != nil {
slog.Error("Failed to contact Anilist for manga updates", slog.String("stack", err.Error()))
return
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
slog.Error("Failed to read Anilist response", slog.String("stack", err.Error()))
return
}
var anilistResponse AnilistResponse

if err = json.Unmarshal(body, &anilistResponse); err != nil {
slog.Error("Error fetching Anilist data", slog.String("stack", err.Error()))
return
}

if len(anilistResponse.Data.Page.Activities) == 0 {
slog.Warn("Found no activities for Anilist")
}

for _, activity := range anilistResponse.Data.Page.Activities {
image, extension, domColours, err := utils.ExtractImageContent(activity.Media.CoverImage.ExtraLarge)
if err != nil {
slog.Error("Failed to extract image content",
slog.String("stack", err.Error()),
slog.String("image_url", activity.Media.CoverImage.ExtraLarge),
)
return
}

discImage, _ := utils.BytesToGUIDLocation(image, extension)

update := playback.Update{
MediaItem: playback.MediaItem{
Title: activity.Progress,
Subtitle: activity.Media.Title.UserPreferred,
Category: string(playback.Manga),
Duration: 0,
Source: string(playback.Anilist),
Image: discImage,
DominantColours: domColours,
},
Elapsed: 0,
Status: playback.StatusStopped,
}

if err := ps.UpdatePlaybackState(update); err != nil {
slog.Error("Failed to save Anilist update",
slog.String("stack", err.Error()),
slog.String("title", update.MediaItem.Title))
}

hash := playback.GenerateMediaID(&update)
if err := utils.SaveCover(hash, image, extension); err != nil {
slog.Error("Failed to save cover for Anilist",
slog.String("stack", err.Error()),
slog.String("guid", hash),
slog.String("title", update.MediaItem.Title),
)
}
}
}
1 change: 0 additions & 1 deletion anilist/anilist_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions db/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func TestSqliteStore_GetRecent(t *testing.T) {
p := fakeSqliteStore(t, query, rows)
want := []models.ComboDBMediaItem{
{
ID: 1,
ID: "1",
Title: "blah",
},
{
ID: 2,
ID: "2",
Title: "bleh",
},
}
Expand All @@ -46,7 +46,7 @@ func TestSqliteStore_GetNewest(t *testing.T) {

p := fakeSqliteStore(t, query, rows)
want := models.ComboDBMediaItem{
ID: 1,
ID: "1",
Title: "blah",
}
got, err := p.GetNewest()
Expand Down
9 changes: 9 additions & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Business Rules

- Multiple sources can be active at the same time
- ie; playing a game while listening to music
- We treat paused items as being stopped but if playback is picked up, we mark the same instance as playing again (ie; podcasts listened to in chunks)
- It is exceedingly rare that I would ever use two of the same media type at the same time ie; listen to two songs at once, play two games at once.
- Different media types have different "priority" ie; a game and a song, the game is the main focus so would be highlighted as the main item with music as a background thing.
- Support more than 2 sources but only surface highest priority two
- Could be interesting to surface paused items if only one item active / recent ie; paused podcast slowly progressing
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ require (
github.com/jmoiron/sqlx v1.3.5
github.com/joho/godotenv v1.5.1
github.com/marekm4/color-extractor v1.2.1
github.com/mattn/go-sqlite3 v1.14.16
github.com/pressly/goose/v3 v3.16.0
github.com/r3labs/sse/v2 v2.10.0
github.com/rs/cors v1.10.1
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
google.golang.org/protobuf v1.31.0
modernc.org/sqlite v1.27.0
Expand All @@ -31,6 +33,7 @@ require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/devgianlu/shannon v0.0.0-20230613115856-82ec90b7fa7e // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
Expand All @@ -42,11 +45,11 @@ require (
github.com/lib/pq v1.10.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/xlab/vorbis-go v0.0.0-20210911202351-b5b85f1ec645 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand All @@ -58,6 +61,7 @@ require (
golang.org/x/sys v0.21.0 // indirect
golang.org/x/tools v0.22.0 // indirect
gopkg.in/cenkalti/backoff.v1 v1.1.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
modernc.org/cc/v3 v3.41.0 // indirect
modernc.org/ccgo/v3 v3.16.15 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo=
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
Expand Down Expand Up @@ -193,6 +195,7 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.1 h1:geMPLpDpQOgVyCg5z5GoRwLHepNdb71NXb67XFkP+Eg=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
Expand Down Expand Up @@ -308,6 +311,7 @@ gopkg.in/cenkalti/backoff.v1 v1.1.0 h1:Arh75ttbsvlpVA7WtVpH4u9h6Zl46xuptxqLxPiSo
gopkg.in/cenkalti/backoff.v1 v1.1.0/go.mod h1:J6Vskwqd+OMVJl8C33mmtxTBs2gyzfv7UDAkHu8BrjI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
38 changes: 38 additions & 0 deletions jobs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"net/http"
"time"

"github.com/go-co-op/gocron"
"github.com/marcus-crane/gunslinger/db"
"github.com/marcus-crane/gunslinger/playback"
"github.com/marcus-crane/gunslinger/plex"
"github.com/marcus-crane/gunslinger/spotify"
"github.com/marcus-crane/gunslinger/steam"
"github.com/marcus-crane/gunslinger/trakt"
"github.com/marcus-crane/gunslinger/utils"
)

var (
STORAGE_DIR = utils.GetEnv("STORAGE_DIR", "/tmp")
)

func SetupInBackground(ps *playback.PlaybackSystem, store db.Store) *gocron.Scheduler {
s := gocron.NewScheduler(time.UTC)

client := http.Client{}

go spotify.SetupSpotifyPoller(ps, store)

s.Every(1).Seconds().Do(plex.GetCurrentlyPlaying, ps, client)
// s.Every(15).Seconds().Do(anilist.GetRecentlyReadManga, ps, store, client) // Rate limit: 90 req/sec
s.Every(15).Seconds().Do(steam.GetCurrentlyPlaying, ps, client)
s.Every(15).Seconds().Do(trakt.GetCurrentlyPlaying, ps, client)
s.Every(15).Seconds().Do(trakt.GetCurrentlyListening, ps, client)

// If we're redeployed, we'll populate the latest state
ps.RefreshCurrentPlayback()

return s
}
Loading

0 comments on commit e2d5c03

Please sign in to comment.