This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.go
174 lines (136 loc) · 3.03 KB
/
scraper.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package scraper
import (
"context"
"log"
"sync"
"time"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
type Scraper struct {
ctx context.Context
service *youtube.Service
}
func New(opts ...option.ClientOption) (*Scraper, error) {
ctx := context.Background()
s, err := youtube.NewService(ctx, opts...)
if err != nil {
return nil, err
}
scraper := &Scraper{ctx: ctx, service: s}
return scraper, nil
}
type searchOptions struct {
duration time.Duration
token string
until time.Time
}
func (s *Scraper) search(channelID string, opts *searchOptions) (*youtube.SearchListResponse, error) {
since := opts.until.Add(-opts.duration)
call := s.service.Search.
List([]string{"id"}).
ChannelId(channelID).
MaxResults(50).
Order("date").
PageToken(opts.token).
PublishedAfter(since.Format(time.RFC3339)).
PublishedBefore(opts.until.Format(time.RFC3339)).
SafeSearch("none").
Type("video")
res, err := call.Do()
if err != nil {
return nil, err
}
return res, nil
}
func (s *Scraper) getVideoList(ids []string) (*youtube.VideoListResponse, error) {
var strIds string
for i, id := range ids {
strIds += id
if i != len(ids)-1 {
strIds += ","
}
}
call := s.service.Videos.
List([]string{"contentDetails", "liveStreamingDetails", "snippet"}).
Id(strIds).
MaxResults(50)
res, err := call.Do()
if err != nil {
return nil, err
}
return res, nil
}
func (s *Scraper) scrape(channelID string, searchOpts *searchOptions) ([]*Video, string, error) {
log.Printf(`channel_id: "%s", published_before: "%s", page_token: "%s"`,
channelID, searchOpts.until.Format(time.RFC3339), searchOpts.token)
searchRes, err := s.search(channelID, searchOpts)
if err != nil {
return nil, "", err
}
var ids []string
for _, item := range searchRes.Items {
ids = append(ids, item.Id.VideoId)
}
res, err := s.getVideoList(ids)
if err != nil {
return nil, "", err
}
var results []*Video
var (
mux sync.Mutex
wg sync.WaitGroup
)
for _, item := range res.Items {
if item.Snippet.ChannelId == channelID {
wg.Add(1)
go func(item *youtube.Video) {
defer wg.Done()
v := NewVideo(item)
mux.Lock()
defer mux.Unlock()
results = append(results, v)
}(item)
}
}
wg.Wait()
return results, searchRes.NextPageToken, nil
}
type ScrapeOptions struct {
All bool
Until time.Time
}
func (s *Scraper) Scrape(channelID string, opts *ScrapeOptions) []*Video {
var (
days = 3 * 24 * time.Hour
date = opts.Until
token = ""
results []*Video
)
if opts.All {
days = 60 * 24 * time.Hour
}
for {
searchOpts := &searchOptions{
duration: days,
token: token,
until: date,
}
items, nextToken, err := s.scrape(channelID, searchOpts)
if err != nil {
log.Printf("error: %v", err)
break
}
for _, item := range items {
results = append(results, item)
}
if !opts.All || (token == "" && len(items) < 1) {
break
}
token = nextToken
if nextToken == "" {
date = date.Add(-days - 1*time.Second)
}
}
return results
}