-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_video_thumbnail.go
executable file
·58 lines (55 loc) · 1.22 KB
/
cache_video_thumbnail.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
package server
import (
"context"
"fmt"
"io"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/thavlik/t4vd/base/pkg/base"
"github.com/thavlik/t4vd/seer/pkg/thumbcache"
"go.uber.org/zap"
)
func cacheVideoThumbnail(
ctx context.Context,
videoID string,
thumbCache thumbcache.ThumbCache,
log *zap.Logger,
) error {
if has, err := thumbCache.Has(ctx, videoID); err != nil {
return errors.Wrap(err, "thumbcache.Has")
} else if has {
log.Debug("video thumbnail already cached")
return nil
}
start := time.Now()
url := fmt.Sprintf("https://i.ytimg.com/vi/%s/maxresdefault.jpg", videoID)
req, err := http.NewRequest(
http.MethodGet,
url,
nil,
)
if err != nil {
return err
}
req = req.WithContext(ctx)
resp, err := (&http.Client{
Timeout: 30 * time.Second,
}).Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotModified {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("status code %d: %s", resp.StatusCode, string(body))
}
if err := thumbCache.Set(
videoID,
resp.Body,
); err != nil {
return errors.Wrap(err, "thumbcache.Set")
}
log.Debug("cached video thumbnail", base.Elapsed(start))
return nil
}