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.
This commit is contained in:
parent
73238f92af
commit
864cda6c34
|
@ -173,6 +173,11 @@ class PluploadController extends Zend_Controller_Action
|
||||||
$soundcloud->uploadTrack($file->getRealFilePath(), $file->getName());
|
$soundcloud->uploadTrack($file->getRealFilePath(), $file->getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$show_instance = $this->_getParam('show_instance');
|
||||||
|
|
||||||
|
$show = new ShowInstance($show_instance);
|
||||||
|
$show->setRecordedFile($file->getId());
|
||||||
|
|
||||||
die('{"jsonrpc" : "2.0", "id" : '.$file->getId().'}');
|
die('{"jsonrpc" : "2.0", "id" : '.$file->getId().'}');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -707,6 +707,14 @@ class ShowInstance {
|
||||||
->delete();
|
->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setRecordedFile($file_id) {
|
||||||
|
|
||||||
|
$showInstance = CcShowInstancesQuery::create()
|
||||||
|
->findPK($this->_instanceId);
|
||||||
|
$showInstance->setDbRecordedFile($file_id)
|
||||||
|
->save();
|
||||||
|
}
|
||||||
|
|
||||||
public function getTimeScheduled() {
|
public function getTimeScheduled() {
|
||||||
|
|
||||||
$instance_id = $this->getShowInstanceId();
|
$instance_id = $this->getShowInstanceId();
|
||||||
|
|
|
@ -13,6 +13,7 @@ from poster.streaminghttp import register_openers
|
||||||
import urllib2
|
import urllib2
|
||||||
|
|
||||||
from subprocess import call
|
from subprocess import call
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
# loading config file
|
# loading config file
|
||||||
try:
|
try:
|
||||||
|
@ -23,19 +24,47 @@ except Exception, e:
|
||||||
|
|
||||||
shows_to_record = {}
|
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"
|
def record_show(self):
|
||||||
filename = filename.replace(" ", "-")
|
|
||||||
filepath = "%s%s.%s" % (config["base_recorded_files"], filename, filetype)
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
|
command = "ecasound -i alsa -o %s -t:%s" % (filepath, length)
|
||||||
call(command, shell=True)
|
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):
|
def getDateTimeObj(time):
|
||||||
|
|
||||||
|
@ -47,29 +76,41 @@ def getDateTimeObj(time):
|
||||||
|
|
||||||
def process_shows(shows):
|
def process_shows(shows):
|
||||||
|
|
||||||
|
global shows_to_record
|
||||||
|
shows_to_record = {}
|
||||||
|
|
||||||
for show in shows:
|
for show in shows:
|
||||||
show_starts = getDateTimeObj(show[u'starts'])
|
show_starts = getDateTimeObj(show[u'starts'])
|
||||||
show_end = getDateTimeObj(show[u'ends'])
|
show_end = getDateTimeObj(show[u'ends'])
|
||||||
time_delta = show_end - show_starts
|
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():
|
def check_record():
|
||||||
|
|
||||||
tnow = datetime.datetime.now()
|
tnow = datetime.datetime.now()
|
||||||
sorted_show_keys = sorted(shows_to_record.keys())
|
sorted_show_keys = sorted(shows_to_record.keys())
|
||||||
|
print sorted_show_keys
|
||||||
start_time = sorted_show_keys[0]
|
start_time = sorted_show_keys[0]
|
||||||
next_show = getDateTimeObj(start_time)
|
next_show = getDateTimeObj(start_time)
|
||||||
|
|
||||||
|
print next_show
|
||||||
|
print tnow
|
||||||
delta = next_show - tnow
|
delta = next_show - tnow
|
||||||
|
print delta
|
||||||
|
|
||||||
if delta <= datetime.timedelta(seconds=60):
|
if delta <= datetime.timedelta(seconds=60):
|
||||||
|
print "sleeping %s seconds until show" % (delta.seconds)
|
||||||
time.sleep(delta.seconds)
|
time.sleep(delta.seconds)
|
||||||
|
|
||||||
show_length = shows_to_record[start_time]
|
show_length = shows_to_record[start_time][0]
|
||||||
filepath = record_show(show_length.seconds, start_time)
|
show_instance = shows_to_record[start_time][1]
|
||||||
upload_file(filepath)
|
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():
|
def get_shows():
|
||||||
|
@ -77,7 +118,6 @@ def get_shows():
|
||||||
url = config["base_url"] + config["show_schedule_url"]
|
url = config["base_url"] + config["show_schedule_url"]
|
||||||
response = urllib.urlopen(url)
|
response = urllib.urlopen(url)
|
||||||
data = response.read()
|
data = response.read()
|
||||||
print data
|
|
||||||
|
|
||||||
response_json = json.loads(data)
|
response_json = json.loads(data)
|
||||||
shows = response_json[u'shows']
|
shows = response_json[u'shows']
|
||||||
|
@ -87,29 +127,12 @@ def get_shows():
|
||||||
process_shows(shows)
|
process_shows(shows)
|
||||||
check_record()
|
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
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
get_shows()
|
get_shows()
|
||||||
time.sleep(30)
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue