This repository was archived by the owner on Apr 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
113 lines (105 loc) · 3.26 KB
/
Copy pathindex.js
File metadata and controls
113 lines (105 loc) · 3.26 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
'use strict';
const minimist = require('minimist')
const bluebird = require('bluebird');
const request = require('request');
const _ = require('lodash');
const moment = require('moment');
const co = require('co');
// parse arguments
var argv = minimist(process.argv.slice(2));
if (!_.every(['token', 'channel', 'title', 'in', 'out'], _.partial(_.has, argv))) {
console.error('usage: node create-recording.js <args>');
console.error();
console.error('args (required):');
console.error(' token=<token>');
console.error(' channel=<channel slug> ex: stod2');
console.error(' title="<title>" ex: "The Simpsons"');
console.error(' in=<in timestamp> ex: "2016-04-05 19:25:25"');
console.error(' out=<out timestamp> ex: "2016-04-05 19:30:25"');
process.exit(1);
}
let coreClient = bluebird.promisifyAll(request.defaults({
baseUrl: 'https://core.oz.com/',
json: true,
headers: {
authorization: 'Bearer ' + argv.token
}
}));
function* slugToId (slug) {
let res = yield coreClient.getAsync({
url: '/channels/?slug=' + slug
});
if (res.statusCode !== 200) {
throw new Error('unable to map slug: ' + JSON.stringify(res.body));
} else if (res.body.data.length === 0) {
throw new Error('invalid channel: ' + slug);
}
let channelId = _.first(res.body.data).id;
console.log(`(1) mapping ${slug} to channelId: ${channelId}`);
return channelId;
}
function* createVideo (slug, channelId, title) {
const video = {
sourceType: 'stream',
contentType: 'movie',
title,
published: true,
playbackCountries: ['IS'],
// TODO: Add playableUntil
}
let res = yield coreClient.postAsync({
url: `/channels/${channelId}/videos`,
body: video
});
if (res.statusCode !== 201) {
throw new Error('unable to create video: ' + JSON.stringify(res.body));
}
let videoId = res.body.data.id;
console.log(`(2) video created, go edit the metadata here: https://creator.oz.com/${slug}/video/${videoId}`);
return videoId;
}
function* createSlot (channelId, videoId, inTime, outTime) {
const slot = {
channelId,
videoId,
type: 'regular',
startTime: inTime,
metadata: {
started: inTime,
ended: outTime
}
};
let res = yield coreClient.postAsync({
url: `/channels/${channelId}/slots`,
body: slot
});
let slotId = res.body.data.id;
if (res.statusCode !== 201) {
throw new Error('unable to create slot: ' + JSON.stringify(res.body));
}
console.log(`(3) recording created with id: ${slotId}`);
return slotId;
}
function* patchSlot (channelId, slotId) {
let res = yield coreClient.patchAsync({
url: `/channels/${channelId}/slots/${slotId}?vodify=true`
});
if (res.statusCode !== 200) {
throw new Error('unable to trigger recording: ' + JSON.stringify(res.body));
}
console.log(`(4) triggering record creation, done.`);
}
// main:
co(function* () {
try {
let channelId = yield slugToId(argv.channel);
let videoId = yield createVideo(argv.channel, channelId, argv.title);
let slotId = yield createSlot(channelId, videoId, argv.in, argv.out);
yield patchSlot(channelId, slotId);
} catch (err) {
console.error('Error hit when creating the recording!');
console.error(err);
// for debugging purposes:
// console.error(err.stack);
}
});