0b5c237831a3f32c81c5cdfda9289ea98ee512a3
[motion2.git] / server / config / schedule / transcribe.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x2868=['results','transcripts','length','transcript','update','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','tempSentiment','Sequelize','Sentiment\x20Analysis\x20not\x20requested','sentimentAccountId','sentimentRegion','language','bucket','ValidationError','Sentiment\x20Analysis\x20not\x20configured','Transcript\x20not\x20available','CloudProvider','Cloud\x20Provider\x20not\x20available','data1','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','SentimentScore','Positive','Mixed','Neutral','FAILED','FailureReason','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','sentiment','findOne','name','error','inspect','VoiceRecording','findAll','IN_PROGRESS','Setting','Settings\x20not\x20available','transcribeAccountId','forEach','all','catch','SequelizeValidationError','start','lodash','request-promise','util','../../mysqldb','transcribe','TranscribeService','Comprehend','detectSentiment','substring','info','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','data2','transcribeRegion','transcribeName','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','TranscriptionJob','TranscriptionJobStatus','fileText','COMPLETED','Transcript','TranscriptFileUri','[RECORDING:%d]\x20Get\x20transcript','then','[RECORDING:%d]\x20Transcript\x20downloaded'];(function(_0x3dd15e,_0x1f0015){var _0x231fd0=function(_0x4f680a){while(--_0x4f680a){_0x3dd15e['push'](_0x3dd15e['shift']());}};_0x231fd0(++_0x1f0015);}(_0x2868,0x191));var _0x8286=function(_0x4d9d89,_0x412868){_0x4d9d89=_0x4d9d89-0x0;var _0x71f89d=_0x2868[_0x4d9d89];return _0x71f89d;};var _=require(_0x8286('0x0'));var AWS=require('aws-sdk');var rp=require(_0x8286('0x1'));var util=require(_0x8286('0x2'));var BPromise=require('bluebird');var db=require(_0x8286('0x3'))['db'];var logger=require('../../config/logger')(_0x8286('0x4'));function getTranscriptionJob(_0x596cd4,_0xf1a3b2,_0x17e43b,_0x55708b){return new Promise(function(_0xa1b0f9,_0x9a8dcd){var _0x2c9b6c=new AWS[(_0x8286('0x5'))]({'accessKeyId':_0xf1a3b2,'secretAccessKey':_0x596cd4,'region':_0x17e43b});_0x2c9b6c['getTranscriptionJob']({'TranscriptionJobName':_0x55708b},function(_0x267cf4,_0x2310b1){if(_0x267cf4){return _0x9a8dcd(_0x267cf4);}else{return _0xa1b0f9(_0x2310b1);}});});}function sentiment(_0x1f30ff,_0x3f45a7,_0x71c096,_0x1a6bcb,_0x3f6737){return new Promise(function(_0x55a92b,_0x3b2250){var _0x28d6c5=new AWS[(_0x8286('0x6'))]({'accessKeyId':_0x3f45a7,'secretAccessKey':_0x1f30ff,'region':_0x71c096});_0x28d6c5[_0x8286('0x7')]({'Text':_0x1a6bcb,'LanguageCode':_0x3f6737[_0x8286('0x8')](0x0,0x2)},function(_0x350b7b,_0x4685d6){if(_0x350b7b){return _0x3b2250(_0x350b7b);}else{return _0x55a92b(_0x4685d6);}});});}function checkTranscribeJob(_0x41e188,_0x276dce,_0x4af502){var _0x146e23;return new BPromise(function(_0x3a5ef7,_0x40d0cd){logger[_0x8286('0x9')](util[_0x8286('0xa')](_0x8286('0xb'),_0x41e188['id'],_0x41e188['transcribeName']));return getTranscriptionJob(_0x4af502['data1'],_0x4af502[_0x8286('0xc')],_0x276dce[_0x8286('0xd')],_0x41e188[_0x8286('0xe')])['then'](function(_0x543f45){logger[_0x8286('0x9')](util[_0x8286('0xa')](_0x8286('0xf'),_0x41e188['id'],_0x41e188[_0x8286('0xe')],_0x543f45[_0x8286('0x10')][_0x8286('0x11')]));if(!_0x41e188[_0x8286('0x12')]){switch(_0x543f45[_0x8286('0x10')][_0x8286('0x11')]){case _0x8286('0x13'):var _0x4ac509={'transcribeStatus':_0x543f45[_0x8286('0x10')][_0x8286('0x11')],'fileUri':_0x543f45[_0x8286('0x10')][_0x8286('0x14')][_0x8286('0x15')]};logger[_0x8286('0x9')](util[_0x8286('0xa')](_0x8286('0x16'),_0x41e188['id']));return rp({'uri':_0x543f45[_0x8286('0x10')][_0x8286('0x14')][_0x8286('0x15')],'json':!![]})[_0x8286('0x17')](function(_0x1618f6){logger['info'](util[_0x8286('0xa')](_0x8286('0x18'),_0x41e188['id']));if(_0x1618f6&&_0x1618f6[_0x8286('0x19')]&&_0x1618f6[_0x8286('0x19')][_0x8286('0x1a')]&&_0x1618f6[_0x8286('0x19')][_0x8286('0x1a')][_0x8286('0x1b')]){_0x4ac509[_0x8286('0x12')]='';for(var _0x302abe=0x0;_0x302abe<_0x1618f6[_0x8286('0x19')][_0x8286('0x1a')][_0x8286('0x1b')];_0x302abe++){_0x4ac509[_0x8286('0x12')]+=_0x1618f6['results'][_0x8286('0x1a')][_0x302abe][_0x8286('0x1c')]+'';}}return _0x41e188[_0x8286('0x1d')](_0x4ac509);})[_0x8286('0x17')](function(){logger['info'](util['format'](_0x8286('0x1e'),_0x41e188['id']));if(!_0x41e188[_0x8286('0x1f')]){throw new db[(_0x8286('0x20'))]['ValidationError'](_0x8286('0x21'));}if(!_0x276dce['sentiment']||!_0x276dce[_0x8286('0x22')]||!_0x276dce[_0x8286('0x23')]||!_0x276dce[_0x8286('0x24')]||!_0x276dce[_0x8286('0x25')]){throw new db['Sequelize'][(_0x8286('0x26'))](_0x8286('0x27'));}if(!_0x4ac509[_0x8286('0x12')]){throw new Error(_0x8286('0x28'));}if(_0x276dce['sentimentAccountId']===_0x4af502['id']){return _0x4af502;}else{return db[_0x8286('0x29')]['findOne']({'where':{'id':_settings['sentimentAccountId']}});}})['then'](function(_0x1c4987){if(!_0x1c4987){throw new db[(_0x8286('0x20'))][(_0x8286('0x26'))](_0x8286('0x2a'));}if(!_0x1c4987[_0x8286('0x2b')]||!_0x1c4987['data2']){throw new db[(_0x8286('0x20'))][(_0x8286('0x26'))](_0x8286('0x2c'));}_0x146e23=_0x1c4987;logger['info'](util[_0x8286('0xa')](_0x8286('0x2d'),_0x41e188['id']));return sentiment(_0x146e23[_0x8286('0x2b')],_0x146e23['data2'],_0x276dce['sentimentRegion'],_0x4ac509[_0x8286('0x12')],_0x276dce[_0x8286('0x24')]);})[_0x8286('0x17')](function(_0x1c375a){logger[_0x8286('0x9')](util[_0x8286('0xa')](_0x8286('0x2e'),_0x41e188['id']));return _0x41e188[_0x8286('0x1d')]({'sentiment':_0x1c375a[_0x8286('0x2f')],'sPositive':_0x1c375a[_0x8286('0x30')][_0x8286('0x31')],'sNegative':_0x1c375a[_0x8286('0x30')]['Negative'],'sMixed':_0x1c375a[_0x8286('0x30')][_0x8286('0x32')],'sNeutral':_0x1c375a[_0x8286('0x30')][_0x8286('0x33')]});})[_0x8286('0x17')](function(){logger[_0x8286('0x9')](util[_0x8286('0xa')](_0x8286('0x1e'),_0x41e188['id']));_0x3a5ef7();});case _0x8286('0x34'):return _0x41e188[_0x8286('0x1d')]({'transcribeStatus':_0x8286('0x34'),'failureReason':_0x543f45['TranscriptionJob'][_0x8286('0x35')]})[_0x8286('0x17')](function(){logger[_0x8286('0x9')](util[_0x8286('0xa')](_0x8286('0x1e'),_0x41e188['id']));_0x3a5ef7();});default:logger[_0x8286('0x9')](util['format'](_0x8286('0x36'),_0x41e188['id']));break;}}else{return BPromise['resolve']()[_0x8286('0x17')](function(){if(!_0x41e188['tempSentiment']){throw new db['Sequelize']['ValidationError']('Sentiment\x20Analysis\x20not\x20requested');}if(!_0x276dce[_0x8286('0x37')]||!_0x276dce[_0x8286('0x22')]||!_0x276dce['sentimentRegion']||!_0x276dce[_0x8286('0x24')]||!_0x276dce[_0x8286('0x25')]){throw new db['Sequelize']['ValidationError'](_0x8286('0x27'));}if(!_0x41e188[_0x8286('0x12')]){throw new Error(_0x8286('0x28'));}if(_0x276dce['sentimentAccountId']===_0x4af502['id']){return _0x4af502;}else{return db[_0x8286('0x29')][_0x8286('0x38')]({'where':{'id':_settings[_0x8286('0x22')]}});}})['then'](function(_0x3517b0){if(!_0x3517b0){throw new db[(_0x8286('0x20'))]['ValidationError'](_0x8286('0x2a'));}if(!_0x3517b0[_0x8286('0x2b')]||!_0x3517b0[_0x8286('0xc')]){throw new db[(_0x8286('0x20'))]['ValidationError'](_0x8286('0x2c'));}_0x146e23=_0x3517b0;logger[_0x8286('0x9')](util[_0x8286('0xa')](_0x8286('0x2d'),_0x41e188['id']));return sentiment(_0x146e23['data1'],_0x146e23[_0x8286('0xc')],_0x276dce[_0x8286('0x23')],_0x41e188[_0x8286('0x12')],_0x276dce[_0x8286('0x24')]);})[_0x8286('0x17')](function(_0x3e0809){logger[_0x8286('0x9')](util['format'](_0x8286('0x2e'),_0x41e188['id']));return _0x41e188[_0x8286('0x1d')]({'sentiment':_0x3e0809[_0x8286('0x2f')],'sPositive':_0x3e0809[_0x8286('0x30')][_0x8286('0x31')],'sNegative':_0x3e0809[_0x8286('0x30')]['Negative'],'sMixed':_0x3e0809[_0x8286('0x30')]['Mixed'],'sNeutral':_0x3e0809[_0x8286('0x30')][_0x8286('0x33')],'transcribeStatus':'COMPLETED'});})['then'](function(){logger[_0x8286('0x9')](util[_0x8286('0xa')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x41e188['id']));_0x3a5ef7();});}})['catch'](function(_0x35324f){if(_0x35324f){if(!_0x35324f['name']||_0x35324f[_0x8286('0x39')]!=='SequelizeValidationError'){logger[_0x8286('0x3a')](util[_0x8286('0xa')]('[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis',_0x41e188['id']),util[_0x8286('0x3b')](_0x35324f,{'showHidden':![],'depth':null}));}}_0x3a5ef7();});});}function checkTranscribe(){var _0x545b48;var _0x2ebbd8;var _0x2f0915;return db[_0x8286('0x3c')][_0x8286('0x3d')]({'where':{'transcribeStatus':_0x8286('0x3e')}})[_0x8286('0x17')](function(_0x5777ee){if(!_0x5777ee||!_0x5777ee[_0x8286('0x1b')]){throw new db[(_0x8286('0x20'))][(_0x8286('0x26'))]('No\x20transcribe\x20job\x20to\x20check');}_0x545b48=_0x5777ee;return db[_0x8286('0x3f')][_0x8286('0x38')]({'where':{'id':0x1}});})[_0x8286('0x17')](function(_0x2d1346){if(!_0x2d1346){throw new db[(_0x8286('0x20'))][(_0x8286('0x26'))](_0x8286('0x40'));}_0x2ebbd8=_0x2d1346;if(!_0x2ebbd8[_0x8286('0x4')]||!_0x2ebbd8[_0x8286('0x41')]||!_0x2ebbd8[_0x8286('0xd')]||!_0x2ebbd8[_0x8286('0x24')]||!_0x2ebbd8[_0x8286('0x25')]){throw new db[(_0x8286('0x20'))]['ValidationError']('Transcribe\x20not\x20configured');}return db['CloudProvider']['findOne']({'where':{'id':_0x2ebbd8[_0x8286('0x41')]}});})[_0x8286('0x17')](function(_0x5da58f){if(!_0x5da58f){throw new db[(_0x8286('0x20'))][(_0x8286('0x26'))](_0x8286('0x2a'));}if(!_0x5da58f[_0x8286('0x2b')]||!_0x5da58f['data2']){throw new db[(_0x8286('0x20'))][(_0x8286('0x26'))](_0x8286('0x2c'));}_0x2f0915=_0x5da58f;var _0x57e432=[];_0x545b48[_0x8286('0x42')](function(_0x2dc41d){_0x57e432['push'](checkTranscribeJob(_0x2dc41d,_0x2ebbd8,_0x2f0915));});return BPromise[_0x8286('0x43')](_0x57e432);})[_0x8286('0x44')](function(_0x5a3366){if(_0x5a3366){if(!_0x5a3366['name']||_0x5a3366[_0x8286('0x39')]!==_0x8286('0x45')){logger[_0x8286('0x3a')](util[_0x8286('0x3b')](_0x5a3366,{'showHidden':![],'depth':null}));}}});}exports[_0x8286('0x46')]=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};