forked from porjo/youtubeuploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.go
169 lines (138 loc) · 4.71 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
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
/*
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 main
import (
"fmt"
"net/http"
"strings"
"github.com/porjo/go-flowrate/flowrate"
"google.golang.org/api/youtube/v3"
)
const inputTimeLayout = "15:04"
type limitTransport struct {
rt http.RoundTripper
lr limitRange
reader *flowrate.Reader
filesize int64
}
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"`
// recording details
Location *youtube.GeoPoint `json:"location,omitempty"`
LocationDescription string `json:"locationDescription,omitempty"`
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 (t *limitTransport) RoundTrip(r *http.Request) (*http.Response, error) {
// Content-Type starts with 'multipart/related' where chunksize >= filesize (including chunksize 0)
// and 'video' for other chunksizes
if strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/related") ||
strings.HasPrefix(r.Header.Get("Content-Type"), "video") {
var monitor *flowrate.Monitor
if t.reader != nil {
monitor = t.reader.Monitor
}
// limit is set in limitChecker.Read
t.reader = flowrate.NewReader(r.Body, 0)
if monitor != nil {
// carry over stats to new limiter
t.reader.Monitor = monitor
} else {
t.reader.Monitor.SetTransferSize(t.filesize)
}
r.Body = &limitChecker{t.lr, t.reader}
}
return t.rt.RoundTrip(r)
}
func playlistList(service *youtube.Service, pageToken string) (*youtube.PlaylistListResponse, error) {
call := service.Playlists.List("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 '%s' 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("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 '%s': %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("snippet", playlistItem)
_, err = insertCall.Do()
if err != nil {
return err
}
fmt.Printf("Video added to playlist '%s' (%s)\n", playlist.Snippet.Title, playlist.Id)
return nil
}