9 from Queue import Queue
12 from web.wsgiserver import CherryPyWSGIServer
22 CherryPyWSGIServer.ssl_certificate = "cacert.pem"
23 CherryPyWSGIServer.ssl_private_key = "privkey.pem"
26 class CIWorker(threading.Thread):
28 Content Ingestion Worker. A class which executes content ingestion jobs
31 CIWorker shares a Queue with its master where jobs are submitted.
36 Initialize Content Ingestion Worker.
39 threading.Thread.__init__(self, name='CIWorker')
41 def transfer_in(self, raw_video):
43 Transfers a raw video file from the Web Server.
45 @param raw_video raw video file name
48 logger.log_msg('#%s: transfering in...' % self.job_id)
50 file_transfer = config.FILE_TRANSFERER_CLASS( \
51 config.RAW_VIDEOS_PATH, config.WS_UPLOAD_PATH)
52 file_transfer.get([raw_video])
55 def transcode(self, input_video, video_name, transcode_configs):
57 Transcodes a video in each requested formats.
59 @param input_video input video file name
60 @param video_name a video name which must be a valid file name
61 @param transcode_configs a list of dictionaries with format settings
64 logger.log_msg('#%s: transcoding...' % self.job_id)
66 transcoder = config.TRANSCODER_CLASS( \
67 input_file = os.path.join(config.RAW_VIDEOS_PATH, input_video), \
68 name = video_name, prog_bin = config.TRANSCODER_BIN)
69 transcoder.dest_path = config.MEDIA_PATH
71 # Transcode the raw video in each requested format.
72 # TODO report partial errors
73 for transcode_config in transcode_configs:
74 transcode_config['output_file'] = \
75 transcoder.transcode(**transcode_config)
77 def extract_thumbs(self, input_video, video_name, thumbs):
79 Extracts thumbnail images from a video.
81 @param input_video input video file name
82 @param video_name a video name which must be a valid file name
83 @param thumbs use 'random' to extract a thumbnail image from a random
84 point of the video or use a positive integer n to extract n summary
88 logger.log_msg('#%s: extracting image thumbnails...' % self.job_id)
90 # TODO report partial errors
91 thumb_extractor = config.THUMB_EXTRACTOR_CLASS( \
92 input_file = os.path.join(config.RAW_VIDEOS_PATH, input_video), \
94 prog_bin = config.THUMB_EXTRACTOR_BIN)
95 thumb_extractor.dest_path = config.THUMBS_PATH
96 if thumbs == 'random':
97 thumb_extractor.extract_random_thumb()
98 elif type(thumbs) is int and thumbs > 0:
99 thumb_extractor.extract_summary_thumbs(thumbs)
101 def seed(self, transcode_configs):
103 Creates torrents from the videos passed and then stats seeding them.
105 @param transcode_configs a list of dictionaries with format settings
108 logger.log_msg('#%s: creating torrents and starting seeding...' \
111 for transcode_config in transcode_configs:
112 # * CREATE TORRENTS FOR EACH TRANSCODED VIDEO
113 # Create torrent file.
114 bt.create_torrent(transcode_config['output_file'])
116 # The torrent file is created in the same directory with the
117 # source file. Move it to the torrents directory.
118 shutil.move(transcode_config['output_file'] + '.tstream', \
119 config.TORRENTS_PATH)
121 output_file = transcode_config['output_file'] + '.tstream'
122 output_file = output_file[(output_file.rindex('/') + 1):]
125 Server.bit_torrent.start_torrent( \
126 os.path.join(config.TORRENTS_PATH, output_file),
129 def transfer_out(self, local_files, local_path, remote_path):
131 Transfers some local files to a remote path of the Web Server.
133 @param local_files list local files to transfer
134 @param remote_path destination path on the Web Server
137 logger.log_msg('#%s: transfering out...' % self.job_id)
139 file_transfer = config.FILE_TRANSFERER_CLASS( \
140 local_path, remote_path)
141 file_transfer.put(local_files)
142 file_transfer.close()
144 def remove_files(self, files, path):
146 Deletes files from a specified path.
149 logger.log_msg('#%s: cleaning up...' % self.job_id)
152 os.unlink(os.path.join(path, f))
154 def notify_completion(self, code):
155 logger.log_msg('#%s: notifying web server about the job completion...'\
158 if config.WS_COMPLETION[len(config.WS_COMPLETION) - 1] == '/':
159 url = config.WS_COMPLETION + code
161 url = config.WS_COMPLETION + '/' + code
163 f = urllib.urlopen(url)
168 job = Server.queue.get()
169 self.job_id = job['code']
171 # * TRANSFER RAW VIDEO IN
173 self.transfer_in(job['raw_video'])
174 except cis_exceptions.FileAlreadyExistsException as e:
175 logger.log_msg('#%s: %s' \
176 % (job['code'], repr(e)), logger.LOG_LEVEL_ERROR)
178 except Exception as e:
179 logger.log_msg('#%s: error while transferring in: %s' \
180 % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL)
183 # * TRANSCODE RAW VIDEO
185 self.transcode(job['raw_video'], job['name'], \
186 job['transcode_configs'])
187 except cis_exceptions.FileAlreadyExistsException as e:
188 logger.log_msg('#%s: %s' \
189 % (job['code'], repr(e)), logger.LOG_LEVEL_ERROR)
191 except Exception as e:
192 logger.log_msg('#%s: error while transcoding: %s' \
193 % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL)
196 # * EXTRACT THUMBNAIL IMAGES
197 if job['thumbs'] != 0:
199 self.extract_thumbs(job['raw_video'], job['name'], \
201 except cis_exceptions.FileAlreadyExistsException as e:
202 logger.log_msg('#%s: %s' \
203 % (job['code'], repr(e)), logger.LOG_LEVEL_ERROR)
205 except Exception as e:
207 '#%s: error while extracting thumbnail images: %s' \
208 % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL)
211 # * CREATE TORRENTS AND START SEEDING OF TRANSCODED VIDEOS
212 self.seed(job['transcode_configs'])
215 files = [f for f in os.listdir(config.TORRENTS_PATH) \
216 if os.path.isfile(os.path.join( \
217 config.TORRENTS_PATH, f))]
218 torrent_files = fnmatch.filter(files, job['name'] + "_*")
220 # Thumbnail images files.
221 files = [f for f in os.listdir(config.THUMBS_PATH) \
222 if os.path.isfile(os.path.join( \
223 config.THUMBS_PATH, f))]
224 thumb_files = fnmatch.filter(files, job['name'] + "_*")
226 # * TRANSFER TORRENTS AND THUMBNAIL IMAGES OUT
228 self.transfer_out(torrent_files, config.TORRENTS_PATH, \
229 config.WS_TORRENTS_PATH)
230 self.transfer_out(thumb_files, config.THUMBS_PATH, \
231 config.WS_THUMBS_PATH)
232 except Exception as e:
233 logger.log_msg('#%s: error while transferring out: %s' \
234 % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL)
237 # * NOTIFY WEB SERVER ABOUT CONTENT INGESTION COMPLETION
238 # TODO in the future web server should also be notified about errors
240 self.notify_completion(job['code'])
241 except Exception as e:
243 '#%s: error while notifying web server about the job completion: %s' \
244 % (job['code'], repr(e)), logger.LOG_LEVEL_FATAL)
247 # * CLEANUP RAW VIDEOS AND THUMBNAIL IMAGES
248 self.remove_files([ job['raw_video'] ], config.RAW_VIDEOS_PATH)
249 self.remove_files(thumb_files, config.THUMBS_PATH)
252 Server.queue.task_done()
253 Server.load -= job['weight']
254 logger.log_msg('#%s: finished' \
255 % job['code'], logger.LOG_LEVEL_INFO)
260 Implementation of the RESTful web service which constitutes the interface
261 with the client (web server).
270 def GET(self, request):
271 #web.header('Cache-Control', 'no-cache')
273 if request == 'get_load':
274 resp = {"load": Server.load}
275 web.header('Content-Type', 'application/json')
276 return json.dumps(resp)
277 elif request == 'get_torrent_list':
278 resp = Server.bit_torrent.get_torrent_list()
279 web.header('Content-Type', 'application/json')
280 return json.dumps(resp)
281 #elif request == 'shutdown':
283 elif request == 'test':
290 def POST(self, request):
291 if request == 'ingest_content':
292 # Read JSON parameters.
293 json_data = web.data()
294 data = json.loads(json_data)
297 if config.SECURITY and \
298 not self.authenticate(data["username"], data["password"]):
299 return "Authentication failed!"
301 # Add job weight to CIS load.
302 Server.load += data["weight"]
305 Server.queue.put(data)
307 return 'Job submitted.'
308 elif request == 'start_torrents':
309 # Read JSON parameters.
310 json_data = web.data()
311 data = json.loads(json_data)
314 Server.start_torrents(data)
315 elif request == 'stop_torrents':
316 # Read JSON parameters.
317 json_data = web.data()
318 data = json.loads(json_data)
321 Server.stop_torrents(data)
322 elif request == 'remove_torrents':
323 # Read JSON parameters.
324 json_data = web.data()
325 data = json.loads(json_data)
328 Server.stop_torrents(data, True)
334 def start_torrents(torrents=None):
336 Scans torrent path for files in order to start download for the files
337 that are not already started.
342 files = [f for f in os.listdir(config.TORRENTS_PATH) \
343 if os.path.isfile(os.path.join( \
344 config.TORRENTS_PATH, f))]
345 torrents = fnmatch.filter(files, "*.tstream")
347 for torrent_file in torrents:
348 Server.bit_torrent.start_torrent( \
349 os.path.join(config.TORRENTS_PATH, torrent_file),
353 def stop_torrents(torrents, remove_content=False):
354 for torrent_file in torrents:
355 Server.bit_torrent.stop_torrent( \
356 torrent_file, remove_content)
358 def authenticate(self, username, password):
359 if not config.SECURITY:
361 if users.users[username] == password:
368 if __name__ == '__main__':
369 # The BitTorrent object implements a NextShare (Tribler) BitTorrent
370 # client for seeding, downloading etc.
371 Server.bit_torrent = bt.BitTorrent()
372 Server.queue = Queue()
375 Server.start_torrents()
376 t = threading.Timer(config.START_DOWNLOADS_INTERVAL, \
377 Server.start_torrents)
382 ci_worker = CIWorker()
383 ci_worker.daemon = True
387 urls = ('/(.*)', 'Server')
388 app = web.application(urls, globals())