-
Notifications
You must be signed in to change notification settings - Fork 98
/
http.go
130 lines (106 loc) · 3.7 KB
/
http.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
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package youtubeuploader
import (
"fmt"
"google.golang.org/api/youtube/v3"
)
type Playlistx struct {
Id string
Title string
PrivacyStatus string
}
type VideoMeta struct {
// snippet
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
CategoryId string `json:"categoryId,omitempty"`
Tags []string `json:"tags,omitempty"`
// status
PrivacyStatus string `json:"privacyStatus,omitempty"`
Embeddable bool `json:"embeddable,omitempty"`
License string `json:"license,omitempty"`
PublicStatsViewable bool `json:"publicStatsViewable,omitempty"`
PublishAt Date `json:"publishAt,omitempty"`
MadeForKids bool `json:"madeForKids,omitempty"`
// recording details
RecordingDate Date `json:"recordingDate,omitempty"`
// PlaylistID is deprecated in favour of PlaylistIDs
PlaylistID string `json:"playlistId,omitempty"`
PlaylistIDs []string `json:"playlistIds,omitempty"`
PlaylistTitles []string `json:"playlistTitles,omitempty"`
// BCP-47 language code e.g. 'en','es'
Language string `json:"language,omitempty"`
}
func playlistList(service *youtube.Service, pageToken string) (*youtube.PlaylistListResponse, error) {
call := service.Playlists.List([]string{"snippet", "contentDetails"})
call = call.Mine(true)
if pageToken != "" {
call = call.PageToken(pageToken)
}
response, err := call.Do()
if err != nil {
return nil, fmt.Errorf("error retrieving playlists: %s", err)
}
return response, nil
}
func (plx *Playlistx) AddVideoToPlaylist(service *youtube.Service, videoID string) error {
var playlist *youtube.Playlist
var err error
nextPageToken := ""
for {
// retrieve the next set of playlists
playlistResponse, err := playlistList(service, nextPageToken)
if err != nil {
return err
}
for _, pl := range playlistResponse.Items {
if pl.Id == plx.Id || pl.Snippet.Title == plx.Title {
playlist = pl
break
}
}
// retrieve the next page of results or exit the loop if done
nextPageToken = playlistResponse.NextPageToken
if nextPageToken == "" {
break
}
}
// create playlist if it doesn't exist
if playlist == nil {
if plx.Id != "" {
return fmt.Errorf("playlist ID %q doesn't exist", plx.Id)
}
playlist = &youtube.Playlist{}
playlist.Snippet = &youtube.PlaylistSnippet{Title: plx.Title}
playlist.Status = &youtube.PlaylistStatus{PrivacyStatus: plx.PrivacyStatus}
insertCall := service.Playlists.Insert([]string{"snippet", "status"}, playlist)
// API doesn't return playlist ID here!?
playlist, err = insertCall.Do()
if err != nil {
return fmt.Errorf("error creating playlist with title %q: %s", plx.Title, err)
}
}
playlistItem := &youtube.PlaylistItem{}
playlistItem.Snippet = &youtube.PlaylistItemSnippet{PlaylistId: playlist.Id, Title: playlist.Snippet.Title}
playlistItem.Snippet.ResourceId = &youtube.ResourceId{
VideoId: videoID,
Kind: "youtube#video",
}
insertCall := service.PlaylistItems.Insert([]string{"snippet"}, playlistItem)
_, err = insertCall.Do()
if err != nil {
return err
}
fmt.Printf("Video added to playlist %q (%s)\n", playlist.Snippet.Title, playlist.Id)
return nil
}