aboutsummaryrefslogtreecommitdiff
path: root/official-api.go
diff options
context:
space:
mode:
authorTim Keller <tjk@tjkeller.xyz>2026-05-17 00:29:39 -0500
committerTim Keller <tjk@tjkeller.xyz>2026-05-17 00:29:39 -0500
commitd4f8fc15b7e6a2204e8dda92a083684d93a5fa59 (patch)
tree74196a63e31a244170c2e35863e655af8b06d008 /official-api.go
parent1316aa7ca5e1668bbb7967264540bff3c8dbef86 (diff)
downloadembedtube-d4f8fc15b7e6a2204e8dda92a083684d93a5fa59.tar.xz
embedtube-d4f8fc15b7e6a2204e8dda92a083684d93a5fa59.zip
refactor some and client side support for new api + cleanup client code and fix some server bugs
Diffstat (limited to 'official-api.go')
-rw-r--r--official-api.go54
1 files changed, 33 insertions, 21 deletions
diff --git a/official-api.go b/official-api.go
index 39db1b8..f44a4b9 100644
--- a/official-api.go
+++ b/official-api.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
+ "strconv"
"time"
)
@@ -40,15 +41,16 @@ func apiRequest[T any](videoId string, endpoint string, videoIdParam string, par
type ytVideoDetails struct {
Items []struct {
Snippet struct {
- Channel string `json:"channel"`
- Description string `json:"description"`
- PublishedAt time.Time `json:"publishedAt"`
- Tags []string `json:"tags"`
+ ChannelTitle string `json:"channelTitle"`
+ ChannelId string `json:"channelId"`
+ Description string `json:"description"`
+ PublishedAt time.Time `json:"publishedAt"`
+ Tags []string `json:"tags"`
} `json:"snippet"`
Statistics struct {
- CommentCount int `json:"commentCount"`
- LikeCount int `json:"likeCount"`
- ViewCount int `json:"viewCount"`
+ CommentCount string `json:"commentCount"`
+ LikeCount string `json:"likeCount"`
+ ViewCount string `json:"viewCount"`
} `json:"statistics"`
} `json:"items"`
}
@@ -78,18 +80,28 @@ type ytComments struct {
type APISourceOfficial struct {}
func (a *APISourceOfficial) getDetails(videoId string) (VideoDetails, error) {
+ var zero VideoDetails
d, err := apiRequest[ytVideoDetails](videoId, "videos", "id", "snippet,statistics,topicDetails")
+ if err != nil {
+ return zero, err
+ }
+
+ if len(d.Items) == 0 {
+ return zero, fmt.Errorf("YouTube API returned no video details")
+ }
+
n := d.Items[0].Snippet
t := d.Items[0].Statistics
return VideoDetails{
- Channel: n.Channel,
- DatePublished: n.PublishedAt,
- Description: n.Description,
- NumComments: t.CommentCount,
- NumLikes: t.LikeCount,
- NumViews: t.ViewCount,
- Tags: n.Tags,
+ Author: n.ChannelTitle,
+ Channel: n.ChannelId,
+ Published: n.PublishedAt,
+ Description: n.Description,
+ Comments: AtoiOrZero(t.CommentCount),
+ Likes: AtoiOrZero(t.LikeCount),
+ Views: AtoiOrZero(t.ViewCount),
+ Tags: n.Tags,
}, err
}
@@ -102,18 +114,18 @@ func genComment(c ytComment, r []ytComment) Comment {
}
return Comment{
- Author: n.AuthorDisplayName,
- Body: n.TextDisplay,
- DatePublished: n.PublishedAt,
- DateUpdated: n.UpdatedAt,
- Likes: n.LikeCount,
- Replies: replies,
+ Author: n.AuthorDisplayName,
+ Body: n.TextDisplay,
+ Published: n.PublishedAt,
+ Updated: n.UpdatedAt,
+ Likes: n.LikeCount,
+ Replies: replies,
}
}
func (a *APISourceOfficial) getComments(videoId string) ([]Comment, error) {
const maxResults = 100
- d, err := apiRequest[ytComments](videoId, "commentThreads", "videoId", "snippet,replies&maxResults=" + fmt.Sprint(maxResults)) // TODO configure max results
+ d, err := apiRequest[ytComments](videoId, "commentThreads", "videoId", "snippet,replies&maxResults=" + strconv.Itoa(maxResults)) // TODO configure max results
var comments []Comment
for _, c := range d.Items {