From 479e69de1203c04819a6b26b93e2f40456c4b280 Mon Sep 17 00:00:00 2001
From: Martin Konecny <martin.konecny@gmail.com>
Date: Thu, 18 Apr 2013 15:40:33 -0400
Subject: [PATCH] CC-5009: Use Silan to silently update track length in the
 background

-done
---
 .../application/controllers/ApiController.php  | 18 +++++++++++++++++-
 .../application/models/airtime/CcFiles.php     |  2 ++
 python_apps/pypo/media/update/silananalyzer.py |  9 ++++++---
 utils/airtime-silan.py                         |  1 +
 4 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php
index ec2de93fc..ee382026f 100644
--- a/airtime_mvc/application/controllers/ApiController.php
+++ b/airtime_mvc/application/controllers/ApiController.php
@@ -979,8 +979,24 @@ class ApiController extends Zend_Controller_Action
             list($id, $info) = $pair;
             // TODO : move this code into model -- RG
             $file = Application_Model_StoredFile::Recall($p_id = $id)->getPropelOrm();
+
+            //What we are doing here is setting a more accurate length that was
+            //calculated with silan by actually scanning the entire file. This
+            //process takes a really long time, and so we only do it in the background
+            //after the file has already been imported -MK
+            $length = $file->getDbLength();
+            if (isset($info['length'])) {
+                $length = $info['length'];
+                //length decimal number in seconds. Need to convert it to format
+                //HH:mm:ss to get around silly PHP limitations.
+                $length = Application_Common_DateHelper::secondsToPlaylistTime($length);
+
+                $file->setDbLength($length);
+            }
+
             $cuein = isset($info['cuein']) ? $info['cuein'] : 0;
-            $cueout = isset($info['cueout']) ? $info['cueout'] : $file->getDbLength();
+            $cueout = isset($info['cueout']) ? $info['cueout'] : $length;
+
             $file->setDbCuein($cuein);
             $file->setDbCueout($cueout);
             $file->setDbSilanCheck(true);
diff --git a/airtime_mvc/application/models/airtime/CcFiles.php b/airtime_mvc/application/models/airtime/CcFiles.php
index 9c6bc8359..001c2c7a1 100644
--- a/airtime_mvc/application/models/airtime/CcFiles.php
+++ b/airtime_mvc/application/models/airtime/CcFiles.php
@@ -20,6 +20,8 @@ class CcFiles extends BaseCcFiles {
 
     public function setDbLength($v)
     {
+        //we are using DateTime instead of DateInterval because the latter doesn't
+        //support subseconds :(
         if ($v instanceof DateTime) {
             $dt = $v;
         }
diff --git a/python_apps/pypo/media/update/silananalyzer.py b/python_apps/pypo/media/update/silananalyzer.py
index 03441d614..4682b14c0 100644
--- a/python_apps/pypo/media/update/silananalyzer.py
+++ b/python_apps/pypo/media/update/silananalyzer.py
@@ -49,9 +49,12 @@ class SilanAnalyzer(Thread):
                         if len(comm):
                             out = comm[0].strip('\r\n')
                             info = json.loads(out)
-                            if len(info['sound']) >= 2:
-                                data['cuein'] = str('{0:f}'.format(info['sound'][0][0]))
-                                data['cueout'] = str('{0:f}'.format(info['sound'][-1][1]))
+                            try: data['length'] = str('{0:f}'.format(info['file duration']))
+                            except: pass
+                            try: data['cuein'] = str('{0:f}'.format(info['sound'][0][0]))
+                            except: pass
+                            try: data['cueout'] = str('{0:f}'.format(info['sound'][-1][1]))
+                            except: pass
                     except Exception, e:
                         self.logger.error(str(command))
                         self.logger.error(e)
diff --git a/utils/airtime-silan.py b/utils/airtime-silan.py
index 00e94d77e..d434d2ddb 100644
--- a/utils/airtime-silan.py
+++ b/utils/airtime-silan.py
@@ -55,6 +55,7 @@ try:
                 data = {}
                 data['cuein'] = str('{0:f}'.format(info['sound'][0][0]))
                 data['cueout'] = str('{0:f}'.format(info['sound'][-1][1]))
+                data['length'] = str('{0:f}'.format(info['file duration']))
                 processed_data.append((f['id'], data))
                 total += 1
                 if total % 5 == 0: