-
Notifications
You must be signed in to change notification settings - Fork 0
/
playlists.go
145 lines (138 loc) · 4.06 KB
/
playlists.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
package main
import (
"bufio"
"fmt"
"google.golang.org/api/youtube/v3"
"os"
"regexp"
"time"
)
func maybeCreatePlaylist(
service *youtube.Service, id string, title string, after string,
) (string, string, string) {
if 0 != len(id) {
listed := listPlaylists(service.Playlists.List([]string{"snippet"}).Id(id))
if 0 != len(listed) {
return id, listed[id].Title, after
}
}
result, err := service.Playlists.Insert(
[]string{"snippet", "status"},
&youtube.Playlist{
Snippet: &youtube.PlaylistSnippet{Title: title},
Status: &youtube.PlaylistStatus{PrivacyStatus: "private"},
},
).Do()
onError(err, fmt.Sprintf("Could not create playlist %s", title))
fmt.Printf("Created playlist %s: https://www.youtube.com/playlist?list=%s\n", result.Snippet.Title, result.Id)
return result.Id, result.Snippet.Title, after
}
func listPlaylists(request *youtube.PlaylistsListCall) map[string]Playlist {
request = request.MaxResults(50)
playlists := make(map[string]Playlist)
fetching := true
for fetching {
result, err := request.Do()
onError(err, fmt.Sprintf("Could not get playlists"))
for _, item := range result.Items {
playlist := Playlist{}.FromPlaylistSnippet(item)
playlists[playlist.Id] = playlist
}
if 0 != len(result.NextPageToken) {
request = request.PageToken(result.NextPageToken)
} else {
fetching = false
}
}
return playlists
}
func resolveSourcePlaylists(service *youtube.Service, playlists []Playlist) []Playlist {
var ids []string
for _, playlist := range playlists {
if 0 == len(playlist.Title) || playlist.Channel.Unresolved() {
ids = append(ids, playlist.Id)
}
}
if 0 == len(ids) {
return playlists
}
listed := listPlaylists(service.Playlists.List([]string{"snippet"}).Id(ids...))
resolved := make([]Playlist, 0, len(playlists))
for _, playlist := range playlists {
list, prs := listed[playlist.Id]
if prs {
resolved = append(resolved, list)
} else {
resolved = append(resolved, playlist)
}
}
return resolved
}
func zipPlaylist(
service *youtube.Service, playlist MergedPlaylist, exclude []*regexp.Regexp, verbose bool, noop bool,
) MergedPlaylist {
reader := bufio.NewReader(os.Stdin)
fmt.Println(playlist)
videos := determineVideosToAdd(service, playlist, exclude, verbose)
fmt.Println("Adding videos:")
if 0 == len(videos) {
fmt.Println("\t - None")
return playlist
}
total := len(videos)
for i, video := range videos {
fmt.Printf("\t - %s\n", video)
if noop {
fmt.Println("\t Noop run, not actually making request")
} else {
_, err := service.PlaylistItems.Insert(
[]string{"snippet"},
&youtube.PlaylistItem{
Snippet: &youtube.PlaylistItemSnippet{
PlaylistId: playlist.Id,
ResourceId: &youtube.ResourceId{
Kind: "youtube#video",
VideoId: video.Id,
},
},
},
).Do()
onError(err, fmt.Sprintf("Could not add video %s", video))
fmt.Printf("\t Done %d/%d\n", i+1, total)
}
if verbose {
_ = quittingInput(reader, "Continue? [y]")
}
}
if !noop && 0 != len(playlist.After) {
playlist.After = time.Now().Format(time.RFC3339)
}
return playlist
}
func zipPlaylists(
service *youtube.Service, playlists []MergedPlaylist, exclude []*regexp.Regexp, verbose bool, noop bool,
) []MergedPlaylist {
resolved := make([]MergedPlaylist, 0, len(playlists))
for _, playlist := range playlists {
merged := MergedPlaylist{}.WithDetails(
maybeCreatePlaylist(service, playlist.Id, playlist.Title, playlist.After),
).WithSources(
resolveSourcePlaylists(service, playlist.Sources),
)
merged = zipPlaylist(service, merged, exclude, verbose, noop)
resolved = append(resolved, merged)
}
return resolved
}
func resolvePlaylists(service *youtube.Service, playlists []MergedPlaylist) []MergedPlaylist {
resolved := make([]MergedPlaylist, 0, len(playlists))
for _, playlist := range playlists {
merged := MergedPlaylist{}.WithDetails(
maybeCreatePlaylist(service, playlist.Id, playlist.Title, playlist.After),
).WithSources(
resolveSourcePlaylists(service, playlist.Sources),
)
resolved = append(resolved, merged)
}
return resolved
}