-
Notifications
You must be signed in to change notification settings - Fork 0
/
stages.go
147 lines (138 loc) · 3.68 KB
/
stages.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
package youtubetoolkit
import (
"fmt"
"sort"
"sync"
"github.com/raffaelecassia/youtubetoolkit/bigg"
)
func (tk *Toolkit) channels2newsubscriptions(errors chan<- error, channelIds <-chan string) <-chan *bigg.Sub {
subs := make(chan *bigg.Sub)
go func() {
for channelId := range channelIds {
tk.logf("subscribing to %s... ", channelId)
sub, err := tk.service.SubscriptionInsert(channelId)
if err != nil {
errors <- fmt.Errorf("channel %s subscribe: %w", channelId, err)
tk.log("fail!")
} else {
subs <- sub
tk.log("channel", sub.Snippet.Title, "added")
}
}
close(subs)
}()
return subs
}
func (tk *Toolkit) channels2channelvideouploads(errors chan<- error, channelIds <-chan string, filter func(*bigg.PlaylistItem) (bool, error), numDigesters int) <-chan *bigg.PlaylistItem {
output := make(chan *bigg.PlaylistItem, 10)
var wg sync.WaitGroup
wg.Add(numDigesters)
for i := 0; i < numDigesters; i++ {
go func() {
for id := range channelIds {
tk.log("Checking channel", id)
c, err := tk.service.GetChannelInfo(id)
if err != nil {
errors <- err
} else {
plid := c.ContentDetails.RelatedPlaylists.Uploads
err := tk.service.PlaylistItemsList(plid, filter, output)
if err != nil {
errors <- err
}
}
}
wg.Done()
}()
}
go func() {
wg.Wait()
close(output)
}()
return output
}
func (tk *Toolkit) videos2playlist(errors chan<- error, playlistId string, videoIds <-chan string) <-chan *bigg.PlaylistItem {
output := make(chan *bigg.PlaylistItem)
go func() {
for id := range videoIds {
tk.log("Adding video", id)
pli, err := tk.service.PlaylistItemsInsert(playlistId, id)
if err != nil {
errors <- err
} else {
output <- pli
}
}
close(output)
}()
return output
}
func sortPlaylistItemByPublishedAt(input <-chan *bigg.PlaylistItem) <-chan *bigg.PlaylistItem {
output := make(chan *bigg.PlaylistItem, 10)
go func() {
items := []*bigg.PlaylistItem{}
// buffers all items
for i := range input {
items = append(items, i)
}
// sorts
sort.Slice(items, func(i, j int) bool {
return items[i].Snippet.PublishedAt < items[j].Snippet.PublishedAt
})
// sends items to chan
for _, pi := range items {
output <- pi
}
close(output)
}()
return output
}
func sub2item(input <-chan *bigg.Sub) <-chan Item {
output := make(chan Item, 10)
go func() {
for s := range input {
output <- &sub{
SubscriptionId: s.Id,
ChannelId: s.Snippet.ResourceId.ChannelId,
ChannelTitle: s.Snippet.Title,
ChannelUrl: fmt.Sprintf("https://www.youtube.com/channel/%s", s.Snippet.ResourceId.ChannelId),
ChannelThumbUrl: s.Snippet.Thumbnails.Default.Url,
}
}
close(output)
}()
return output
}
func playlist2item(input <-chan *bigg.Playlist) <-chan Item {
output := make(chan Item, 10)
go func() {
for i := range input {
output <- &playlist{
PlaylistId: i.Id,
PlaylistTitle: i.Snippet.Title,
VideoCount: i.ContentDetails.ItemCount,
}
}
close(output)
}()
return output
}
func playlistItem2item(input <-chan *bigg.PlaylistItem) <-chan Item {
output := make(chan Item, 10)
go func() {
for i := range input {
output <- &playlistItem{
PlaylistItemId: i.Id,
ChannelId: i.Snippet.VideoOwnerChannelId,
ChannelTitle: i.Snippet.VideoOwnerChannelTitle,
ChannelUrl: fmt.Sprintf("https://www.youtube.com/channel/%s", i.Snippet.VideoOwnerChannelId),
VideoId: i.Snippet.ResourceId.VideoId,
VideoTitle: i.Snippet.Title,
VideoUrl: fmt.Sprintf("https://www.youtube.com/watch?v=%s", i.Snippet.ResourceId.VideoId),
PublishedAt: i.Snippet.PublishedAt,
}
}
close(output)
}()
return output
}