From f70ca93b538700e9304322f50a03ef5fa9ffdf3a Mon Sep 17 00:00:00 2001 From: James Date: Fri, 18 Jan 2013 11:53:26 -0500 Subject: [PATCH] CC-4841: Silan script done --- .../application/controllers/ApiController.php | 35 ++++++++ airtime_mvc/application/models/StoredFile.php | 23 ++++++ python_apps/api_clients/api_client.cfg | 4 + python_apps/api_clients/api_client.py | 15 ++++ utils/airtime-silan/airtime-silan | 22 +++++ utils/airtime-silan/airtime-silan.py | 81 +++++++++++++++++++ 6 files changed, 180 insertions(+) create mode 100755 utils/airtime-silan/airtime-silan create mode 100644 utils/airtime-silan/airtime-silan.py diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index b3a8d8229..6bbe480da 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -38,6 +38,7 @@ class ApiController extends Zend_Controller_Action ->addActionContext('update-source-status' , 'json') ->addActionContext('get-bootstrap-info' , 'json') ->addActionContext('get-files-without-replay-gain' , 'json') + ->addActionContext('get-files-without-silan-value' , 'json') ->addActionContext('reload-metadata-group' , 'json') ->addActionContext('notify-webstream-data' , 'json') ->addActionContext('get-stream-parameters' , 'json') @@ -903,6 +904,18 @@ class ApiController extends Zend_Controller_Action echo json_encode($rows); } + + public function getFilesWithoutSilanValueAction() + { + // disable the view and the layout + $this->view->layout()->disableLayout(); + $this->_helper->viewRenderer->setNoRender(true); + + //connect to db and get get sql + $rows = Application_Model_StoredFile::getAllFilesWithoutSilan(); + + echo json_encode($rows); + } public function updateReplayGainValueAction() { @@ -921,6 +934,28 @@ class ApiController extends Zend_Controller_Action $file->save(); } } + + public function updateCueValuesBySilanAction() + { + // disable layout + $this->view->layout()->disableLayout(); + $this->_helper->viewRenderer->setNoRender(true); + + $request = $this->getRequest(); + $data = json_decode($request->getParam('data')); + Logging::info($data); + foreach ($data as $pair) { + list($id, $info) = $pair; + // TODO : move this code into model -- RG + $cuein = $info->cuein; + $cueout = $info->cueout; + $file = Application_Model_StoredFile::Recall($p_id = $id)->getPropelOrm(); + $file->setDbCuein($cuein); + $file->setDbCueout($cueout); + $file->setDbSilanCheck(true); + $file->save(); + } + } public function notifyWebstreamDataAction() { diff --git a/airtime_mvc/application/models/StoredFile.php b/airtime_mvc/application/models/StoredFile.php index 3fac651e5..bd50f2826 100644 --- a/airtime_mvc/application/models/StoredFile.php +++ b/airtime_mvc/application/models/StoredFile.php @@ -1111,6 +1111,29 @@ SQL; return $rows; } + + public static function getAllFilesWithoutSilan() { + $con = Propel::getConnection(); + + $sql = <<prepare($sql); + + if ($stmt->execute()) { + $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); + } else { + $msg = implode(',', $stmt->errorInfo()); + throw new Exception("Error: $msg"); + } + + return $rows; + } /* Gets number of tracks uploaded to * Soundcloud in the last 24 hours diff --git a/python_apps/api_clients/api_client.cfg b/python_apps/api_clients/api_client.cfg index 369f74eb8..4de375c96 100644 --- a/python_apps/api_clients/api_client.cfg +++ b/python_apps/api_clients/api_client.cfg @@ -126,3 +126,7 @@ get_stream_parameters = 'get-stream-parameters/api_key/%%api_key%%/format/json' push_stream_stats = 'push-stream-stats/api_key/%%api_key%%/format/json' update_stream_setting_table = 'update-stream-setting-table/api_key/%%api_key%%/format/json' + +get_files_without_silan_value = 'get-files-without-silan-value/api_key/%%api_key%%' + +update_cue_values_by_silan = 'update-cue-values-by-silan/api_key/%%api_key%%' diff --git a/python_apps/api_clients/api_client.py b/python_apps/api_clients/api_client.py index c4a12b2c3..b472f08ef 100644 --- a/python_apps/api_clients/api_client.py +++ b/python_apps/api_clients/api_client.py @@ -356,6 +356,14 @@ class AirtimeApiClient(object): """ #http://localhost/api/get-files-without-replay-gain/dir_id/1 return self.services.get_files_without_replay_gain(dir_id=dir_id) + + def get_files_without_silan_value(self): + """ + Download a list of files that need to have their cue in/out value + calculated. This list of files is downloaded into a file and the path + to this file is the return value. + """ + return self.services.get_files_without_silan_value() def update_replay_gain_values(self, pairs): """ @@ -364,6 +372,13 @@ class AirtimeApiClient(object): """ self.logger.debug(self.services.update_replay_gain_value( _post_data={'data': json.dumps(pairs)})) + + def update_cue_values_by_silan(self, pairs): + """ + 'pairs' is a list of pairs in (x, y), where x is the file's database + row id and y is the file's cue values in dB + """ + print self.services.update_cue_values_by_silan(_post_data={'data': json.dumps(pairs)}) def notify_webstream_data(self, data, media_id): diff --git a/utils/airtime-silan/airtime-silan b/utils/airtime-silan/airtime-silan new file mode 100755 index 000000000..be88f8954 --- /dev/null +++ b/utils/airtime-silan/airtime-silan @@ -0,0 +1,22 @@ +#!/bin/bash + +virtualenv_bin="/usr/lib/airtime/airtime_virtualenv/bin/" +. ${virtualenv_bin}activate + +invokePwd=$PWD + +#airtime_silan_path="/usr/lib/airtime/utils/airtime-silan/" +airtime_silan_path="/home/james/src/airtime/utils/airtime-silan/" +airtime_silan_script="airtime-silan.py" + +api_client_path="/usr/lib/airtime/" +cd ${airtime_silan_path} + +exec 2>&1 + +export PYTHONPATH=${api_client_path} + +# Note the -u when calling python! we need it to get unbuffered binary stdout and stderr +exec python -u ${airtime_silan_path}${airtime_silan_script} --dir "$invokePwd" "$@" + +# EOF diff --git a/utils/airtime-silan/airtime-silan.py b/utils/airtime-silan/airtime-silan.py new file mode 100644 index 000000000..d5f691698 --- /dev/null +++ b/utils/airtime-silan/airtime-silan.py @@ -0,0 +1,81 @@ +import logging +from api_clients import api_client as apc +import json +import shutil +import commands +import os +import sys +from configobj import ConfigObj +import subprocess +import traceback + +# create logger +logger = logging.getLogger() + +# no logging +ch = logging.StreamHandler() +logging.disable(50) + +# add ch to logger +logger.addHandler(ch) + +if (os.geteuid() != 0): + print 'Must be a root user.' + sys.exit() + +# loading config file +try: + config = ConfigObj('/etc/airtime/media-monitor.cfg') +except Exception, e: + print('Error loading config file: %s', e) + sys.exit() + +api_client = apc.AirtimeApiClient(config) + +try: + # keep getting few rows at a time for current music_dir (stor + # or watched folder). + subtotal = 0 + while True: + # return a list of pairs where the first value is the + # file's database row id and the second value is the + # filepath + files = api_client.get_files_without_silan_value() + total_files = len(files) + if total_files == 0: break + processed_data = [] + total = 0 + for f in files: + full_path = f['fp'] + # silence detect(set default queue in and out) + try: + command = ['silan', '-f', 'JSON', full_path] + proc = subprocess.Popen(command, stdout=subprocess.PIPE) + out = proc.stdout.read() + info = json.loads(out) + data = {} + data['cuein'] = str('{0:f}'.format(info['sound'][0][0])) + data['cueout'] = str('{0:f}'.format(info['sound'][-1][1])) + processed_data.append((f['id'], data)) + total += 1 + if (total % 5 == 0): + print "Total %s / %s files has been processed.." % (total, total_files) + except Exception, e: + print e + print traceback.format_exc() + break + print "Processed: %d songs" % total + subtotal += total + total = 0 + try: + api_client.update_cue_values_by_silan(processed_data) + except Exception ,e: + print e + print traceback.format_exc() + print "Total %d songs Processed" % subtotal + +except Exception, e: + print e + print traceback.format_exc() + +#update_cue_values_by_silan