Last active
March 3, 2018 18:44
-
-
Save jordicenzano/89b175d0e0575016d9d4f7d091037859 to your computer and use it in GitHub Desktop.
Test Brightcove VideoCloud JAWS ingest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const https = require('https'); | |
if (process.argv.length !== 6) { | |
console.error("Use: ./jaws-ingest.js hls_asset_url client_id client_secret VC_account_id"); | |
return 1; | |
} | |
function httpReq (method, host, path, headers, body, callback) { | |
let answer_raw = []; | |
// Set up the request | |
const options = { | |
host: host, | |
path: path, | |
method: method, | |
headers: headers | |
}; | |
let req = https.request(options, function(response) { | |
response.setEncoding('utf8'); | |
response.on('data', function (chunk) { | |
answer_raw.push(chunk); | |
}); | |
response.on('end', function() { | |
if (response.statusCode < 300) { | |
return callback(null, JSON.parse(answer_raw.join())); | |
} | |
else {//error | |
return callback(response.statusCode + answer_raw.join(), null); | |
} | |
}); | |
}); | |
req.on('error', function(e) { | |
return callback(e, null); | |
}); | |
// post the data | |
req.write(body); | |
req.end(); | |
} | |
//Start execution | |
// Get VC OAUTH token | |
const create_bearer_token = { | |
method: 'POST', | |
host: 'oauth.brightcove.com', | |
path: '/v4/access_token' + '?' + 'grant_type=client_credentials', | |
headers: { | |
'Content-Type':'Content-Type: application/x-www-form-urlencoded', | |
'Authorization': "Basic " + new Buffer(process.argv[3] + ":" + process.argv[4]).toString('base64') | |
}, | |
body: '' | |
}; | |
httpReq(create_bearer_token.method,create_bearer_token.host,create_bearer_token.path,create_bearer_token.headers, create_bearer_token.body, function (err, data_oauth) { | |
if (err) { | |
console.error("Error in getting bearer token:" + err.toString()); | |
return 1; | |
} | |
console.info("Got access token info: " + JSON.stringify(data_oauth)); | |
// Create VC new asset | |
let create_vc_asset = { | |
method: 'POST', | |
host: 'cms.api.brightcove.com', | |
path: '/v1/accounts/' + process.argv[5] + '/videos', | |
headers: { | |
'Content-Type':'application/json', | |
'Authorization': 'Bearer ' + data_oauth.access_token | |
}, | |
body: { | |
name: "TestAsset" + new Date().toISOString() | |
} | |
}; | |
httpReq(create_vc_asset.method, create_vc_asset.host, create_vc_asset.path, create_vc_asset.headers, JSON.stringify(create_vc_asset.body), function(err, data_vc_asset) { | |
if (err) { | |
console.error("Error creating new asset:" + err.toString()); | |
return 1; | |
} | |
console.info("Created VC asset: " + JSON.stringify(data_vc_asset)); | |
// Create JAWS transmux ingest | |
let create_jaws_ingest = { | |
method: 'POST', | |
host: 'ingest.api.brightcove.com', | |
path: '/v1/accounts/' + process.argv[5] + '/videos/' + data_vc_asset.id + '/ingest_assets', | |
headers: { | |
'Content-Type':'application/json', | |
'Authorization': 'Bearer ' + data_oauth.access_token | |
}, | |
body: { | |
account_id: process.argv[5], | |
video_id: data_vc_asset.id, | |
manifest_url: process.argv[2] | |
} | |
}; | |
httpReq(create_jaws_ingest.method, create_jaws_ingest.host, create_jaws_ingest.path, create_jaws_ingest.headers, JSON.stringify(create_jaws_ingest.body), function(err, data_jaws_ingest) { | |
if (err) { | |
console.error("Error creating jaws ingest:" + err.toString()); | |
return 1; | |
} | |
console.info("Created jaws ingest: " + JSON.stringify(data_jaws_ingest)); | |
return 0; | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment