Example #1
0
    def test_prepare_vidly_video_url(self):
        url = 'https://foo.bar/file.flv'
        eq_(utils.prepare_vidly_video_url(url), url)

        url = 'https://mybucket.s3.amazonaws.com/file.mp4'
        eq_(utils.prepare_vidly_video_url(url), url + '?nocopy')

        url = 'https://mybucket.s3.amazonaws.com/file.mp4?cachebust=1'
        eq_(utils.prepare_vidly_video_url(url), url + '&nocopy')
Example #2
0
    def test_prepare_vidly_video_url(self):
        url = 'https://foo.bar/file.flv'
        eq_(utils.prepare_vidly_video_url(url), url)

        url = 'https://mybucket.s3.amazonaws.com/file.mp4'
        eq_(utils.prepare_vidly_video_url(url), url + '?nocopy')

        url = 'https://mybucket.s3.amazonaws.com/file.mp4?cachebust=1'
        eq_(utils.prepare_vidly_video_url(url), url + '&nocopy')
Example #3
0
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')
Example #4
0
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')
Example #5
0
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
    }
Example #6
0
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
    }
Example #7
0
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)
Example #8
0
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)
Example #9
0
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)
Example #10
0
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)