<?

### DO THIS FIRST:
require_once("/BANGK!/master_config.php");
require_once($master_config_root_path . "shared/functions.shared");
$active_user_session_timeout = "OFF"; // no session timeout on this page
require_once($master_config_root_path . "shared/session.shared");

/// SET VARS FOR CORRECT MAIL HEADERS:
$force_domain = "Tube-to-Text.com";
$tmp_sql = "SELECT * FROM templates_companies WHERE domain = '". $force_domain ."';";
$_SESSION['template']['company'] = object_to_array(readTHECASHIER($tmp_sql));




$incoming_vars = "";
$incoming_vars = array();
logger3("_POST",$_POST);
if($_POST) $incoming_vars = $_POST;
logger3("_GET",$_GET);
if(empty($incoming_vars)) $incoming_vars = $_GET;
$incoming_vars['argv'] = $_SERVER['argv'];


logger3("












########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
### NEW SESSION:  [" . $_SERVER['SCRIPT_NAME'] . "]
########################################################################################
########################################################################################

",$incoming_vars);
#
#
#
### START: SESSIONS & STUFF:


logger3("ARRIVED: transcription engine",1);


$transcription_hash = "";
if(!$transcription_hash) $transcription_hash = $incoming_vars['hash'];
if(!$transcription_hash) $transcription_hash = $incoming_vars['argv'][1];





//$incoming_vars['hash'] = "9e9b93e1bf40022ab037175bc490b2fa";

# $tmp_sql = "SELECT * FROM transcriptions WHERE hash = '". $incoming_vars['hash'] ."';";
# logger3("tmp_sql",$tmp_sql);
# $transcription_details = readTHECASHIER($tmp_sql,"transcriptions");
# logger3("tmp_sql",$tmp_sql);
# 
# 
# 
# $_XXXXX_hash = $transcription_details->hash;
# $_XXXXX_youtube_video_id = $transcription_details->youtube_video_id;


# function postToWatson($send_url,$send_data,$asynchronous = false) {
# 	
# 	global $incoming_vars;
# 	
# 	$username = "10746476-552f-4300-a562-96f63eec6096";
# 	$password = "zwPJt7M1dCUx";
# 
# 	
# 	$send_session = curl_init();
# 	# echo "send_session: $send_session<br>";
# 	
# 	#curl_setopt ($send_session, CURLOPT_HEADER,0);
# 	curl_setopt($send_session, CURLOPT_URL,$send_url);
# 	
# 	curl_setopt($send_session, CURLOPT_POST,1);
# 	curl_setopt($send_session, CURLOPT_NOPROGRESS,1);
# 	curl_setopt($send_session, CURLOPT_COOKIESESSION,1);
# 	curl_setopt($send_session, CURLOPT_RETURNTRANSFER,1);
# 	
# 	
# 	
# 	curl_setopt($send_session, CURLOPT_SSL_VERIFYPEER, FALSE);
# 
# 	curl_setopt($send_session, CURLOPT_TIMEOUT, 7200); // two hours
#     curl_setopt($send_session, CURLOPT_USERPWD, "$username:$password");
# 
# 
# 	# $args['file'] = new CurlFile("http://tube-to-text.com/".$incoming_vars["hash"] .".flac", 'audio/flac',"http://tube-to-text.com/".$incoming_vars["hash"] .".flac");
# 	# 
# 	# logger3("args['file']",$args['file']);
# 	# curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $args);
# 	
# 	curl_setopt($send_session,CURLOPT_HTTPHEADER,array('Content-Type: audio/flac'));
# 	
# 	
# 
# 
# 	# //curl_setopt($send_session,CURLOPT_POSTFIELDS,$send_data);
# 	curl_setopt($send_session,CURLOPT_POSTFIELDS,array(
# 	  	'audio' => "@". $incoming_vars['hash'] .".flac"
# 	  	));
# 	  	
# 	# curl_setopt($send_session,CURLOPT_HTTPHEADER,array('Content-Type: audio/flac'));
# 	# 
# 	# 
#     #     if($asynchronous){
#     #         curl_setopt($send_session, CURLOPT_FRESH_CONNECT, true);
#     #         curl_setopt($send_session, CURLOPT_TIMEOUT,1);
#     #         curl_setopt($send_session, CURLOPT_NOBODY, true); 
#     #     }
#     
# 	$send_results = curl_exec($send_session);
# 	//echo "<pre>send_results: $send_results</pre>";
# 	
# 	if(!$send_results) {
# 		$send_results = curl_error($send_session);
# 	}
# 	
# 	curl_close($send_session);
# 	
# 	return $send_results;
# }


function curlToWatson($send_url,$transcription_hash) {
	 
	$username = "10746476-552f-4300-a562-96f63eec6096";
	$password = "zwPJt7M1dCUx";

     $url = $send_url;
     //$filename = $_FILES['voice']['name'];
     //$filedata = $_FILES['voice']['tmp_name'];
     
     
     $file = fopen("/var/www/html/_other/tube-to-text.com/tubetotext_audio_to_transcribe_". $transcription_hash.".flac", 'r');
     
     $filesize = filesize("/var/www/html/_other/tube-to-text.com/tubetotext_audio_to_transcribe_". $transcription_hash.".flac");
     $bytes = fread($file,$filesize);
     $data = array('part_content_type' => 'audio/flac');
     $headers = array("Content-Type: audio/flac");
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
     curl_setopt($ch, CURLOPT_POST, TRUE);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $bytes);
     curl_setopt($ch, CURLOPT_INFILESIZE, $filesize);
     curl_setopt($ch, CURLOPT_VERBOSE, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     $executed = curl_exec($ch);
     curl_close($ch);
     var_dump($executed); 
     return $executed;
     exit;
}




$send_url = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize";
$send_data = "timestamps=false&max_alternatives=3&profanity_filter=false&smart_formatting=true&speaker_labels=true";

//$curl_results = postToWatson($send_url . "?" . $send_data,$send_data="",$asynchronous = false);

$transcription_raw = $curl_results = curlToWatson($send_url=$send_url . "?" . $send_data,$transcription_hash=$transcription_hash);



logger3("curl_results",$curl_results);

$transcription_array = json_decode($transcription_raw);
	
$transcription_array = object_to_array($transcription_array);

logger3("transcription_array",$transcription_array);
//exit;

//$transcription_array['results'][0]['alternatives'][0]['transcript']

$key = 0;
$transcription_html_formatted = "";
//foreach($transcription_array as $key => $value) {
//logger3("transcription_array['results'][0]['alternatives'][$key]",$transcription_array['results'][0]->['alternatives'][$key]);

//exit;

while($transcription_array['results'][$key]['alternatives'][0]) {

	logger3("transcription_array['results'][0]['alternatives'][$key]['transcript']",$transcription_array['results'][0]['alternatives'][$key]['transcript']);
	
	
	$transcription_html_formatted .= "
	<br>";
	$transcription_html_formatted .= $transcription_array['results'][$key]['alternatives'][0]['transcript'];
	$key++;
	
}

logger3("transcription_html_formatted",$transcription_html_formatted);


//$_XXXXX_transacription_id = "";
//$_XXXXX_hash = "";
//$_XXXXX_url = "";
//$_XXXXX_youtube_video_id = "";
$_XXXXX_IBM_json_response = $transcription_raw;
$_XXXXX_text = $transcription_html_formatted;
//$_XXXXX_title = "";
//$_XXXXX_channel_name = "";
$_XXXXX_keywords = "";
//$_XXXXX_datetime_published = "";
//$_XXXXX_datetime_created = datetime();
	
	
# $tmp_sql = "UPDATE `transcriptions` SET 
# 	`transacription_id`='". $_XXXXX_transacription_id ."'
# 	,`hash`='". $_XXXXX_transacription_id ."'
# 	,`url`='". $_XXXXX_transacription_id ."'
# 	,`youtube_video_id`='". $_XXXXX_transacription_id ."'
# 	,`IBM_json_response`='". $_XXXXX_transacription_id ."'
# 	,`text`='". $_XXXXX_transacription_id ."'
# 	,`title`='". $_XXXXX_transacription_id ."'
# 	,`channel_name`='". $_XXXXX_transacription_id ."'
# 	,`keywords`='". $_XXXXX_transacription_id ."'
# 	,`datetime_published`='". $_XXXXX_transacription_id ."'
# 	,`datetime_created`='". $_XXXXX_transacription_id ."'
# WHERE hash = '". $transcription_hash ."'
# ;";

$tmp_sql = "UPDATE `transcriptions` SET 
	
	`IBM_json_response`='". addslashes($_XXXXX_IBM_json_response) ."'
	,`text`='". addslashes($_XXXXX_text) ."'
	
	,`keywords`='". addslashes($_XXXXX_keywords) ."'
	
WHERE hash = '". $transcription_hash ."'
;";


logger3("tmp_sql",$tmp_sql);

$update_results = updateTHECASHIER($tmp_sql,"transcriptions");


$tmp_sql = "SELECT * FROM transcriptions WHERE hash = '". $transcription_hash ."';";
logger3("tmp_sql",$tmp_sql);
$transcription_details = object_to_array(readTHECASHIER($tmp_sql,"transcriptions"));
logger3("transcriptions_details",$transcription_details);



$to = $transcription_details['requested_by_emailaddress'];
logger3("to",$to);

$subject = "Your transcription is ready: [ ". $transcription_details['title'] . " ]";
logger3("subject",$subject);

$message = "";
$message .= "<span style='line-height:22px; font-size:16px; font-family:verdana;'>";
$message .= "Your transcription is ready: [ ". $transcription_details['title'] . " ]";
$message .= "<br>";
$message .= "<br>";
$message .= "<b>PUBLISHER:</b> ". $transcription_details['channel_name'];
$message .= "<br>";
$message .= "<br>";
$message .= "<b>TITLE:</b> [ ". $transcription_details['title']. " ]";
$message .= "<br>";
$message .= "<br>";
$message .= "<b>LINK:</b> <a href='http://tube-to-text.com/?transcription=". $transcription_details['hash'] . "'>http://tube-to-text.com/?transcription=". $transcription_details['hash'] . "</a>";
$message .= "<br>";
$message .= "<br>";
$message .= "<b>TRANSCRIPTION:</b>";
$message .= "<hr>";
$_XXXXX_text_updated = str_ireplace("%HESITATION", "<span style='color:;'><i> ... </i></span>", $_XXXXX_text);
$message .= $_XXXXX_text_updated;
$message .= "<br>";
$message .= "<br>";
$message .= "<hr>";
$message .= "<b><i>END</i></b>";
$message .= "<hr>";
$message .= "<br>";
$message .= disclaimer($company=$_SESSION['template']['company']['domain']);
$message .= "</span>";
logger3("message",$message);

quick_mailer($to,$subject,$message,$activate_unsubscribe=false,$mail_exchange="SENDGRID");
	







?>

	