Created
May 7, 2020 00:38
-
-
Save joewright/8aa5dcfd8465eea44d076d9a4af04183 to your computer and use it in GitHub Desktop.
Kaltura source file cleanup script
This file contains hidden or 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
/** | |
* This script will take a list of Kaltura Media Entities, | |
* check to see if they have more than 1 flavor asset, | |
* and remove the Source flavor asset if it exists. | |
* | |
* Prerequisites: | |
* Install NodeJS v12+ | |
* - Add this file to a folder | |
* - Add a file named "config.json" in the same folder, populate it with | |
* this: `{"session": "YOUR-VALID-KALTURA-SESSION-TOKEN"}` | |
* - Run `npm init` in that folder | |
* - Run `npm install xml2js request` | |
* - Edit the `ENTITY_IDs` to have the IDs you want to clean up | |
* - run `node kaltura-media-flavor-asset-cleanup.js` | |
* | |
*/ | |
const request = require('request'); | |
var xml2js = require('xml2js'); | |
const CONF = require('./config.json'); | |
const KALTURA_FLAVOR_ASSET_LIST = 'https://www.kaltura.com/api_v3/service/flavorasset/action/list'; | |
const KALTURA_FLAVOR_ASSET_GET = 'https://www.kaltura.com/api_v3/service/flavorasset/action/get'; | |
const KALTURA_FLAVOR_ASSET_DELETE = 'https://www.kaltura.com/api_v3/service/flavorasset/action/delete'; | |
const ENTITY_IDS = []; | |
const timerMsg = 'Deleted the source assets'; | |
console.time(timerMsg); | |
// start the script | |
run(); | |
async function run() { | |
// ensure we have a session | |
if (!CONF.session) { | |
throw new Error('Place a valid session value in a file named `config.json`'); | |
} | |
// for each entity in the list of entity IDs | |
try { | |
for (let id of ENTITY_IDS) { | |
// read the entity's flavors | |
logg(`Accessing entity ${id} flavors`); | |
const flavorData = await getFlavorsForEntityId(id); | |
let flavors = flavorData.xml.result.objects.item; | |
if (!Array.isArray(flavors)) { | |
flavors = [flavors]; | |
} | |
if (flavors.length === 1) { | |
logg(`Skipping entity with only 1 flavor asset`); | |
continue; | |
} | |
// for each flavor, check to see if it is the Source asset | |
for (let flavor of flavors) { | |
if (flavor.isOriginal === '1') { | |
// make the request to remove the Source asset | |
logg(`Deleting entity ${id} flavor asset ${flavor.id}`); | |
const flavorRes = await makeRequest(KALTURA_FLAVOR_ASSET_DELETE, { | |
method: 'POST', | |
form: { | |
ks: CONF.session, | |
id: flavor.id | |
} | |
}); | |
logg('Waiting 1 second before deleting the next media flavor asset'); | |
await sleep(1000); | |
} | |
} | |
} | |
} catch (err) { | |
console.error(err); | |
logg('Failed to remove all source flavor assets'); | |
} | |
console.timeEnd(timerMsg); | |
} | |
function sleep(n) { | |
return new Promise(resolve => { | |
setTimeout(resolve, n || 2000); | |
}); | |
} | |
async function getFlavorsForEntityId(entityId) { | |
const resp = await makeRequest(KALTURA_FLAVOR_ASSET_LIST, { | |
method: 'POST', | |
form: { | |
ks: CONF.session, | |
'filter[objectType]': 'KalturaFlavorAssetFilter', | |
'filter[entryIdEqual]': entityId | |
} | |
}); | |
var parser = new xml2js.Parser({ | |
explicitArray: false | |
}); | |
return await parser.parseStringPromise(resp.body); | |
} | |
function makeRequest(url, options) { | |
return new Promise((resolve, reject) => { | |
request(url, options, (err, res) => { | |
if (err) { | |
return reject(err); | |
} | |
resolve(res); | |
}); | |
}); | |
} | |
function logg(msg) { | |
console.log(`${new Date().toJSON()} ${msg}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment