def test_add_media_with_email(self, p_urllib2, p_logging): def mocked_urlopen(request): return StringIO(""" <?xml version="1.0"?> <Response> <Message>All medias have been added.</Message> <MessageCode>2.1</MessageCode> <BatchID>47520</BatchID> <Success> <MediaShortLink> <SourceFile>http://www.com/file.flv</SourceFile> <ShortLink>8oxv6x</ShortLink> <MediaID>13969839</MediaID> <QRCode>http://vid.ly/8oxv6x/qrcodeimg</QRCode> <HtmlEmbed>code code</HtmlEmbed> <EmailEmbed>more code code</EmailEmbed> </MediaShortLink> </Success> </Response> """) p_urllib2.urlopen = mocked_urlopen shortcode, error = vidly.add_media('http//www.com') eq_(shortcode, '8oxv6x') ok_(not error) # same thing should work with optional extras shortcode, error = vidly.add_media( 'http//www.com', email='*****@*****.**', token_protection=True ) eq_(shortcode, '8oxv6x') ok_(not error)
def resubmit(clone): event = clone.event token_protection = event.privacy != Event.PRIVACY_PUBLIC url = clone.url hd = clone.hd site = Site.objects.get_current() base_url = 'https://%s' % site.domain # yuck! webhook_url = base_url + reverse('manage:vidly_media_webhook') shortcode, error = vidly.add_media( url=url, hd=hd, token_protection=token_protection, notify_url=webhook_url, ) VidlySubmission.objects.create( event=event, url=url, token_protection=token_protection, hd=hd, tag=shortcode, submission_error=error ) if not event.template_environment: event.template_environment = {} event.template_environment['tag'] = shortcode event.status = Event.STATUS_PROCESSING event.save() return error
def test_add_media_with_notify_url(self, p_urllib2, p_logging): def mocked_urlopen(request): return StringIO(""" <?xml version="1.0"?> <Response> <Message>All medias have been added.</Message> <MessageCode>2.1</MessageCode> <BatchID>47520</BatchID> <Success> <MediaShortLink> <SourceFile>http://www.com/file.flv</SourceFile> <ShortLink>8oxv6x</ShortLink> <MediaID>13969839</MediaID> <QRCode>http://vid.ly/8oxv6x/qrcodeimg</QRCode> <HtmlEmbed>code code</HtmlEmbed> <EmailEmbed>more code code</EmailEmbed> </MediaShortLink> </Success> </Response> """) def mocked_Request(url, query_string): ok_('<Notify>https://mywebhook.example.com</Notify>' in urllib.unquote(query_string)) return mock.MagicMock() p_urllib2.Request = mocked_Request p_urllib2.urlopen = mocked_urlopen shortcode, error = vidly.add_media( 'http//www.com', notify_url='https://mywebhook.example.com', ) eq_(shortcode, '8oxv6x') ok_(not error)
def vidly_url_to_shortcode(request, id): event = get_object_or_404(Event, id=id) form = forms.VidlyURLForm(data=request.POST) if form.is_valid(): url = form.cleaned_data['url'] if event.privacy != Event.PRIVACY_PUBLIC: # forced token_protection = True else: token_protection = form.cleaned_data['token_protection'] hd = form.cleaned_data['hd'] base_url = get_base_url(request) webhook_url = base_url + reverse('manage:vidly_media_webhook') shortcode, error = vidly.add_media( url, token_protection=token_protection, hd=hd, notify_url=webhook_url, ) VidlySubmission.objects.create( event=event, url=url, token_protection=token_protection, hd=hd, tag=shortcode, submission_error=error ) url_scrubbed = scrub_transform_passwords(url) if shortcode: return {'shortcode': shortcode, 'url': url_scrubbed} else: return http.HttpResponseBadRequest(error) return http.HttpResponseBadRequest(str(form.errors))
def test_add_media_failure(self, p_urllib2, p_logging): def mocked_urlopen(request): # I don't actually know what it would say return StringIO( """ <?xml version="1.0"?> <Response> <Message>Error</Message> <MessageCode>0.0</MessageCode> <Errors> <Error> <ErrorCode>0.0</ErrorCode> <ErrorName>Error message</ErrorName> <Description>bla bla</Description> <Suggestion>ble ble</Suggestion> </Error> </Errors> </Response> """ ) p_urllib2.urlopen = mocked_urlopen shortcode, error = vidly.add_media("http//www.com") ok_(not shortcode) ok_("0.0" in error)
def test_add_media_with_notify_url(self, p_urllib2, p_logging): def mocked_urlopen(request): return StringIO( """ <?xml version="1.0"?> <Response> <Message>All medias have been added.</Message> <MessageCode>2.1</MessageCode> <BatchID>47520</BatchID> <Success> <MediaShortLink> <SourceFile>http://www.com/file.flv</SourceFile> <ShortLink>8oxv6x</ShortLink> <MediaID>13969839</MediaID> <QRCode>http://vid.ly/8oxv6x/qrcodeimg</QRCode> <HtmlEmbed>code code</HtmlEmbed> <EmailEmbed>more code code</EmailEmbed> </MediaShortLink> </Success> </Response> """ ) def mocked_Request(url, query_string): ok_("<Notify>https://mywebhook.example.com</Notify>" in urllib.unquote(query_string)) return mock.MagicMock() p_urllib2.Request = mocked_Request p_urllib2.urlopen = mocked_urlopen shortcode, error = vidly.add_media("http//www.com", notify_url="https://mywebhook.example.com") eq_(shortcode, "8oxv6x") ok_(not error)
def resubmit(clone): event = clone.event token_protection = event.privacy != Event.PRIVACY_PUBLIC url = clone.url hd = clone.hd site = Site.objects.get_current() base_url = 'https://%s' % site.domain # yuck! webhook_url = base_url + reverse('manage:vidly_media_webhook') shortcode, error = vidly.add_media( url=url, hd=hd, token_protection=token_protection, notify_url=webhook_url, ) VidlySubmission.objects.create(event=event, url=url, token_protection=token_protection, hd=hd, tag=shortcode, submission_error=error) if not event.template_environment: event.template_environment = {} event.template_environment['tag'] = shortcode event.status = Event.STATUS_PROCESSING event.save() return error
def vidly_media_resubmit(request): if request.POST.get('cancel'): return redirect(reverse('manage:vidly_media') + '?status=Error') form = forms.VidlyResubmitForm(data=request.POST) if not form.is_valid(): return http.HttpResponse(str(form.errors)) event = get_object_or_404(Event, pk=form.cleaned_data['id']) environment = event.template_environment or {} if not environment.get('tag') or environment.get('tag') == 'None': raise ValueError("Not a valid tag in template") if event.privacy != Event.PRIVACY_PUBLIC: token_protection = True # no choice else: token_protection = form.cleaned_data['token_protection'] base_url = get_base_url(request) webhook_url = base_url + reverse('manage:vidly_media_webhook') old_tag = environment['tag'] url = prepare_vidly_video_url(form.cleaned_data['url']) shortcode, error = vidly.add_media( url=url, hd=form.cleaned_data['hd'], token_protection=token_protection, notify_url=webhook_url, ) VidlySubmission.objects.create( event=event, url=url, token_protection=token_protection, hd=form.cleaned_data['hd'], tag=shortcode, submission_error=error ) if error: messages.warning( request, "Media could not be re-submitted:\n<br>\n%s" % error ) else: messages.success( request, "Event re-submitted to use tag '%s'" % shortcode ) vidly.delete_media(old_tag) event.template_environment['tag'] = shortcode event.status = Event.STATUS_PROCESSING event.save() cache_key = 'vidly-query-%s' % old_tag cache.delete(cache_key) return redirect(reverse('manage:vidly_media') + '?status=Error')
def vidly_media_resubmit(request): if request.POST.get('cancel'): return redirect(reverse('manage:vidly_media') + '?status=Error') form = forms.VidlyResubmitForm(data=request.POST) if not form.is_valid(): return http.HttpResponse(str(form.errors)) event = get_object_or_404(Event, pk=form.cleaned_data['id']) environment = event.template_environment or {} if not environment.get('tag') or environment.get('tag') == 'None': raise ValueError("Not a valid tag in template") if event.privacy != Event.PRIVACY_PUBLIC: token_protection = True # no choice else: token_protection = form.cleaned_data['token_protection'] base_url = get_base_url(request) webhook_url = base_url + reverse('manage:vidly_media_webhook') old_tag = environment['tag'] url = prepare_vidly_video_url(form.cleaned_data['url']) shortcode, error = vidly.add_media( url=url, hd=form.cleaned_data['hd'], token_protection=token_protection, notify_url=webhook_url, ) VidlySubmission.objects.create( event=event, url=url, token_protection=token_protection, hd=form.cleaned_data['hd'], tag=shortcode, submission_error=error ) if error: messages.warning( request, "Media could not be re-submitted:\n<br>\n%s" % error ) else: messages.success( request, "Event re-submitted to use tag '%s'" % shortcode ) vidly.delete_media(old_tag) event.template_environment['tag'] = shortcode event.save() cache_key = 'vidly-query-%s' % old_tag cache.delete(cache_key) return redirect(reverse('manage:vidly_media') + '?status=Error')
def summary(request, event): if request.method == 'POST': # Start the archiving process. # This is basically the same code as # in manage.views.vidly_url_to_shortcode(). token_protection = event.privacy != Event.PRIVACY_PUBLIC # email = request.user.email email = settings.EMAIL_FROM_ADDRESS url = event.upload.url shortcode, error = vidly.add_media( url, email=email, token_protection=token_protection, hd=True, ) VidlySubmission.objects.create( event=event, url=url, email=email, token_protection=token_protection, hd=True, tag=shortcode, submission_error=error ) event.archive_time = None event.status = Event.STATUS_PENDING template = Template.objects.get(default_archive_template=True) event.template = template event.template_environment = {'tag': shortcode} event.save() messages.info( request, "Excellent! Your video is being transcoded. " "Once it finishes, you'll receive an email." ) return redirect('webrtc:summary', event.id) successful_vidly_submission = False # If the uploaded file has a successful VidlySubmission, there's nothing # the user needs to do at this point. if event.upload: submissions = VidlySubmission.objects.filter( event=event, url=event.upload.url, submission_error__isnull=True, ) for submission in submissions.order_by('-submission_time'): successful_vidly_submission = True break context = { 'event': event, 'successful_vidly_submission': successful_vidly_submission, } return render(request, 'webrtc/summary.html', context)
def event_archive(request, event): if event.status != Event.STATUS_INITIATED: return http.HttpResponseBadRequest( "You can't archive events that are NOT in the state of initiated." ) submissions = VidlySubmission.objects.filter( event=event, url__startswith=event.upload.url ) for vidly_submission in submissions.order_by('-submission_time'): break else: # we haven't sent it in for archive yet upload = event.upload base_url = get_base_url(request) webhook_url = base_url + reverse('new:vidly_media_webhook') video_url = prepare_vidly_video_url(upload.url) tag, error = vidly.add_media( video_url, hd=True, notify_url=webhook_url, # Note that we deliberately don't bother yet to set # token_protection here because we don't yet know if the # event is going to be private or not. # Also, it's much quicker to make screencaptures of videos # that are not token protected on vid.ly. ) # then we need to record that we did this vidly_submission = VidlySubmission.objects.create( event=event, url=video_url, tag=tag, hd=True, submission_error=error or None ) default_template = Template.objects.get(default_archive_template=True) # Do an in place edit in case this started before the fetch_duration # has started. Event.objects.filter(id=event.id).update( template=default_template, template_environment={'tag': tag}, ) create_all_timestamp_pictures.delay(event.id) return { 'tag': vidly_submission.tag, 'error': vidly_submission.submission_error }
def event_archive(request, event): if event.status != Event.STATUS_INITIATED: return http.HttpResponseBadRequest( "You can't archive events that are NOT in the state of initiated." ) submissions = VidlySubmission.objects.filter( event=event, url__startswith=event.upload.url ) for vidly_submission in submissions.order_by('-submission_time'): break else: # we haven't sent it in for archive yet upload = event.upload base_url = get_base_url(request) webhook_url = base_url + reverse('new:vidly_media_webhook') video_url = prepare_vidly_video_url(upload.url) tag, error = vidly.add_media( video_url, hd=True, notify_url=webhook_url, # Note that we deliberately don't bother yet to set # token_protection here because we don't yet know if the # event is going to be private or not. # Also, it's much quicker to make screencaptures of videos # that are not token protected on vid.ly. ) # then we need to record that we did this vidly_submission = VidlySubmission.objects.create( event=event, url=video_url, tag=tag, hd=True, submission_error=error or None ) default_template = Template.objects.get(default_archive_template=True) # Do an in place edit in case this started before the fetch_duration # has started. Event.objects.filter(id=event.id).update( template=default_template, template_environment={'tag': tag} ) return { 'tag': vidly_submission.tag, 'error': vidly_submission.submission_error }
def test_add_media_failure(self, p_urllib2, p_logging): def mocked_urlopen(request): # I don't actually know what it would say return StringIO(""" <?xml version="1.0"?> <Response> <Message>Error</Message> <MessageCode>0.0</MessageCode> <Errors> <Error> <ErrorCode>0.0</ErrorCode> <ErrorName>Error message</ErrorName> <Description>bla bla</Description> <Suggestion>ble ble</Suggestion> </Error> </Errors> </Response> """) p_urllib2.urlopen = mocked_urlopen shortcode, error = vidly.add_media('http//www.com') ok_(not shortcode) ok_('0.0' in error)
def legacy_video_migration(request): # pragma: no cover """for one off mass vid.ly submission""" class VideoURLError(Exception): pass def redirect_recurse(url): """return the URL only when it's not redirecting. Raise an error on all other statuses >= 400 """ response = requests.head(url) if response.status_code in (301, 302): return redirect_recurse(response.headers['Location']) elif response.status_code >= 400: raise VideoURLError('{} => {}'.format( url, response.status_code )) return url if request.method == 'POST': events = Event.objects.filter(id__in=request.POST.getlist('ids')) template, = Template.objects.filter(default_archive_template=True) for event in events: try: url = event.template_environment['url'] url = redirect_recurse(url) base_url = get_base_url(request) webhook_url = base_url + reverse('manage:vidly_media_webhook') url = prepare_vidly_video_url(url) token_protection = event.privacy != Event.PRIVACY_PUBLIC shortcode, error = vidly.add_media( url, hd=True, token_protection=token_protection, notify_url=webhook_url, ) VidlySubmission.objects.create( event=event, url=url, token_protection=token_protection, hd=True, tag=shortcode, submission_error=error ) event.template_environment = { 'tag': shortcode, } if shortcode: event.template = template event.archive_time = None event.status = Event.STATUS_PROCESSING event.save() videoinfo.fetch_duration( event, video_url=url, save=True, verbose=settings.DEBUG ) if Event.objects.get(id=event.id).duration: create_all_event_pictures.delay( event.id, video_url=url, ) create_all_timestamp_pictures.delay( event.id, video_url=url, ) except Exception as exception: error = str(exception) messages.error( request, 'Failed to submit "{}". Error: {}'.format( event.title, error, ) ) messages.success( request, 'Submitted {} events for Vid.ly processing'.format( events.count() ) ) return redirect('manage:legacy_video_migration') search = request.GET.get('search', 'http://videos.mozilla.org/') if search: events = Event.objects.filter( template_environment__icontains=search ) else: events = Event.objects.none() context = { 'events': events, 'search': search, } return render(request, 'manage/legacy_video_migration.html', context)
def legacy_video_migration(request): # pragma: no cover """for one off mass vid.ly submission""" class VideoURLError(Exception): pass def redirect_recurse(url): """return the URL only when it's not redirecting. Raise an error on all other statuses >= 400 """ response = requests.head(url) if response.status_code in (301, 302): return redirect_recurse(response.headers['Location']) elif response.status_code >= 400: raise VideoURLError('{} => {}'.format(url, response.status_code)) return url if request.method == 'POST': events = Event.objects.filter(id__in=request.POST.getlist('ids')) template, = Template.objects.filter(default_archive_template=True) for event in events: try: url = event.template_environment['url'] url = redirect_recurse(url) base_url = get_base_url(request) webhook_url = base_url + reverse('manage:vidly_media_webhook') url = prepare_vidly_video_url(url) token_protection = event.privacy != Event.PRIVACY_PUBLIC shortcode, error = vidly.add_media( url, hd=True, token_protection=token_protection, notify_url=webhook_url, ) VidlySubmission.objects.create( event=event, url=url, token_protection=token_protection, hd=True, tag=shortcode, submission_error=error) event.template_environment = { 'tag': shortcode, } if shortcode: event.template = template event.archive_time = None event.status = Event.STATUS_PROCESSING event.save() videoinfo.fetch_duration(event, video_url=url, save=True, verbose=settings.DEBUG) if Event.objects.get(id=event.id).duration: create_all_event_pictures.delay( event.id, video_url=url, ) create_all_timestamp_pictures.delay( event.id, video_url=url, ) except Exception as exception: error = str(exception) messages.error( request, 'Failed to submit "{}". Error: {}'.format( event.title, error, )) messages.success( request, 'Submitted {} events for Vid.ly processing'.format(events.count())) return redirect('manage:legacy_video_migration') search = request.GET.get('search', 'http://videos.mozilla.org/') if search: events = Event.objects.filter(template_environment__icontains=search) else: events = Event.objects.none() context = { 'events': events, 'search': search, } return render(request, 'manage/legacy_video_migration.html', context)
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)
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)