-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.go
114 lines (100 loc) · 2.59 KB
/
schedule.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
package pkg
import (
"fmt"
"time"
"github.com/araddon/dateparse"
"github.com/mniak/ytlive/internal"
"github.com/pkg/errors"
"google.golang.org/api/option"
"google.golang.org/api/youtube/v3"
)
type ScheduleRequest struct {
Title string
// Visibility string
Description string
// Category string
Date time.Time
MadeForKids bool
AutoStart bool
AutoStop bool
DVR bool
}
type ScheduleResponse struct {
ID string
Title string
Description string
Date time.Time
Link string
StreamKeyName string
StreamKey string
StreamURL string
}
func Schedule(options ScheduleRequest) (result ScheduleResponse, err error) {
config := internal.GetOAuthConfig()
ctx, tokenSource := internal.GetTokenSource(config)
svc, err := youtube.NewService(ctx, option.WithTokenSource(tokenSource))
if err != nil {
errors.Wrap(err, "could not create Youtube API client")
return
}
stream, err := svc.LiveStreams.Insert(
[]string{
"snippet",
"cdn",
"contentDetails",
},
&youtube.LiveStream{
Snippet: &youtube.LiveStreamSnippet{
Title: fmt.Sprintf("[%s] Generated Key (ytlive)", options.Date.Format("2006-01-02")),
},
Cdn: &youtube.CdnSettings{
FrameRate: "30fps",
IngestionType: "rtmp",
Resolution: "1080p",
},
ContentDetails: &youtube.LiveStreamContentDetails{
IsReusable: false,
},
},
).Do()
if err != nil {
err = errors.Wrap(err, "could not create a new stream")
return
}
result.StreamURL = stream.Cdn.IngestionInfo.IngestionAddress
result.StreamKey = stream.Cdn.IngestionInfo.StreamName
result.StreamKeyName = stream.Snippet.Title
broadcast, err := svc.LiveBroadcasts.Insert(
[]string{
"snippet",
"status",
"contentDetails",
},
&youtube.LiveBroadcast{
Snippet: &youtube.LiveBroadcastSnippet{
Title: options.Title,
Description: options.Description,
ScheduledStartTime: options.Date.Format(time.RFC3339),
},
Status: &youtube.LiveBroadcastStatus{
PrivacyStatus: "public",
SelfDeclaredMadeForKids: false,
},
ContentDetails: &youtube.LiveBroadcastContentDetails{
EnableAutoStart: options.AutoStart,
EnableAutoStop: options.AutoStop,
EnableDvr: options.DVR,
},
},
).Do()
if err != nil {
err = errors.Wrap(err, "could not create a new stream")
return
}
scheduledDate, _ := dateparse.ParseAny(broadcast.Snippet.ScheduledStartTime)
result.ID = broadcast.Id
result.Title = broadcast.Snippet.Title
result.Link = fmt.Sprintf("https://youtu.be/%s", broadcast.Id)
result.Date = scheduledDate
return
}