-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (190 loc) · 5.63 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
func main() {
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
ctx := context.Background()
client := getClient(youtube.YoutubeScope)
service, err := youtube.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return fmt.Errorf("failed to create youtube service: %w", err)
}
/* FETCH PLAYLIST */
// if err := getPlaylistAndItems(service); err != nil {
// return err
// }
/* FETCH PLAYLIST */
/* SYNC PLAYLIST */
// playlistId := "PLeQFt2AXw9mSQpqcBfHkufqpBsS2x4hTD"
// videoIds := []string{"BdEe5SpdIuo", "aqlGlaNlcWE"}
// if _, err := syncPlaylist(ctx, service, playlistId, videoIds); err != nil {
// return fmt.Errorf("failed to sync playlist: %w", err)
// }
/* SYNC PLAYLIST */
/* CREATE PLAYLIST */
playlist, err := CreatePlaylist(ctx, service, "YamlTubeTest", "Some Description", "public")
if err != nil {
return fmt.Errorf("failed to create playlist: %w", err)
}
mustPrettyPrint(playlist.MarshalJSON)
/* CREATE PLAYLIST */
return nil
}
func getPlaylistAndItems(service *youtube.Service) error {
playlistsCall := service.Playlists.List([]string{"snippet,contentDetails"})
playlistsCall.Mine(true)
playlistsCall.MaxResults(100)
playlistsResp, err := playlistsCall.Do()
if err != nil {
return fmt.Errorf("faild to get playlists: %w", err)
}
mustPrettyPrint(playlistsResp.MarshalJSON)
// itemsListCall := service.PlaylistItems.List([]string{"snippet,contentDetails"})
// itemsListCall.PlaylistId("PLeQFt2AXw9mTdc30PLoUNfyqJPc3cCgnM")
// items, err := itemsListCall.Do()
// if err != nil {
// return fmt.Errorf("failed to get playlist items: %w", err)
// }
// mustPrettyPrint(items.MarshalJSON)
return nil
}
type PlaylistInsert struct {
VideoId string
Position int64
}
type PlaylistItemDelete struct {
ItemId string
}
type PlaylistDiffResult struct {
Inserts []PlaylistInsert
Deletes []PlaylistItemDelete
}
func diffPlaylist(wantIds []string, gotItems []*youtube.PlaylistItem) PlaylistDiffResult {
diff := PlaylistDiffResult{}
// if what we got is longer than what we want, remove everything that's after
// what we want
if len(gotItems) > len(wantIds) {
for i := len(wantIds); i < len(gotItems); i++ {
diff.Deletes = append(diff.Deletes, PlaylistItemDelete{
ItemId: gotItems[i].Id,
})
}
}
for i, wantVideoId := range wantIds {
// check if we're out of bounds
if len(gotItems) > i {
gotItem := gotItems[i]
// happy case, nothing to do
if wantVideoId == gotItem.ContentDetails.VideoId {
continue
}
// delete the playlistItem since it doesnt match
diff.Deletes = append(diff.Deletes, PlaylistItemDelete{
ItemId: gotItem.Id,
})
// we fall through to the insert here because
// we didn't find the playlistItem we were looking for,
// so we stil need to add it to the playlist
}
diff.Inserts = append(diff.Inserts, PlaylistInsert{
VideoId: wantVideoId,
Position: int64(i),
})
}
return diff
}
func syncPlaylist(ctx context.Context, service *youtube.Service, playlistId string, wantIds []string) (*PlaylistDiffResult, error) {
items, err := fetchPlaylistItems(ctx, service, playlistId)
if err != nil {
return nil, fmt.Errorf("failed to fetch playlist for sync: %w", err)
}
diff := diffPlaylist(wantIds, items)
for _, item := range diff.Deletes {
err := service.PlaylistItems.Delete(item.ItemId).
Context(ctx).
Do()
if err != nil {
return nil, fmt.Errorf("failed to delete playlistItem %q: %w", item, err)
}
}
for _, video := range diff.Inserts {
_, err := service.PlaylistItems.Insert([]string{"id,snippet"}, &youtube.PlaylistItem{
Snippet: &youtube.PlaylistItemSnippet{
PlaylistId: playlistId,
Position: video.Position,
ResourceId: &youtube.ResourceId{
Kind: "youtube#video",
VideoId: video.VideoId,
},
},
}).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("failed to insert playlistItem: %w", err)
}
}
return &diff, nil
}
func fetchPlaylistItems(ctx context.Context, service *youtube.Service, playlistId string) ([]*youtube.PlaylistItem, error) {
var pageToken string
var items []*youtube.PlaylistItem
firstLoop := true
for len(pageToken) > 0 || firstLoop {
itemsListCall := service.PlaylistItems.List([]string{"snippet,contentDetails"})
itemsListCall.PlaylistId(playlistId)
itemsListCall.Context(ctx)
itemsListCall.MaxResults(50)
itemsListCall.PageToken(pageToken)
// execute
listResp, err := itemsListCall.Do()
if err != nil {
return nil, fmt.Errorf("failed to fetch items: %w", err)
}
pageToken = listResp.NextPageToken
items = append(items, listResp.Items...)
firstLoop = false
}
return items, nil
}
func CreatePlaylist(ctx context.Context, service *youtube.Service, title, desc, visibility string) (*youtube.Playlist, error) {
insert := service.Playlists.Insert([]string{"snippet,status"}, &youtube.Playlist{
Snippet: &youtube.PlaylistSnippet{
Title: title,
Description: desc,
},
Status: &youtube.PlaylistStatus{
PrivacyStatus: visibility,
},
})
playlist, err := insert.Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("failed to create playlist: %w", err)
}
return playlist, nil
}
func mustPrettyPrint(f func() ([]byte, error)) {
data, err := f()
if err != nil {
panic(fmt.Errorf("failed to marshal data to json: %w", err))
}
buf := bytes.NewBuffer([]byte{})
json.Indent(buf, data, " ", " ")
fmt.Println(buf.String())
}
func listToMap[T comparable](list []T) map[T]struct{} {
m := make(map[T]struct{})
for _, v := range list {
m[v] = struct{}{}
}
return m
}