-
Notifications
You must be signed in to change notification settings - Fork 1
/
youtube.go
115 lines (94 loc) 路 2.68 KB
/
youtube.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package app
import (
"github.com/pkg/errors"
"net/url"
"time"
)
const (
CacheGetChannelPrefix = "youtube-channel-"
CacheGetChannelTtl = time.Hour * 24 * 31
CacheListChannelPrefix = "youtube-channels-q-"
CacheListChannelTtl = time.Hour * 24
)
type youTubeRepository interface {
ListEntry(string) ([]YouTubeFeedEntry, error)
}
type youTubeApiRepository interface {
GetChannel(string) (YouTubeChannel, error)
ListChannel(string) ([]YouTubeChannel, error)
}
type YouTubeFeedEntry struct {
ID string
Title string
Description string
Published time.Time
URL url.URL
Thumbnail YouTubeThumbnail
}
type YouTubeThumbnail struct {
Height int
Width int
Url url.URL
}
// YouTubeChannel holds metadata about channel
type YouTubeChannel struct {
Author string
AuthorEmail string
ChannelId string
Country string
Description string
PublishedAt time.Time
Thumbnail YouTubeThumbnail
Title string
Url string
Category string
}
type YouTubeService struct {
youTubeRepository youTubeRepository
youTubeApiRepository youTubeApiRepository
cache CacheService
}
func NewYouTubeService(ytRepo youTubeRepository, ytApiRepo youTubeApiRepository, cache CacheService) YouTubeService {
return YouTubeService{
youTubeRepository: ytRepo,
youTubeApiRepository: ytApiRepo,
cache: cache,
}
}
func (y YouTubeService) ListEntry(channelId string) ([]YouTubeFeedEntry, error) {
return y.youTubeRepository.ListEntry(channelId)
}
func (y YouTubeService) GetChannel(channelId string) (YouTubeChannel, error) {
return y.youTubeApiRepository.GetChannel(channelId)
}
func (y YouTubeService) ListChannel(query string) ([]YouTubeChannel, error) {
return y.youTubeApiRepository.ListChannel(query)
}
func (y YouTubeService) GetChannelCache(channelId string) (YouTubeChannel, error) {
var channel YouTubeChannel
cacheKey := CacheGetChannelPrefix + channelId
err := y.cache.Get(cacheKey, &channel)
if err == nil {
return channel, nil
}
response, err := y.GetChannel(channelId)
if err != nil {
return channel, errors.Wrapf(err, "unable value get channel for channel id: %s", channelId)
}
go y.cache.MarshalAndSet(cacheKey, response)
return response, nil
}
func (y YouTubeService) ListChannelCache(query string) ([]YouTubeChannel, error) {
var channels []YouTubeChannel
cacheKey := CacheListChannelPrefix + query
err := y.cache.Get(cacheKey, &channels)
if err == nil {
return channels, nil
}
response, err := y.ListChannel(query)
if err != nil {
return channels, errors.Wrapf(err, "unable value list channel for query: %s", query)
}
go y.cache.MarshalAndSet(cacheKey, response)
return response, nil
}