Skip to content

Commit

Permalink
fix(plaxt): scrobble only if status changed
Browse files Browse the repository at this point in the history
  • Loading branch information
RoyXiang committed Mar 9, 2022
1 parent e28aaa2 commit 36315f1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
1 change: 1 addition & 0 deletions handler/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ const (
const (
sessionUnplayed sessionStatus = iota
sessionPlaying
sessionStopped
sessionWatched
)
70 changes: 39 additions & 31 deletions handler/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@ func (u *plexUser) UnmarshalBinary(data []byte) error {
return json.Unmarshal(data, u)
}

func (a sessionData) Equal(b sessionData) bool {
func (a sessionData) Check(b sessionData) (bool, bool) {
if a.status != b.status {
return true, true
}
if a.progress != b.progress {
return false
return true, false
}
if a.lastEvent != b.lastEvent {
return false
}
if a.status != b.status {
return false
return true, false
}
if len(a.guids) != len(b.guids) {
return false
return true, false
}
return true
return false, false
}

func (c *PlexClient) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -285,6 +285,9 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request, user *plexUser) {
return
}
session := c.sessions[sessionKey]
if session.status == sessionWatched {
return
}

progress := int(math.Round(float64(viewOffset) / float64(session.metadata.Duration) * 100.0))
if progress == 0 {
Expand All @@ -296,21 +299,6 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request, user *plexUser) {
}
}

serverIdentifier := c.getServerIdentifier()
if serverIdentifier == "" {
return
}
var section plex.Directory
sectionId := session.metadata.LibrarySectionID.String()
if c.getLibrarySection(sectionId) {
section = c.sections[sectionId]
if section.Type != "show" && section.Type != "movie" {
return
}
} else {
return
}

externalGuids := make([]plexhooks.ExternalGuid, 0)
if session.guids == nil {
metadata := c.getMetadata(ratingKey)
Expand Down Expand Up @@ -354,21 +342,41 @@ func (c *PlexClient) syncTimelineWithPlaxt(r *http.Request, user *plexUser) {
event = webhookEventStop
}
}
if event == "" || session.status == sessionWatched {
if event == "" {
return
} else if event == webhookEventScrobble {
session.status = sessionWatched
}
switch event {
case webhookEventPlay, webhookEventResume:
session.status = sessionPlaying
case webhookEventPause:
session.status = sessionStopped
case webhookEventStop:
session.status = sessionStopped
go clearCachedMetadata(ratingKey, user.Id)
} else if event == webhookEventStop {
case webhookEventScrobble:
session.status = sessionWatched
go clearCachedMetadata(ratingKey, user.Id)
} else if session.status == sessionUnplayed {
session.status = sessionPlaying
}

session.lastEvent = event
session.progress = progress
if !session.Equal(c.sessions[sessionKey]) {
if shouldUpdate, shouldScrobble := session.Check(c.sessions[sessionKey]); shouldUpdate {
c.sessions[sessionKey] = session
if !shouldScrobble {
return
}
}

serverIdentifier := c.getServerIdentifier()
if serverIdentifier == "" {
return
}
var section plex.Directory
sectionId := session.metadata.LibrarySectionID.String()
if c.getLibrarySection(sectionId) {
section = c.sections[sectionId]
if section.Type != "show" && section.Type != "movie" {
return
}
} else {
return
}
Expand Down

0 comments on commit 36315f1

Please sign in to comment.