-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
thumbnail.go
55 lines (44 loc) · 1.2 KB
/
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
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"google.golang.org/api/youtube/v3"
"net/http"
"os"
"pomu/s3"
)
// FindSuitableThumbnail returns the highest resolution thumbnail url which is available
func FindSuitableThumbnail(details *youtube.ThumbnailDetails) string {
qualities := []*youtube.Thumbnail{
details.Maxres,
details.High,
details.Medium,
details.Standard,
details.Default,
}
for _, thumbnail := range qualities {
if thumbnail != nil {
return thumbnail.Url
}
}
return ""
}
// SaveThumbnail saves a thumbnail to S3
func SaveThumbnail(id string, url string) (string, error) {
s3Client, err := s3.New(os.Getenv("S3_BUCKET"))
if err != nil {
log.Printf("[warn] failed to create s3 client in order to upload thumbnail: %s\n", err)
return url, err
}
response, err := http.Get(url)
if err != nil {
log.Printf("[warn] failed to get thumbnail from youtube: %s\n", err)
return url, err
}
defer response.Body.Close()
if err := s3Client.Upload(fmt.Sprintf("%s.jpg", id), response.Body, "image/jpeg"); err != nil {
log.Println("s3 thumbnail upload failed:", err)
return url, err
}
return fmt.Sprintf("%s/%s.jpg", os.Getenv("S3_DOWNLOAD_URL"), id), nil
}