Parent directory

is_short.go

813 bytes
 1package youtube
 2
 3import (
 4	"context"
 5	"fmt"
 6	"net/http"
 7)
 8
 9func (c *youTubeClient) isShort(ctx context.Context, videoID string) (bool, error) {
10	url := fmt.Sprintf("https://www.youtube.com/shorts/%s", videoID)
11	req, err := http.NewRequestWithContext(ctx, http.MethodHead, url, nil)
12	if err != nil {
13		c.log.Error(
14			"Failed to create request to check if a video is a short",
15			"videoID", videoID,
16			"error", err,
17		)
18		return false, err
19	}
20
21	client := &http.Client{
22		CheckRedirect: func(req *http.Request, via []*http.Request) error {
23			return http.ErrUseLastResponse
24		},
25	}
26	resp, err := client.Do(req)
27	if err != nil {
28		c.log.Error("Failed to check if a video is a short", "videoID", videoID, "error", err)
29		return false, err
30	}
31	defer resp.Body.Close()
32
33	return resp.StatusCode == http.StatusOK, nil
34}