From a2b44689b4daed43d81fbfa277b8a8a2aa72cc65 Mon Sep 17 00:00:00 2001 From: Naomi Date: Mon, 25 Apr 2011 17:34:58 -0400 Subject: [PATCH] CC-1799 : Live Studio Playout from media library (pytagsfs) sending a file's metadata to airtime when it is modified --- .../application/controllers/ApiController.php | 11 +++++-- python_apps/api_clients/api_client.py | 14 ++++---- python_apps/pytag-fs/MediaMonitor.cfg | 4 +-- python_apps/pytag-fs/MediaMonitor.py | 32 ++++++++++++++++--- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/airtime_mvc/application/controllers/ApiController.php b/airtime_mvc/application/controllers/ApiController.php index 7e0597214..307f116ee 100644 --- a/airtime_mvc/application/controllers/ApiController.php +++ b/airtime_mvc/application/controllers/ApiController.php @@ -10,6 +10,7 @@ class ApiController extends Zend_Controller_Action $context->addActionContext('version', 'json') ->addActionContext('recorded-shows', 'json') ->addActionContext('upload-recorded', 'json') + ->addActionContext('reload-metadata', 'json') ->initContext(); } @@ -121,7 +122,7 @@ class ApiController extends Zend_Controller_Action "nextShow"=>Show_DAL::GetNextShows($timeNow, 5), "timezone"=> date("T"), "timezoneOffset"=> date("Z")); - + //echo json_encode($result); header("Content-type: text/javascript"); echo $_GET['callback'].'('.json_encode($result).')'; @@ -314,11 +315,11 @@ class ApiController extends Zend_Controller_Action } } - $this->view->id = $file->getId(); + $this->view->id = $file->getId(); } public function reloadMetadataAction() { - + global $CC_CONFIG; $api_key = $this->_getParam('api_key'); @@ -328,6 +329,10 @@ class ApiController extends Zend_Controller_Action print 'You are not allowed to access this resource.'; exit; } + + $md = $this->_getParam('md'); + + $this->view->response = $md; } } diff --git a/python_apps/api_clients/api_client.py b/python_apps/api_clients/api_client.py index 197e67aae..167ddccae 100644 --- a/python_apps/api_clients/api_client.py +++ b/python_apps/api_clients/api_client.py @@ -98,7 +98,7 @@ class ApiClientInterface: def upload_recorded_show(self): pass - def update_media_metadata(self): + def update_media_metadata(self, md): pass # Put here whatever tests you want to run to make sure your API is working @@ -350,18 +350,20 @@ class AirTimeApiClient(ApiClientInterface): return response - def update_media_metadata(self): + def update_media_metadata(self, md): logger = logging.getLogger() response = None try: - url = "http://%s:%s/%s/%s" % (self.config["base_url"], str(self.config["base_port"]), self.config["api_base"], self.config["show_schedule_url"]) - #url = self.config["base_url"] + self.config["api_base"] + self.config["show_schedule_url"] + url = "http://%s:%s/%s/%s" % (self.config["base_url"], str(self.config["base_port"]), self.config["api_base"], self.config["update_media_url"]) logger.debug(url) url = url.replace("%%api_key%%", self.config["api_key"]) + + data = urllib.urlencode(md) + req = urllib2.Request(url, data) + response = urllib2.urlopen(req) - response = urllib.urlopen(url) response = json.loads(response.read()) - logger.info("shows %s", response) + logger.info("update media %s", response) except Exception, e: logger.error("Exception: %s", e) diff --git a/python_apps/pytag-fs/MediaMonitor.cfg b/python_apps/pytag-fs/MediaMonitor.cfg index d32a6d4b1..2c22fe656 100644 --- a/python_apps/pytag-fs/MediaMonitor.cfg +++ b/python_apps/pytag-fs/MediaMonitor.cfg @@ -19,5 +19,5 @@ api_base = 'api' # URL to get the version number of the server API version_url = 'version/api_key/%%api_key%%' -# URL to get the schedule of shows set to record -show_schedule_url = 'recorded-shows/format/json/api_key/%%api_key%%' +# URL to tell Airtime to update file's meta data +update_media_url = 'reload-metadata/format/json/api_key/%%api_key%%' diff --git a/python_apps/pytag-fs/MediaMonitor.py b/python_apps/pytag-fs/MediaMonitor.py index c84cc3abf..1016afcfa 100644 --- a/python_apps/pytag-fs/MediaMonitor.py +++ b/python_apps/pytag-fs/MediaMonitor.py @@ -8,6 +8,7 @@ import time import datetime import os import sys +from subprocess import Popen, PIPE, STDOUT from configobj import ConfigObj @@ -34,20 +35,41 @@ except Exception, e: mask = pyinotify.ALL_EVENTS wm = WatchManager() -wdd = wm.add_watch('/srv/airtime/stor', mask, rec=True) +wdd = wm.add_watch('/srv/airtime/stor', mask, rec=True) + +class MediaMonitor(ProcessEvent): + + def my_init(self): + """ + Method automatically called from ProcessEvent.__init__(). Additional + keyworded arguments passed to ProcessEvent.__init__() are then + delegated to my_init(). + """ + self.api_client = api_client.api_client_factory(config) -class PTmp(ProcessEvent): def process_IN_CREATE(self, event): if event.dir : global wm wdd = wm.add_watch(event.pathname, mask, rec=True) #print wdd.keys() - + print "%s: %s" % (event.maskname, os.path.join(event.path, event.name)) def process_IN_MODIFY(self, event): if not event.dir : - print event.path + p = Popen(["pytags", event.pathname], stdout=PIPE, stderr=STDOUT) + output = p.stdout.read().decode("utf-8").strip() + print output.split("\n") + + md = {'filepath':event.pathname} + + for tag in output.split("\n")[2:] : + key,value = tag.split("=") + md[key] = value + + data = {'md': md} + + response = self.api_client.update_media_metadata(data) print "%s: %s" % (event.maskname, os.path.join(event.path, event.name)) @@ -59,7 +81,7 @@ if __name__ == '__main__': print 'Media Monitor' try: - notifier = Notifier(wm, PTmp(), read_freq=2, timeout=1) + notifier = Notifier(wm, MediaMonitor(), read_freq=2, timeout=1) notifier.coalesce_events() notifier.loop() except KeyboardInterrupt: