Esempio n. 1
0
    def upload(self, filename, **kwargs):
        """Upload a file.

        This should be used to upload a local file. If you want a form for your
        site to upload direct to Vimeo, you should look at the `POST
        /me/videos` endpoint.

        https://developer.vimeo.com/api/endpoints/videos#POST/users/{user_id}/videos

        Args:
            filename (string): Path on disk to file
            **kwargs: Supply a `data` dictionary for data to set to your video
                when uploading. See the API documentation for parameters you
                can send. This is optional.

        Returns:
            string: The Vimeo Video URI of your uploaded video.

        Raises:
            UploadAttemptCreationFailure: If we were unable to create an upload
                attempt for you.
            VideoUploadFailure: If unknown errors occured when uploading your
                video.
        """

        filesize = self.__get_file_size(filename)
        uri = self.UPLOAD_ENDPOINT
        data = kwargs['data'] if 'data' in kwargs else {}

        # Is a `chunk_size` specified? Use default value if not.
        proposed_or_default_chunk_size = data.get('chunk_size', self.DEFAULT_CHUNK_SIZE)
        # For efficiency, lets ensure the pending chunk_size does not result in too many cycles
        chunk_size = self.apply_chunk_size_rules(proposed_or_default_chunk_size, filesize)

        # Ignore any specified upload approach and size.
        if 'upload' not in data:
            data['upload'] = {
                'approach': 'tus',
                'size': filesize
            }
        else:
            data['upload']['approach'] = 'tus'
            data['upload']['size'] = filesize

        attempt = self.post(uri, data=data, params={'fields': 'uri,upload'})
        if attempt.status_code != 200:
            raise exceptions.UploadAttemptCreationFailure(
                attempt,
                "Unable to initiate an upload attempt."
            )

        attempt = attempt.json()

        return self.__perform_tus_upload(filename, attempt, chunk_size=chunk_size)
Esempio n. 2
0
    def replace(self, video_uri, filename, **kwargs):
        """Replace the source of a single Vimeo video.

        https://developer.vimeo.com/api/endpoints/videos#POST/videos/{video_id}/versions

        Args:
            video_uri (string): Vimeo Video URI
            filename (string): Path on disk to file
            **kwargs: Supply a `data` dictionary for data to set to your video
                when uploading. See the API documentation for parameters you
                can send. This is optional.

        Returns:
            string: The Vimeo Video URI of your replaced video.
        """
        filesize = self.__get_file_size(filename)
        uri = self.VERSIONS_ENDPOINT.format(video_uri=video_uri)

        data = kwargs['data'] if 'data' in kwargs else {}
        data['file_name'] = os.path.basename(filename)

        # Is a `chunk_size` specified? Use default value if not.
        proposed_or_default_chunk_size = data.get('chunk_size', self.DEFAULT_CHUNK_SIZE)
        # For efficiency, lets ensure the pending chunk_size does not result in too many cycles
        chunk_size = self.apply_chunk_size_rules(proposed_or_default_chunk_size, filesize)

        # Ignore any specified upload approach and size.
        if 'upload' not in data:
            data['upload'] = {
                'approach': 'tus',
                'size': filesize
            }
        else:
            data['upload']['approach'] = 'tus'
            data['upload']['size'] = filesize

        attempt = self.post(uri, data=data, params={'fields': 'upload'})
        if attempt.status_code != 201:
            raise exceptions.UploadAttemptCreationFailure(
                attempt,
                "Unable to initiate an upload attempt."
            )

        attempt = attempt.json()

        # `uri` doesn't come back from `/videos/:id/versions` so we need to
        # manually set it here for uploading.
        attempt['uri'] = video_uri

        return self.__perform_tus_upload(filename, attempt, chunk_size=chunk_size)