Skip to content

Commit

Permalink
feat: streams support
Browse files Browse the repository at this point in the history
  • Loading branch information
oddyamill committed Sep 9, 2024
1 parent 61cf662 commit fd9bc32
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
23 changes: 13 additions & 10 deletions cmd/ytmusicrpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type updatePresenceBody struct {
Artist string `json:"artist"`
Artwork string `json:"artwork"`
Album string `json:"album"`
Current int64 `json:"current"`
End int64 `json:"end"`
Current *int64 `json:"current"`
End *int64 `json:"end"`
}

func updatePresence(w http.ResponseWriter, r *http.Request) {
Expand All @@ -69,24 +69,18 @@ func updatePresence(w http.ResponseWriter, r *http.Request) {
return
}

if body.Artist == "" || body.Artwork == "" || body.Current < 0 || body.End == 0 || body.Title == "" || body.TrackId == "" {
if body.Artist == "" || body.Artwork == "" || body.Title == "" || body.TrackId == "" {
http.Error(w, "400 invalid request body", http.StatusBadRequest)
return
}

timestamp := time.Now().UnixMilli()

activity := discord.Activity{
Type: activityType,
Details: body.Title,
State: body.Artist,
Assets: discord.Assets{
LargeImage: body.Artwork,
},
Timestamps: discord.Timestamps{
Start: timestamp - body.Current,
End: timestamp - body.Current + body.End,
},
Buttons: []discord.Button{
{
Label: "Слушать",
Expand All @@ -95,10 +89,19 @@ func updatePresence(w http.ResponseWriter, r *http.Request) {
},
}

if body.Album != "" && body.Title != body.Album {
if body.Album != "" && body.Title != body.Album && body.Artist != body.Album {
activity.Assets.LargeText = body.Album
}

if body.Current != nil && body.End != nil {
timestamp := time.Now().UnixMilli()

activity.Timestamps = &discord.Timestamps{
Start: timestamp - *body.Current,
End: timestamp - *body.Current + *body.End,
}
}

discord.UpdatePresence(activity)
fmt.Fprintf(w, "201 created")
}
Expand Down
14 changes: 7 additions & 7 deletions discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ type Args struct {
}

type Activity struct {
Type int `json:"type"`
Details string `json:"details"`
State string `json:"state"`
Assets Assets `json:"assets"`
Timestamps Timestamps `json:"timestamps"`
Buttons []Button `json:"buttons"`
Type int `json:"type"`
Details string `json:"details"`
State string `json:"state"`
Assets Assets `json:"assets"`
Timestamps *Timestamps `json:"timestamps,omitempty"`
Buttons []Button `json:"buttons"`
}

type Assets struct {
LargeImage string `json:"large_image"`
LargeText string `json:"large_text,omitempty"`
LargeText string `json:"large_text,omitempty"`
}

type Timestamps struct {
Expand Down
9 changes: 7 additions & 2 deletions userscript/ytmusicrpc.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,13 @@
navigator.mediaSession.metadata.album ||
[...document.querySelectorAll('.byline a')].at(-1)?.textContent ||
undefined,
current: parse(current),
end: parse(end),
current: null,
end: null,
}

if (end !== undefined) {
track.current = parse(current)
track.end = parse(end)
}

updatePresence(track)
Expand Down

0 comments on commit fd9bc32

Please sign in to comment.