fd7f42c196a2178c3d3e66ac7a909cd5a3acc9dd
[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 _0xb077=['transcribeName','data2','transcribeRegion','then','TranscriptionJob','TranscriptionJobStatus','fileText','COMPLETED','[RECORDING:%d]\x20Get\x20transcript','Transcript','TranscriptFileUri','[RECORDING:%d]\x20Transcript\x20downloaded','results','length','transcripts','update','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','tempSentiment','Sequelize','ValidationError','Sentiment\x20Analysis\x20not\x20requested','sentiment','sentimentAccountId','sentimentRegion','language','bucket','Transcript\x20not\x20available','CloudProvider','Cloud\x20Provider\x20not\x20available','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','SentimentScore','Positive','Negative','Mixed','Neutral','FAILED','FailureReason','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','Sentiment\x20Analysis\x20not\x20configured','findOne','catch','name','SequelizeValidationError','error','inspect','No\x20transcribe\x20job\x20to\x20check','Setting','Settings\x20not\x20available','transcribeAccountId','forEach','push','all','start','lodash','util','../../mysqldb','../../config/logger','transcribe','Comprehend','substring','info','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)'];(function(_0x9fb10d,_0xa7970e){var _0x54f389=function(_0x344a38){while(--_0x344a38){_0x9fb10d['push'](_0x9fb10d['shift']());}};_0x54f389(++_0xa7970e);}(_0xb077,0x7a));var _0x7b07=function(_0x331cf8,_0x29c108){_0x331cf8=_0x331cf8-0x0;var _0x5b0bc6=_0xb077[_0x331cf8];return _0x5b0bc6;};var _=require(_0x7b07('0x0'));var AWS=require('aws-sdk');var rp=require('request-promise');var util=require(_0x7b07('0x1'));var BPromise=require('bluebird');var db=require(_0x7b07('0x2'))['db'];var logger=require(_0x7b07('0x3'))(_0x7b07('0x4'));function getTranscriptionJob(_0x2b0c6c,_0x423957,_0x5df1bf,_0x5722bb){return new Promise(function(_0xdc333,_0x325d95){var _0x110ee5=new AWS['TranscribeService']({'accessKeyId':_0x423957,'secretAccessKey':_0x2b0c6c,'region':_0x5df1bf});_0x110ee5['getTranscriptionJob']({'TranscriptionJobName':_0x5722bb},function(_0x3a82e8,_0x4b6f20){if(_0x3a82e8){return _0x325d95(_0x3a82e8);}else{return _0xdc333(_0x4b6f20);}});});}function sentiment(_0x3dd0ad,_0x292db1,_0x2d3741,_0x214052,_0x1e8092){return new Promise(function(_0x23522e,_0x2f7009){var _0x33a461=new AWS[(_0x7b07('0x5'))]({'accessKeyId':_0x292db1,'secretAccessKey':_0x3dd0ad,'region':_0x2d3741});_0x33a461['detectSentiment']({'Text':_0x214052,'LanguageCode':_0x1e8092[_0x7b07('0x6')](0x0,0x2)},function(_0x282fe2,_0x1ffc7c){if(_0x282fe2){return _0x2f7009(_0x282fe2);}else{return _0x23522e(_0x1ffc7c);}});});}function checkTranscribeJob(_0x5684db,_0xe825c4,_0x3d73c6){var _0x9f3404;return new BPromise(function(_0x7930f1,_0x530d1d){logger[_0x7b07('0x7')](util[_0x7b07('0x8')](_0x7b07('0x9'),_0x5684db['id'],_0x5684db[_0x7b07('0xa')]));return getTranscriptionJob(_0x3d73c6['data1'],_0x3d73c6[_0x7b07('0xb')],_0xe825c4[_0x7b07('0xc')],_0x5684db[_0x7b07('0xa')])[_0x7b07('0xd')](function(_0x4b7d85){logger['info'](util['format']('[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s',_0x5684db['id'],_0x5684db[_0x7b07('0xa')],_0x4b7d85[_0x7b07('0xe')][_0x7b07('0xf')]));if(!_0x5684db[_0x7b07('0x10')]){switch(_0x4b7d85[_0x7b07('0xe')][_0x7b07('0xf')]){case _0x7b07('0x11'):var _0x5141d7={'transcribeStatus':_0x4b7d85[_0x7b07('0xe')][_0x7b07('0xf')],'fileUri':_0x4b7d85['TranscriptionJob']['Transcript']['TranscriptFileUri']};logger[_0x7b07('0x7')](util[_0x7b07('0x8')](_0x7b07('0x12'),_0x5684db['id']));return rp({'uri':_0x4b7d85[_0x7b07('0xe')][_0x7b07('0x13')][_0x7b07('0x14')],'json':!![]})[_0x7b07('0xd')](function(_0x481812){logger[_0x7b07('0x7')](util[_0x7b07('0x8')](_0x7b07('0x15'),_0x5684db['id']));if(_0x481812&&_0x481812['results']&&_0x481812[_0x7b07('0x16')]['transcripts']&&_0x481812[_0x7b07('0x16')]['transcripts'][_0x7b07('0x17')]){_0x5141d7[_0x7b07('0x10')]='';for(var _0x23f830=0x0;_0x23f830<_0x481812[_0x7b07('0x16')]['transcripts'][_0x7b07('0x17')];_0x23f830++){_0x5141d7['fileText']+=_0x481812[_0x7b07('0x16')][_0x7b07('0x18')][_0x23f830]['transcript']+'';}}return _0x5684db[_0x7b07('0x19')](_0x5141d7);})[_0x7b07('0xd')](function(){logger[_0x7b07('0x7')](util[_0x7b07('0x8')](_0x7b07('0x1a'),_0x5684db['id']));if(!_0x5684db[_0x7b07('0x1b')]){throw new db[(_0x7b07('0x1c'))][(_0x7b07('0x1d'))](_0x7b07('0x1e'));}if(!_0xe825c4[_0x7b07('0x1f')]||!_0xe825c4[_0x7b07('0x20')]||!_0xe825c4[_0x7b07('0x21')]||!_0xe825c4[_0x7b07('0x22')]||!_0xe825c4[_0x7b07('0x23')]){throw new db[(_0x7b07('0x1c'))][(_0x7b07('0x1d'))]('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x5141d7['fileText']){throw new Error(_0x7b07('0x24'));}if(_0xe825c4[_0x7b07('0x20')]===_0x3d73c6['id']){return _0x3d73c6;}else{return db[_0x7b07('0x25')]['findOne']({'where':{'id':_settings[_0x7b07('0x20')]}});}})[_0x7b07('0xd')](function(_0x15713b){if(!_0x15713b){throw new db[(_0x7b07('0x1c'))]['ValidationError'](_0x7b07('0x26'));}if(!_0x15713b['data1']||!_0x15713b[_0x7b07('0xb')]){throw new db['Sequelize'][(_0x7b07('0x1d'))](_0x7b07('0x27'));}_0x9f3404=_0x15713b;logger['info'](util['format'](_0x7b07('0x28'),_0x5684db['id']));return sentiment(_0x9f3404['data1'],_0x9f3404[_0x7b07('0xb')],_0xe825c4[_0x7b07('0x21')],_0x5141d7['fileText'],_0xe825c4[_0x7b07('0x22')]);})['then'](function(_0x661e89){logger[_0x7b07('0x7')](util[_0x7b07('0x8')](_0x7b07('0x29'),_0x5684db['id']));return _0x5684db[_0x7b07('0x19')]({'sentiment':_0x661e89[_0x7b07('0x2a')],'sPositive':_0x661e89[_0x7b07('0x2b')][_0x7b07('0x2c')],'sNegative':_0x661e89[_0x7b07('0x2b')][_0x7b07('0x2d')],'sMixed':_0x661e89[_0x7b07('0x2b')][_0x7b07('0x2e')],'sNeutral':_0x661e89[_0x7b07('0x2b')][_0x7b07('0x2f')]});})[_0x7b07('0xd')](function(){logger[_0x7b07('0x7')](util[_0x7b07('0x8')](_0x7b07('0x1a'),_0x5684db['id']));_0x7930f1();});case _0x7b07('0x30'):return _0x5684db[_0x7b07('0x19')]({'transcribeStatus':'FAILED','failureReason':_0x4b7d85[_0x7b07('0xe')][_0x7b07('0x31')]})[_0x7b07('0xd')](function(){logger[_0x7b07('0x7')](util[_0x7b07('0x8')](_0x7b07('0x1a'),_0x5684db['id']));_0x7930f1();});default:logger[_0x7b07('0x7')](util[_0x7b07('0x8')](_0x7b07('0x32'),_0x5684db['id']));break;}}else{return BPromise['resolve']()[_0x7b07('0xd')](function(){if(!_0x5684db[_0x7b07('0x1b')]){throw new db['Sequelize'][(_0x7b07('0x1d'))](_0x7b07('0x1e'));}if(!_0xe825c4['sentiment']||!_0xe825c4[_0x7b07('0x20')]||!_0xe825c4[_0x7b07('0x21')]||!_0xe825c4[_0x7b07('0x22')]||!_0xe825c4[_0x7b07('0x23')]){throw new db[(_0x7b07('0x1c'))]['ValidationError'](_0x7b07('0x33'));}if(!_0x5684db[_0x7b07('0x10')]){throw new Error(_0x7b07('0x24'));}if(_0xe825c4[_0x7b07('0x20')]===_0x3d73c6['id']){return _0x3d73c6;}else{return db[_0x7b07('0x25')][_0x7b07('0x34')]({'where':{'id':_settings[_0x7b07('0x20')]}});}})[_0x7b07('0xd')](function(_0x523355){if(!_0x523355){throw new db[(_0x7b07('0x1c'))][(_0x7b07('0x1d'))](_0x7b07('0x26'));}if(!_0x523355['data1']||!_0x523355[_0x7b07('0xb')]){throw new db[(_0x7b07('0x1c'))][(_0x7b07('0x1d'))]('Cloud\x20Provider\x20not\x20configured');}_0x9f3404=_0x523355;logger[_0x7b07('0x7')](util[_0x7b07('0x8')](_0x7b07('0x28'),_0x5684db['id']));return sentiment(_0x9f3404['data1'],_0x9f3404[_0x7b07('0xb')],_0xe825c4['sentimentRegion'],_0x5684db[_0x7b07('0x10')],_0xe825c4[_0x7b07('0x22')]);})[_0x7b07('0xd')](function(_0x55a96d){logger[_0x7b07('0x7')](util[_0x7b07('0x8')]('[RECORDING:%d]\x20Sentiment\x20Analysis\x20available',_0x5684db['id']));return _0x5684db['update']({'sentiment':_0x55a96d[_0x7b07('0x2a')],'sPositive':_0x55a96d['SentimentScore'][_0x7b07('0x2c')],'sNegative':_0x55a96d[_0x7b07('0x2b')]['Negative'],'sMixed':_0x55a96d['SentimentScore'][_0x7b07('0x2e')],'sNeutral':_0x55a96d[_0x7b07('0x2b')]['Neutral'],'transcribeStatus':_0x7b07('0x11')});})[_0x7b07('0xd')](function(){logger[_0x7b07('0x7')](util[_0x7b07('0x8')](_0x7b07('0x1a'),_0x5684db['id']));_0x7930f1();});}})[_0x7b07('0x35')](function(_0xddd3b7){if(_0xddd3b7){if(!_0xddd3b7[_0x7b07('0x36')]||_0xddd3b7[_0x7b07('0x36')]!==_0x7b07('0x37')){logger[_0x7b07('0x38')](util[_0x7b07('0x8')]('[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis',_0x5684db['id']),util[_0x7b07('0x39')](_0xddd3b7,{'showHidden':![],'depth':null}));}}_0x7930f1();});});}function checkTranscribe(){var _0x30ec1b;var _0x3e5593;var _0x4fc266;return db['VoiceRecording']['findAll']({'where':{'transcribeStatus':'IN_PROGRESS'}})[_0x7b07('0xd')](function(_0x2ed908){if(!_0x2ed908||!_0x2ed908[_0x7b07('0x17')]){throw new db[(_0x7b07('0x1c'))][(_0x7b07('0x1d'))](_0x7b07('0x3a'));}_0x30ec1b=_0x2ed908;return db[_0x7b07('0x3b')][_0x7b07('0x34')]({'where':{'id':0x1}});})['then'](function(_0x2c2605){if(!_0x2c2605){throw new db[(_0x7b07('0x1c'))][(_0x7b07('0x1d'))](_0x7b07('0x3c'));}_0x3e5593=_0x2c2605;if(!_0x3e5593[_0x7b07('0x4')]||!_0x3e5593['transcribeAccountId']||!_0x3e5593[_0x7b07('0xc')]||!_0x3e5593[_0x7b07('0x22')]||!_0x3e5593[_0x7b07('0x23')]){throw new db[(_0x7b07('0x1c'))][(_0x7b07('0x1d'))]('Transcribe\x20not\x20configured');}return db[_0x7b07('0x25')][_0x7b07('0x34')]({'where':{'id':_0x3e5593[_0x7b07('0x3d')]}});})['then'](function(_0x53777d){if(!_0x53777d){throw new db['Sequelize'][(_0x7b07('0x1d'))](_0x7b07('0x26'));}if(!_0x53777d['data1']||!_0x53777d[_0x7b07('0xb')]){throw new db[(_0x7b07('0x1c'))]['ValidationError'](_0x7b07('0x27'));}_0x4fc266=_0x53777d;var _0x92e583=[];_0x30ec1b[_0x7b07('0x3e')](function(_0x516676){_0x92e583[_0x7b07('0x3f')](checkTranscribeJob(_0x516676,_0x3e5593,_0x4fc266));});return BPromise[_0x7b07('0x40')](_0x92e583);})[_0x7b07('0x35')](function(_0x188590){if(_0x188590){if(!_0x188590[_0x7b07('0x36')]||_0x188590['name']!==_0x7b07('0x37')){logger[_0x7b07('0x38')](util[_0x7b07('0x39')](_0x188590,{'showHidden':![],'depth':null}));}}});}exports[_0x7b07('0x41')]=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};