Skip to content

Instantly share code, notes, and snippets.

@kuu
Created June 5, 2018 13:58
Show Gist options
  • Save kuu/a94bd10f3c22775d9fedd9036bf4be6a to your computer and use it in GitHub Desktop.
Save kuu/a94bd10f3c22775d9fedd9036bf4be6a to your computer and use it in GitHub Desktop.
Get storage summary for each account
const OoyalaApi = require('ooyala-api');
const providers = [
{
name: 'Account-1',
key: 'API-KEY for Account-1',
secret: 'API-SECRET for Account-1'
},
{
name: 'Account-2',
key: 'API-KEY for Account-2',
secret: 'API-SECRET for Account-2'
},
{
name: 'Account-3',
key: 'API-KEY for Account-3',
secret: 'API-SECRET for Account-3' }
];
const waitList = [];
for (const {name, key, secret} of providers) {
waitList.push(report(name, new OoyalaApi(key, secret)));
}
Promise.all(waitList)
.then(results => {
for (const {provider, source, hls, hds, mp4} of results) {
console.log(`
--- ${provider} ---
Storage Usage (bytes):
Source: ${source}
HLS: ${hls}
HDS: ${hds}
MP4: ${mp4}
`);
}
console.log('Done');
});
function report(provider, api) {
const storage = {
provider,
hls: 0,
hds: 0,
mp4: 0,
source: 0
};
return api.get('/v2/assets', {where: `asset_type='video'`, limit: 500}, {recursive: true})
.then(assets => {
const promises = [];
for (const asset of assets) {
const promise = api.get(`/v2/assets/${asset.embed_code}/streams`, {})
.then(streams => {
for (const stream of streams) {
if (stream.is_source) {
storage.source += stream.file_size;
} else if (stream.muxing_format === 'TS') {
storage.hls += stream.file_size;
} else if (stream.muxing_format === 'F4F') {
storage.hds += stream.file_size;
} else if (stream.muxing_format === 'MP4') {
storage.mp4 += stream.file_size;
}
}
return null;
})
.catch(err => {
return err;
});
promises.push(promise);
}
return Promise.all(promises);
})
.then(results => {
for (const err of results) {
if (err) {
console.error(err);
}
}
return storage;
})
.catch(err => {
console.error(err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment