forked from porjo/youtubeuploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
141 lines (118 loc) · 3.82 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
package main
import (
"fmt"
"net/http"
"strings"
"github.com/porjo/go-flowrate/flowrate"
"google.golang.org/api/youtube/v3"
)
//
// Types
//
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"`
// JSON map
JSON map[string]interface{}
}
//
// Global constants
//
const inputTimeLayout = "15:04"
//
// Functions
//
func (t *limitTransport) RoundTrip(r *http.Request) (res *http.Response, err 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 (plx *Playlistx) AddVideoToPlaylist(service *youtube.Service, videoID string) (err error) {
listCall := service.Playlists.List([]string{"snippet", "contentDetails"})
listCall = listCall.Mine(true)
response, err := listCall.Do()
if err != nil {
return fmt.Errorf("Error retrieving playlists: %s", err)
}
var playlist *youtube.Playlist
for _, pl := range response.Items {
if pl.Id == plx.Id || pl.Snippet.Title == plx.Title {
playlist = pl
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([]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 '%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([]string{"snippet"}, playlistItem)
_, err = insertCall.Do()
if err != nil {
return err
}
logf("Video added to playlist '%s' (%s)\n", playlist.Snippet.Title, playlist.Id)
return nil
}