954fb2953b72e9fd633603b2090db916d10c2540
[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 _0x6bc9=['results','transcripts','length','transcript','update','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','sentiment','sentimentAccountId','sentimentRegion','language','bucket','Sequelize','ValidationError','Transcript\x20not\x20available','CloudProvider','findOne','Cloud\x20Provider\x20not\x20available','data1','data2','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','Positive','SentimentScore','Mixed','Neutral','FAILED','FailureReason','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','resolve','tempSentiment','Sentiment\x20Analysis\x20not\x20configured','Cloud\x20Provider\x20not\x20configured','Negative','catch','name','SequelizeValidationError','error','[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis','inspect','VoiceRecording','findAll','IN_PROGRESS','No\x20transcribe\x20job\x20to\x20check','Setting','transcribe','transcribeAccountId','push','all','start','lodash','request-promise','util','bluebird','../../mysqldb','../../config/logger','TranscribeService','getTranscriptionJob','Comprehend','detectSentiment','info','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','transcribeName','transcribeRegion','then','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','TranscriptionJob','TranscriptionJobStatus','fileText','COMPLETED','Transcript','TranscriptFileUri','[RECORDING:%d]\x20Get\x20transcript'];(function(_0x417774,_0x1b95e6){var _0x10015d=function(_0x2526b3){while(--_0x2526b3){_0x417774['push'](_0x417774['shift']());}};_0x10015d(++_0x1b95e6);}(_0x6bc9,0xc6));var _0x96bc=function(_0x29d623,_0x1a5c9c){_0x29d623=_0x29d623-0x0;var _0x242e59=_0x6bc9[_0x29d623];return _0x242e59;};var _=require(_0x96bc('0x0'));var AWS=require('aws-sdk');var rp=require(_0x96bc('0x1'));var util=require(_0x96bc('0x2'));var BPromise=require(_0x96bc('0x3'));var db=require(_0x96bc('0x4'))['db'];var logger=require(_0x96bc('0x5'))('transcribe');function getTranscriptionJob(_0x4e83b5,_0x375342,_0x3caf16,_0x266b46){return new Promise(function(_0x6b33,_0x21ea0c){var _0x1fb155=new AWS[(_0x96bc('0x6'))]({'accessKeyId':_0x375342,'secretAccessKey':_0x4e83b5,'region':_0x3caf16});_0x1fb155[_0x96bc('0x7')]({'TranscriptionJobName':_0x266b46},function(_0x44ce45,_0x39926){if(_0x44ce45){return _0x21ea0c(_0x44ce45);}else{return _0x6b33(_0x39926);}});});}function sentiment(_0x15bcf4,_0x1711d8,_0x176aa7,_0xd5786,_0x384cc1){return new Promise(function(_0x1b0b31,_0x427ea6){var _0x249ee5=new AWS[(_0x96bc('0x8'))]({'accessKeyId':_0x1711d8,'secretAccessKey':_0x15bcf4,'region':_0x176aa7});_0x249ee5[_0x96bc('0x9')]({'Text':_0xd5786,'LanguageCode':_0x384cc1['substring'](0x0,0x2)},function(_0x5a8d9e,_0x2911f1){if(_0x5a8d9e){return _0x427ea6(_0x5a8d9e);}else{return _0x1b0b31(_0x2911f1);}});});}function checkTranscribeJob(_0x2bd407,_0x4894be,_0x40cb3d){var _0x5dfe70;return new BPromise(function(_0x19b648,_0x5247fc){logger[_0x96bc('0xa')](util[_0x96bc('0xb')](_0x96bc('0xc'),_0x2bd407['id'],_0x2bd407[_0x96bc('0xd')]));return getTranscriptionJob(_0x40cb3d['data1'],_0x40cb3d['data2'],_0x4894be[_0x96bc('0xe')],_0x2bd407[_0x96bc('0xd')])[_0x96bc('0xf')](function(_0x3e650b){logger[_0x96bc('0xa')](util[_0x96bc('0xb')](_0x96bc('0x10'),_0x2bd407['id'],_0x2bd407[_0x96bc('0xd')],_0x3e650b[_0x96bc('0x11')][_0x96bc('0x12')]));if(!_0x2bd407[_0x96bc('0x13')]){switch(_0x3e650b['TranscriptionJob'][_0x96bc('0x12')]){case _0x96bc('0x14'):var _0x2ced21={'transcribeStatus':_0x3e650b[_0x96bc('0x11')]['TranscriptionJobStatus'],'fileUri':_0x3e650b[_0x96bc('0x11')][_0x96bc('0x15')][_0x96bc('0x16')]};logger[_0x96bc('0xa')](util[_0x96bc('0xb')](_0x96bc('0x17'),_0x2bd407['id']));return rp({'uri':_0x3e650b[_0x96bc('0x11')]['Transcript'][_0x96bc('0x16')],'json':!![]})[_0x96bc('0xf')](function(_0x4ef464){logger[_0x96bc('0xa')](util[_0x96bc('0xb')]('[RECORDING:%d]\x20Transcript\x20downloaded',_0x2bd407['id']));if(_0x4ef464&&_0x4ef464[_0x96bc('0x18')]&&_0x4ef464['results'][_0x96bc('0x19')]&&_0x4ef464[_0x96bc('0x18')][_0x96bc('0x19')][_0x96bc('0x1a')]){_0x2ced21['fileText']='';for(var _0x147dca=0x0;_0x147dca<_0x4ef464['results'][_0x96bc('0x19')][_0x96bc('0x1a')];_0x147dca++){_0x2ced21[_0x96bc('0x13')]+=_0x4ef464[_0x96bc('0x18')][_0x96bc('0x19')][_0x147dca][_0x96bc('0x1b')]+'';}}return _0x2bd407[_0x96bc('0x1c')](_0x2ced21);})[_0x96bc('0xf')](function(){logger[_0x96bc('0xa')](util[_0x96bc('0xb')](_0x96bc('0x1d'),_0x2bd407['id']));if(!_0x2bd407['tempSentiment']){throw new db['Sequelize']['ValidationError']('Sentiment\x20Analysis\x20not\x20requested');}if(!_0x4894be[_0x96bc('0x1e')]||!_0x4894be[_0x96bc('0x1f')]||!_0x4894be[_0x96bc('0x20')]||!_0x4894be[_0x96bc('0x21')]||!_0x4894be[_0x96bc('0x22')]){throw new db[(_0x96bc('0x23'))][(_0x96bc('0x24'))]('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x2ced21[_0x96bc('0x13')]){throw new Error(_0x96bc('0x25'));}if(_0x4894be[_0x96bc('0x1f')]===_0x40cb3d['id']){return _0x40cb3d;}else{return db[_0x96bc('0x26')][_0x96bc('0x27')]({'where':{'id':_settings[_0x96bc('0x1f')]}});}})['then'](function(_0x5a4473){if(!_0x5a4473){throw new db[(_0x96bc('0x23'))][(_0x96bc('0x24'))](_0x96bc('0x28'));}if(!_0x5a4473[_0x96bc('0x29')]||!_0x5a4473[_0x96bc('0x2a')]){throw new db[(_0x96bc('0x23'))][(_0x96bc('0x24'))]('Cloud\x20Provider\x20not\x20configured');}_0x5dfe70=_0x5a4473;logger[_0x96bc('0xa')](util[_0x96bc('0xb')](_0x96bc('0x2b'),_0x2bd407['id']));return sentiment(_0x5dfe70[_0x96bc('0x29')],_0x5dfe70['data2'],_0x4894be[_0x96bc('0x20')],_0x2ced21[_0x96bc('0x13')],_0x4894be[_0x96bc('0x21')]);})[_0x96bc('0xf')](function(_0x466aca){logger[_0x96bc('0xa')](util[_0x96bc('0xb')](_0x96bc('0x2c'),_0x2bd407['id']));return _0x2bd407[_0x96bc('0x1c')]({'sentiment':_0x466aca[_0x96bc('0x2d')],'sPositive':_0x466aca['SentimentScore'][_0x96bc('0x2e')],'sNegative':_0x466aca[_0x96bc('0x2f')]['Negative'],'sMixed':_0x466aca[_0x96bc('0x2f')][_0x96bc('0x30')],'sNeutral':_0x466aca[_0x96bc('0x2f')][_0x96bc('0x31')]});})['then'](function(){logger[_0x96bc('0xa')](util[_0x96bc('0xb')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x2bd407['id']));_0x19b648();});case _0x96bc('0x32'):return _0x2bd407[_0x96bc('0x1c')]({'transcribeStatus':_0x96bc('0x32'),'failureReason':_0x3e650b[_0x96bc('0x11')][_0x96bc('0x33')]})[_0x96bc('0xf')](function(){logger[_0x96bc('0xa')](util['format'](_0x96bc('0x1d'),_0x2bd407['id']));_0x19b648();});default:logger[_0x96bc('0xa')](util[_0x96bc('0xb')](_0x96bc('0x34'),_0x2bd407['id']));break;}}else{return BPromise[_0x96bc('0x35')]()[_0x96bc('0xf')](function(){if(!_0x2bd407[_0x96bc('0x36')]){throw new db[(_0x96bc('0x23'))][(_0x96bc('0x24'))]('Sentiment\x20Analysis\x20not\x20requested');}if(!_0x4894be[_0x96bc('0x1e')]||!_0x4894be[_0x96bc('0x1f')]||!_0x4894be[_0x96bc('0x20')]||!_0x4894be[_0x96bc('0x21')]||!_0x4894be[_0x96bc('0x22')]){throw new db[(_0x96bc('0x23'))][(_0x96bc('0x24'))](_0x96bc('0x37'));}if(!_0x2bd407[_0x96bc('0x13')]){throw new Error('Transcript\x20not\x20available');}if(_0x4894be[_0x96bc('0x1f')]===_0x40cb3d['id']){return _0x40cb3d;}else{return db[_0x96bc('0x26')]['findOne']({'where':{'id':_settings[_0x96bc('0x1f')]}});}})[_0x96bc('0xf')](function(_0x5dd254){if(!_0x5dd254){throw new db[(_0x96bc('0x23'))][(_0x96bc('0x24'))](_0x96bc('0x28'));}if(!_0x5dd254['data1']||!_0x5dd254[_0x96bc('0x2a')]){throw new db[(_0x96bc('0x23'))][(_0x96bc('0x24'))](_0x96bc('0x38'));}_0x5dfe70=_0x5dd254;logger[_0x96bc('0xa')](util['format'](_0x96bc('0x2b'),_0x2bd407['id']));return sentiment(_0x5dfe70[_0x96bc('0x29')],_0x5dfe70['data2'],_0x4894be[_0x96bc('0x20')],_0x2bd407[_0x96bc('0x13')],_0x4894be[_0x96bc('0x21')]);})[_0x96bc('0xf')](function(_0x1b79e2){logger['info'](util[_0x96bc('0xb')](_0x96bc('0x2c'),_0x2bd407['id']));return _0x2bd407[_0x96bc('0x1c')]({'sentiment':_0x1b79e2[_0x96bc('0x2d')],'sPositive':_0x1b79e2[_0x96bc('0x2f')][_0x96bc('0x2e')],'sNegative':_0x1b79e2[_0x96bc('0x2f')][_0x96bc('0x39')],'sMixed':_0x1b79e2[_0x96bc('0x2f')][_0x96bc('0x30')],'sNeutral':_0x1b79e2['SentimentScore'][_0x96bc('0x31')],'transcribeStatus':_0x96bc('0x14')});})[_0x96bc('0xf')](function(){logger[_0x96bc('0xa')](util[_0x96bc('0xb')](_0x96bc('0x1d'),_0x2bd407['id']));_0x19b648();});}})[_0x96bc('0x3a')](function(_0x25eb91){if(_0x25eb91){if(!_0x25eb91[_0x96bc('0x3b')]||_0x25eb91[_0x96bc('0x3b')]!==_0x96bc('0x3c')){logger[_0x96bc('0x3d')](util[_0x96bc('0xb')](_0x96bc('0x3e'),_0x2bd407['id']),util[_0x96bc('0x3f')](_0x25eb91,{'showHidden':![],'depth':null}));}}_0x19b648();});});}function checkTranscribe(){var _0x1af69a;var _0x201cfd;var _0x764518;return db[_0x96bc('0x40')][_0x96bc('0x41')]({'where':{'transcribeStatus':_0x96bc('0x42')}})[_0x96bc('0xf')](function(_0x128a69){if(!_0x128a69||!_0x128a69[_0x96bc('0x1a')]){throw new db['Sequelize']['ValidationError'](_0x96bc('0x43'));}_0x1af69a=_0x128a69;return db[_0x96bc('0x44')][_0x96bc('0x27')]({'where':{'id':0x1}});})['then'](function(_0x271dbf){if(!_0x271dbf){throw new db[(_0x96bc('0x23'))]['ValidationError']('Settings\x20not\x20available');}_0x201cfd=_0x271dbf;if(!_0x201cfd[_0x96bc('0x45')]||!_0x201cfd['transcribeAccountId']||!_0x201cfd[_0x96bc('0xe')]||!_0x201cfd[_0x96bc('0x21')]||!_0x201cfd[_0x96bc('0x22')]){throw new db[(_0x96bc('0x23'))][(_0x96bc('0x24'))]('Transcribe\x20not\x20configured');}return db[_0x96bc('0x26')][_0x96bc('0x27')]({'where':{'id':_0x201cfd[_0x96bc('0x46')]}});})['then'](function(_0x52f3a6){if(!_0x52f3a6){throw new db['Sequelize'][(_0x96bc('0x24'))](_0x96bc('0x28'));}if(!_0x52f3a6[_0x96bc('0x29')]||!_0x52f3a6[_0x96bc('0x2a')]){throw new db[(_0x96bc('0x23'))][(_0x96bc('0x24'))](_0x96bc('0x38'));}_0x764518=_0x52f3a6;var _0x4107ac=[];_0x1af69a['forEach'](function(_0x52b8b4){_0x4107ac[_0x96bc('0x47')](checkTranscribeJob(_0x52b8b4,_0x201cfd,_0x764518));});return BPromise[_0x96bc('0x48')](_0x4107ac);})['catch'](function(_0x119d08){if(_0x119d08){if(!_0x119d08[_0x96bc('0x3b')]||_0x119d08[_0x96bc('0x3b')]!==_0x96bc('0x3c')){logger['error'](util[_0x96bc('0x3f')](_0x119d08,{'showHidden':![],'depth':null}));}}});}exports[_0x96bc('0x49')]=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};