-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlist.go
98 lines (80 loc) · 2.41 KB
/
playlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package client
import (
"context"
"encoding/json"
_errors "errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/rl404/fairy/errors/stack"
"github.com/rl404/shimakaze/internal/errors"
)
type getPlaylistItemsResponse struct {
NextPageToken string `json:"nextPageToken"`
Items []struct {
ContentDetails struct {
VideoID string `json:"videoId"`
VideoPublishedAt time.Time `json:"videoPublishedAt"`
} `json:"contentDetails"`
} `json:"items"`
}
// GetVideoIDsByChannelID to get video ids by channel id.
func (c *Client) GetVideoIDsByChannelID(ctx context.Context, channelID string) ([]string, int, error) {
playlistID := c.channelIDToPlaylistID(channelID)
if playlistID == "" {
return nil, http.StatusNotFound, stack.Wrap(ctx, errors.ErrChannelNotFound)
}
url, _ := url.Parse(fmt.Sprintf("%s/playlistItems", c.host))
q := url.Query()
q.Add("playlistId", playlistID)
q.Add("part", "contentDetails")
q.Add("maxResults", "50")
// Loop until max age.
var res []string
var nextToken string
for {
q.Set("pageToken", nextToken)
url.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil)
if err != nil {
return nil, http.StatusInternalServerError, stack.Wrap(ctx, err, errors.ErrInternalServer)
}
resp, err := c.http.Do(req)
if err != nil {
return nil, http.StatusInternalServerError, stack.Wrap(ctx, err, errors.ErrInternalServer)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, resp.StatusCode, stack.Wrap(ctx, _errors.New(http.StatusText(resp.StatusCode)))
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, http.StatusInternalServerError, stack.Wrap(ctx, err, errors.ErrInternalServer)
}
var body getPlaylistItemsResponse
if err := json.Unmarshal(respBody, &body); err != nil {
return nil, http.StatusInternalServerError, stack.Wrap(ctx, err, errors.ErrInternalServer)
}
var done bool
for _, item := range body.Items {
if item.ContentDetails.VideoPublishedAt.Before(c.maxAge) {
done = true
break
}
res = append(res, item.ContentDetails.VideoID)
}
if len(body.Items) < 50 || body.NextPageToken == "" || done {
break
}
nextToken = body.NextPageToken
}
return res, http.StatusOK, nil
}
func (c *Client) channelIDToPlaylistID(channelID string) string {
if len(channelID) < 3 {
return ""
}
return "UU" + channelID[2:]
}