From 864cda6c34ec99e05b1c4c901b8b0acad26d5211 Mon Sep 17 00:00:00 2001 From: naomiaro Date: Tue, 22 Mar 2011 00:46:31 -0400 Subject: [PATCH] CC-1630 : Automatic recording of shows multi threading now used in recorded for back to back shows. recorded file id is put into show instances table. --- .../controllers/PluploadController.php | 7 +- application/models/Shows.php | 8 ++ python_apps/show-recorder/testrecordscript.py | 89 ++++++++++++------- 3 files changed, 70 insertions(+), 34 deletions(-) diff --git a/application/controllers/PluploadController.php b/application/controllers/PluploadController.php index 25dc47482..0e73bfbb4 100644 --- a/application/controllers/PluploadController.php +++ b/application/controllers/PluploadController.php @@ -173,7 +173,12 @@ class PluploadController extends Zend_Controller_Action $soundcloud->uploadTrack($file->getRealFilePath(), $file->getName()); } - die('{"jsonrpc" : "2.0", "id" : '.$file->getId().' }'); + $show_instance = $this->_getParam('show_instance'); + + $show = new ShowInstance($show_instance); + $show->setRecordedFile($file->getId()); + + die('{"jsonrpc" : "2.0", "id" : '.$file->getId().'}'); } public function pluploadAction() diff --git a/application/models/Shows.php b/application/models/Shows.php index 959a6bf00..57ba0fb45 100644 --- a/application/models/Shows.php +++ b/application/models/Shows.php @@ -707,6 +707,14 @@ class ShowInstance { ->delete(); } + public function setRecordedFile($file_id) { + + $showInstance = CcShowInstancesQuery::create() + ->findPK($this->_instanceId); + $showInstance->setDbRecordedFile($file_id) + ->save(); + } + public function getTimeScheduled() { $instance_id = $this->getShowInstanceId(); diff --git a/python_apps/show-recorder/testrecordscript.py b/python_apps/show-recorder/testrecordscript.py index b5cda7409..4c659cdcb 100644 --- a/python_apps/show-recorder/testrecordscript.py +++ b/python_apps/show-recorder/testrecordscript.py @@ -13,6 +13,7 @@ from poster.streaminghttp import register_openers import urllib2 from subprocess import call +from threading import Thread # loading config file try: @@ -23,18 +24,46 @@ except Exception, e: shows_to_record = {} +class Recorder(Thread): -def record_show(filelength, filename, filetype="mp3"): + def __init__ (self, show_instance, filelength, filename, filetype): + Thread.__init__(self) + self.filelength = filelength + self.filename = filename + self.filetype = filetype + self.show_instance = show_instance - length = str(filelength)+".0" - filename = filename.replace(" ", "-") - filepath = "%s%s.%s" % (config["base_recorded_files"], filename, filetype) + def record_show(self): - command = "ecasound -i alsa -o %s -t:%s" % (filepath, filelength) + length = str(self.filelength)+".0" + filename = self.filename.replace(" ", "-") + filepath = "%s%s.%s" % (config["base_recorded_files"], filename, self.filetype) - call(command, shell=True) + command = "ecasound -i alsa -o %s -t:%s" % (filepath, length) + call(command, shell=True) - return filepath + return filepath + + def upload_file(self, filepath): + + filename = os.path.split(filepath)[1] + + # Register the streaming http handlers with urllib2 + register_openers() + + # headers contains the necessary Content-Type and Content-Length + # datagen is a generator object that yields the encoded parameters + datagen, headers = multipart_encode({"file": open(filepath, "rb"), 'name': filename, 'show_instance': self.show_instance}) + + url = config["base_url"] + config["upload_file_url"] + + req = urllib2.Request(url, datagen, headers) + response = urllib2.urlopen(req).read().strip() + print response + + def run(self): + filepath = self.record_show() + self.upload_file(filepath) def getDateTimeObj(time): @@ -46,30 +75,42 @@ def getDateTimeObj(time): return datetime.datetime(int(date[0]), int(date[1]), int(date[2]), int(time[0]), int(time[1]), int(time[2])) def process_shows(shows): + + global shows_to_record + shows_to_record = {} for show in shows: show_starts = getDateTimeObj(show[u'starts']) show_end = getDateTimeObj(show[u'ends']) time_delta = show_end - show_starts - shows_to_record[show[u'starts']] = time_delta + shows_to_record[show[u'starts']] = [time_delta, show[u'instance_id']] def check_record(): tnow = datetime.datetime.now() sorted_show_keys = sorted(shows_to_record.keys()) + print sorted_show_keys start_time = sorted_show_keys[0] next_show = getDateTimeObj(start_time) + print next_show + print tnow delta = next_show - tnow + print delta if delta <= datetime.timedelta(seconds=60): + print "sleeping %s seconds until show" % (delta.seconds) time.sleep(delta.seconds) - - show_length = shows_to_record[start_time] - filepath = record_show(show_length.seconds, start_time) - upload_file(filepath) + + show_length = shows_to_record[start_time][0] + show_instance = shows_to_record[start_time][1] + show = Recorder(show_instance, show_length.seconds, start_time, filetype="mp3") + show.start() + + #remove show from shows to record. + del shows_to_record[start_time] def get_shows(): @@ -77,39 +118,21 @@ def get_shows(): url = config["base_url"] + config["show_schedule_url"] response = urllib.urlopen(url) data = response.read() - print data - + response_json = json.loads(data) shows = response_json[u'shows'] print shows if len(shows): process_shows(shows) - check_record() - -def upload_file(filepath): - - filename = os.path.split(filepath)[1] - - # Register the streaming http handlers with urllib2 - register_openers() - - # headers contains the necessary Content-Type and Content-Length - # datagen is a generator object that yields the encoded parameters - datagen, headers = multipart_encode({"file": open(filepath, "rb"), 'name': filename}) - - url = config["base_url"] + config["upload_file_url"] - - req = urllib2.Request(url, datagen, headers) - response = urllib2.urlopen(req).read().strip() - print response + check_record() if __name__ == '__main__': while True: get_shows() - time.sleep(30) + time.sleep(5)