import config
import bt
import users
+import logger
if config.SECURITY:
CherryPyWSGIServer.ssl_certificate = "cacert.pem"
CIWorker shares a Queue with its master where jobs are submitted.
"""
- raw_videos_dir = 'tmp/raw'
- transcoded_videos_dir = 'tmp/media'
- thumbs_dir = 'tmp/thumbs'
- torrents_dir = config.CIS_TORRENTS_PATH
-
def __init__(self):
"""
Initialize Content Ingestion Worker.
@param raw_video raw video file name
"""
- print '** Transfering in...'
+ logger.log_msg('#%s: transfering in...' % self.job_id)
file_transfer = config.FILE_TRANSFERER_CLASS( \
- self.raw_videos_dir, config.WS_UPLOAD_PATH)
+ config.RAW_VIDEOS_PATH, config.WS_UPLOAD_PATH)
file_transfer.get([raw_video])
file_transfer.close()
@param transcode_configs a list of dictionaries with format settings
"""
- print '** Transcoding...'
+ logger.log_msg('#%s: transcoding...' % self.job_id)
transcoder = config.TRANSCODER_CLASS( \
- input_file = os.path.join(self.raw_videos_dir, input_video), \
+ input_file = os.path.join(config.RAW_VIDEOS_PATH, input_video), \
name = video_name, prog_bin = config.TRANSCODER_BIN)
- transcoder.dest_path = self.transcoded_videos_dir
+ transcoder.dest_path = config.MEDIA_PATH
# Transcode the raw video in each requested format.
# TODO report partial errors
thumbnail
"""
- print '** Extracting image thumbnails...'
+ logger.log_msg('#%s: extracting image thumbnails...' % self.job_id)
# TODO report partial errors
thumb_extractor = config.THUMB_EXTRACTOR_CLASS( \
- input_file = os.path.join(self.raw_videos_dir, input_video), \
+ input_file = os.path.join(config.RAW_VIDEOS_PATH, input_video), \
name = video_name, \
prog_bin = config.THUMB_EXTRACTOR_BIN)
- thumb_extractor.dest_path = self.thumbs_dir
+ thumb_extractor.dest_path = config.THUMBS_PATH
if thumbs == 'random':
thumb_extractor.extract_random_thumb()
elif type(thumbs) is int and thumbs > 0:
@param transcode_configs a list of dictionaries with format settings
"""
- print '** Creating torrents and starting seeding...'
+ logger.log_msg('#%s: creating torrents and starting seeding...' \
+ % self.job_id)
for transcode_config in transcode_configs:
# * CREATE TORRENTS FOR EACH TRANSCODED VIDEO
# The torrent file is created in the same directory with the
# source file. Move it to the torrents directory.
shutil.move(transcode_config['output_file'] + '.tstream', \
- self.torrents_dir)
+ config.TORRENTS_PATH)
output_file = transcode_config['output_file'] + '.tstream'
output_file = output_file[(output_file.rindex('/') + 1):]
# * SEED TORRENTS
Server.bit_torrent.start_download( \
- os.path.join(self.torrents_dir, output_file),
- self.transcoded_videos_dir)
-
+ os.path.join(config.TORRENTS_PATH, output_file),
+ config.MEDIA_PATH)
+
def transfer_out(self, local_files, local_path, remote_path):
"""
Transfers some local files to a remote path of the Web Server.
@param remote_path destination path on the Web Server
"""
- print '** Transfering out...'
+ logger.log_msg('#%s: transfering out...' % self.job_id)
file_transfer = config.FILE_TRANSFERER_CLASS( \
local_path, remote_path)
Deletes files from a specified path.
"""
- print '** Cleaning up...'
+ logger.log_msg('#%s: cleaning up...' % self.job_id)
for f in files:
os.unlink(os.path.join(path, f))
def run(self):
while True:
job = Server.queue.get()
+ self.job_id = job['id']
# * TRANSFER RAW VIDEO IN
self.transfer_in(job['raw_video'])
self.seed(job['transcode_configs'])
# Torrent files.
- files = [f for f in os.listdir(self.torrents_dir) \
+ files = [f for f in os.listdir(config.TORRENTS_PATH) \
if os.path.isfile(os.path.join( \
- self.torrents_dir, f))]
+ config.TORRENTS_PATH, f))]
torrent_files = fnmatch.filter(files, job['name'] + "_*")
# Thumbnail images files.
- files = [f for f in os.listdir(self.thumbs_dir) \
+ files = [f for f in os.listdir(config.THUMBS_PATH) \
if os.path.isfile(os.path.join( \
- self.thumbs_dir, f))]
+ config.THUMBS_PATH, f))]
thumb_files = fnmatch.filter(files, job['name'] + "_*")
# * TRANSFER TORRENTS AND THUMBNAIL IMAGES OUT
- self.transfer_out(torrent_files, self.torrents_dir, \
+ self.transfer_out(torrent_files, config.TORRENTS_PATH, \
config.WS_TORRENTS_PATH)
- self.transfer_out(thumb_files, self.thumbs_dir, \
+ self.transfer_out(thumb_files, config.THUMBS_PATH, \
config.WS_THUMBS_PATH)
# * CLEANUP RAW VIDEOS AND THUMBNAIL IMAGES
- self.remove_files([ job['raw_video'] ], self.raw_videos_dir)
- self.remove_files(thumb_files, self.thumbs_dir)
+ self.remove_files([ job['raw_video'] ], config.RAW_VIDEOS_PATH)
+ self.remove_files(thumb_files, config.THUMBS_PATH)
# * JOB FINISHED
Server.queue.task_done()
else:
web.badrequest()
return ""
+
+ @staticmethod
+ def start_downloads():
+ # All torrent files.
+ files = [f for f in os.listdir(config.TORRENTS_PATH) \
+ if os.path.isfile(os.path.join( \
+ config.TORRENTS_PATH, f))]
+ torrent_files = fnmatch.filter(files, "*.tstream")
+
+ for torrent_file in torrent_files:
+ Server.bit_torrent.start_download( \
+ torrent_file,
+ config.MEDIA_PATH)
+
+ t = threading.Timer(config.START_DOWNLOADS_INTERVAL, \
+ Server.start_downloads)
+ t.start()
def authenticate(self, username, password):
if not config.SECURITY:
Server.queue = Queue()
Server.load = 0
+ Server.start_downloads()
+
# Worker thread.
ci_worker = CIWorker()
ci_worker.daemon = True