-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgoogleapi-routing.js
More file actions
executable file
·144 lines (129 loc) · 3.88 KB
/
Copy pathgoogleapi-routing.js
File metadata and controls
executable file
·144 lines (129 loc) · 3.88 KB
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
const {obsStartStreaming, obsStopStreaming} = require('./obs');
const {getWebcamStatus, wait} = require('./utils');
module.exports = (app, io, memStore, oauth2Client, youtubeClient, obs) => {
app.get('/auth_status', (req, res) => {
res.write(JSON.stringify({error: null, data: {status: memStore.authorized}}));
res.end();
})
app.post('/store_auth_code', async (req, res) => {
try {
memStore.code = req.body.code;
await oauth2Client.getToken(memStore.code);
res.write(JSON.stringify({error: null, data: null}));
res.end();
}
catch(error) {
res.write(JSON.stringify({error: 'Could not get tokens'}));
res.end();
}
});
app.get('/broadcasts', async (req, res) => {
try {
if (memStore.authorized) {
const liveBroadcasts = await youtubeClient.liveBroadcasts.list({
part: ['snippet,contentDetails,status'],
broadcastType: 'all',
mine: true
});
res.write(JSON.stringify({
error: null, data: {broadcasts: liveBroadcasts.data.items}
}, null, '\t'));
} else {
res.write(JSON.stringify({error: 'not authorized'}))
}
res.end();
}
catch(error) {
res.write(JSON.stringify({error: 'could not get list of broadcasts'}));
res.end();
}
});
app.get('/delete_broadcast', async (req, res) => {
try {
if (memStore.authorized) {
const id = req.query.id;
await youtubeClient.liveBroadcasts.delete({id});
res.end(`broadcast with id ${id} has been deleted`);
} else {
throw new Error('app is not authorized');
}
}
catch(error) {
res.end(error.message);
}
});
app.post('/start_livebroadcast', async (req, res) => {
try {
const liveBroadcast = await youtubeClient.liveBroadcasts.insert({
part: ['snippet,contentDetails,status'],
resource: {
snippet: {
title: 'BLOCKBERRYPI WEBCAM STREAM',
scheduledStartTime: new Date()
},
contentDetails: {
enableAutoStart: true,
enableAutoStop: true,
enableLowLatency: false,
latencyPreference: 'ultralow'
},
status: {
privacyStatus: 'public'
}
}
});
const liveBroadcastId = liveBroadcast.data.id;
memStore.liveBroadcastId = liveBroadcastId;
const streams = await youtubeClient.liveStreams.list({
part: ['snippet,cdn,contentDetails,status'],
mine: true
});
const liveStreamId = streams.data.items[0].id;
await youtubeClient.liveBroadcasts.bind({
id: liveBroadcastId,
part: ['snippet'],
streamId: liveStreamId
});
await obsStartStreaming(obs);
memStore.streaming = true;
await wait(15, io);
io.of('webcam').emit('webcamStatus', getWebcamStatus(memStore))
res.write(JSON.stringify({
error: null, data: null
}));
res.end();
}
catch(error) {
console.log('ERR: ', error);
res.write(JSON.stringify({error: 'Failed to start live stream'}));
res.end();
}
});
app.post('/stop_livebroadcast', async (req, res) => {
try {
if (memStore.liveBroadcast) {
await youtubeClient.liveBroadcasts.delete({
id: memStore.liveBroadcastId
});
res.write(JSON.stringify({
error: null, data: {
message: `Livebroadcast with id ${memStore.liveBroadcastId} deleted`
}
}));
memStore.liveBroadcastId = null;
} else {
res.write(JSON.stringify({
error: null, data: {
message: `Nothing to delete: id = null`
}
}));
}
res.end();
}
catch(error) {
console.log('ERR: ', error);
res.write(JSON.stringify({error: 'Failed to stop live stream'}));
res.end();
}
});
}