def upload_video(event): if isinstance(event, basestring): # pragma: no cover # This is only really used when you use the commandline tool, like # ./manage.py debug-amara upload_video my-cool-slug if event.isdigit(): event = Event.objects.get(id=event) else: event = Event.objects.get( Q(slug=event) | Q(title=event) ) video_url, _ = videoinfo.get_video_url( event, True, False, ) # Same dimensions we use for the Open Graph in main/event.html picture = event.picture and event.picture.file or event.placeholder_img thumb = thumbnail(picture, '385x218', crop='center') data = { 'video_url': video_url, 'title': event.title, 'description': event.short_description or event.description, 'duration': event.duration, # 'primary_audio_language_code': event.primary_audio_language_code, 'team': settings.AMARA_TEAM, 'project': settings.AMARA_PROJECT, 'thumbnail': build_absolute_url(thumb.url), } response = requests.post( settings.AMARA_BASE_URL + '/api/videos/', data=data, headers=_get_headers() ) if not response.status_code == 201: raise UploadError(response.status_code, response.content) upload_info = response.json() amara_video, _ = AmaraVideo.objects.get_or_create( event=event, video_url=video_url, video_id=upload_info['id'], upload_info=upload_info, ) return amara_video
def upload_video(event): if isinstance(event, basestring): # pragma: no cover # This is only really used when you use the commandline tool, like # ./manage.py debug-amara upload_video my-cool-slug if event.isdigit(): event = Event.objects.get(id=event) else: event = Event.objects.get(Q(slug=event) | Q(title=event)) video_url, _ = videoinfo.get_video_url( event, True, False, ) # Same dimensions we use for the Open Graph in main/event.html picture = event.picture and event.picture.file or event.placeholder_img thumb = thumbnail(picture, '385x218', crop='center') data = { 'video_url': video_url, 'title': event.title, 'description': event.short_description or event.description, 'duration': event.duration, # 'primary_audio_language_code': event.primary_audio_language_code, 'team': settings.AMARA_TEAM, 'project': settings.AMARA_PROJECT, 'thumbnail': build_absolute_url(thumb.url), } response = requests.post(settings.AMARA_BASE_URL + '/api/videos/', data=data, headers=_get_headers()) if not response.status_code == 201: raise UploadError(response.status_code, response.content) upload_info = response.json() amara_video, _ = AmaraVideo.objects.get_or_create( event=event, video_url=video_url, video_id=upload_info['id'], upload_info=upload_info, ) return amara_video
def render_edit(edit_id, verbose=False): edit = PopcornEdit.objects.get(id=edit_id) event = edit.event filename = '%s.webm' % edit.id filepath = os.path.join(tempfile.gettempdir(), filename) if not (os.path.isfile(filepath) and os.stat(filepath).st_size > 0): edit.status = PopcornEdit.STATUS_PROCESSING edit.save() if verbose: print 'Rendering file at %s' % filepath process_json( data=edit.data['data'], out=filepath, background_color=edit.data['background'] ) prefix = datetime.datetime.utcnow().strftime('%Y/%m/%d/') upload_file_name = prefix + uuid4().hex[:13] + '.webm' if verbose: print 'Connecting to s3 and uploading as %s' % upload_file_name # Uploads file to s3 connection = boto.connect_s3( settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, ) bucket = connection.lookup(settings.S3_UPLOAD_BUCKET) if bucket is None: if verbose: print 'Creating bucket %s' % settings.S3_UPLOAD_BUCKET bucket = connection.create_bucket(settings.S3_UPLOAD_BUCKET) video_key = Key(bucket) video_key.key = upload_file_name start = time.time() video_key.set_contents_from_filename(filepath) end = time.time() video_url = video_key.generate_url(expires_in=0, query_auth=False) video_url = prepare_vidly_video_url(video_url) if verbose: print 'Video uploaded to S3 at url: %s' % video_url filesize = os.stat(filepath).st_size upload = Upload.objects.create( event=event, user=edit.user, url=video_url, file_name=upload_file_name, mime_type='video/webm', size=filesize, upload_time=int(end - start) ) edit.upload = upload edit.save() if verbose: print 'Upload object created with id: %s' % upload.id webhook_url = build_absolute_url(reverse('popcorn:vidly_webhook')) token_protection = event.privacy != Event.PRIVACY_PUBLIC # if the original submission was *without* HD stick to that hd = True if 'vid.ly' in event.template.name.lower(): submissions = ( VidlySubmission.objects .filter(event=event) .order_by('submission_time') ) for submission in submissions[:1]: hd = submission.hd tag, error = vidly.add_media( url=video_url, token_protection=token_protection, hd=hd, notify_url=webhook_url, ) if verbose: print 'Vidly media added with tag %s' % tag VidlySubmission.objects.create( event=event, url=video_url, tag=tag, hd=True, submission_error=error ) # raise exception if error if error: raise Exception(error) edit.status = PopcornEdit.STATUS_SUCCESS edit.finished = timezone.now() edit.save() if verbose: print 'Removing file at %s' % filepath os.remove(filepath)
def render_edit(edit_id, verbose=False): edit = PopcornEdit.objects.get(id=edit_id) event = edit.event filename = '%s.webm' % edit.id filepath = os.path.join(tempfile.gettempdir(), filename) if not (os.path.isfile(filepath) and os.stat(filepath)[os.stat.st_size] > 0): edit.status = PopcornEdit.STATUS_PROCESSING edit.save() if verbose: print 'Rendering file at %s' % filepath process_json(data=edit.data['data'], out=filepath, background_color=edit.data['background']) prefix = datetime.datetime.utcnow().strftime('%Y/%m/%d/') upload_file_name = prefix + uuid4().hex[:13] + '.webm' if verbose: print 'Connecting to s3 and uploading as %s' % upload_file_name # Uploads file to s3 connection = boto.connect_s3( settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY, ) bucket = connection.lookup(settings.S3_UPLOAD_BUCKET) if bucket is None: if verbose: print 'Creating bucket %s' % settings.S3_UPLOAD_BUCKET bucket = connection.create_bucket(settings.S3_UPLOAD_BUCKET) video_key = Key(bucket) video_key.key = upload_file_name start = time.time() video_key.set_contents_from_filename(filepath) end = time.time() video_url = video_key.generate_url(expires_in=0, query_auth=False) if verbose: print 'Video uploaded to S3 at url: %s' % video_url video_url = prepare_vidly_video_url(video_url) filesize = os.stat(filepath).st_size upload = Upload.objects.create(event=event, user=edit.user, url=video_url, file_name=upload_file_name, mime_type='video/webm', size=filesize, upload_time=int(end - start)) if verbose: print 'Upload object created with id: %s' % upload.id webhook_url = build_absolute_url(reverse('popcorn:vidly_webhook')) token_protection = event.privacy != Event.PRIVACY_PUBLIC # if the original submission was *without* HD stick to that hd = True if 'vid.ly' in event.template.name.lower(): submissions = (VidlySubmission.objects.filter( event=event).order_by('submission_time')) for submission in submissions[:1]: hd = submission.hd tag, error = vidly.add_media( url=video_url, token_protection=token_protection, hd=hd, notify_url=webhook_url, ) if verbose: print 'Vidly media added with tag %s' % tag VidlySubmission.objects.create(event=event, url=video_url, tag=tag, hd=True, submission_error=error) # raise exception if error if error: raise Exception(error) edit.status = PopcornEdit.STATUS_SUCCESS edit.finished = timezone.now() edit.save() if verbose: print 'Removing file at %s' % filepath os.remove(filepath)