diff options
Diffstat (limited to 'official-api.go')
| -rw-r--r-- | official-api.go | 54 |
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 { |
