~gabriel1984sibiu/openshot/openshot

« back to all changes in this revision

Viewing changes to openshot/uploads/vimeo/convenience.py

  • Committer: Jonathan Thomas
  • Date: 2010-11-23 14:46:59 UTC
  • Revision ID: jonathan.oomph@gmail.com-20101123144659-59hrhh5yxlb4ijtg
Added 2 python libraries that will assist with connecting and 
uploading files to Vimeo.com (i.e. oauth2).

Refactored some of the upload methods, to better support having
folders full of python files related to a single upload service.

The vimeo.com upload does not work yet,

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Module providing convenience classes and methods for some basic API procedures.
 
3
 
 
4
In general, this module is more rigid than the base module, in that it relies
 
5
on some current API behavior (e.g. hard-coding some parameters in the Uploader
 
6
class) where the base module chooses to remain ambigous. While this module
 
7
shouldn't completely break either until Vimeo seriously changes their API, keep
 
8
in mind that if something in this module doesn't work, it still might work the
 
9
"conventional" way using just the base module.
 
10
"""
 
11
from os.path import getsize
 
12
from cStringIO import StringIO
 
13
from urllib import urlencode
 
14
 
 
15
import urllib2
 
16
import oauth2
 
17
 
 
18
from . import VimeoClient, VimeoError, API_REST_URL
 
19
from httplib2wrap import Http
 
20
 
 
21
class VimeoUploader(object):
 
22
    """
 
23
    A convenience uploader class to be used alongside a client.
 
24
 
 
25
    The ticket is assumed to be a dict-like object, which means that if you
 
26
    aren't using a JSON client the ticket will need to be converted first.
 
27
    """
 
28
    def __init__(self, vimeo_client, ticket, **kwargs):
 
29
        self.vimeo_client = vimeo_client
 
30
        self.endpoint = ticket["endpoint"]
 
31
        self.ticket_id = ticket["id"]
 
32
        self.max_file_size = ticket["max_file_size"]
 
33
        self.chunk_id = 0
 
34
 
 
35
        self.user = getattr(vimeo_client, "user", None)
 
36
 
 
37
        quota = kwargs.pop("quota", {})
 
38
        self.has_sd_quota = bool(quota.get("sd_quota", None))
 
39
        self.has_hd_quota = bool(quota.get("hd_quota", None))
 
40
        self.upload_space = quota.get("upload_space", {})
 
41
 
 
42
    def _check_file_size(self, file_size):
 
43
        if file_size > self.upload_space.get("free", file_size):
 
44
            raise VimeoError("Not enough free space to upload the file.")
 
45
        elif file_size > self.max_file_size:
 
46
            raise VimeoError("File is larger than the maximum allowed size.")
 
47
 
 
48
    def _post_to_endpoint(self, open_file, **kwargs):
 
49
        params = {"chunk_id" : self.chunk_id,
 
50
                  "ticket_id" : self.ticket_id}
 
51
 
 
52
        headers = kwargs.get("headers",
 
53
                             dict(self.vimeo_client._CLIENT_HEADERS))
 
54
 
 
55
        request = oauth2.Request.from_consumer_and_token(
 
56
                                          consumer=self.vimeo_client.consumer,
 
57
                                          token=self.vimeo_client.token,
 
58
                                          http_method="POST",
 
59
                                          http_url=self.endpoint,
 
60
                                          parameters=params)
 
61
 
 
62
        request.sign_request(self.vimeo_client.signature_method,
 
63
                             self.vimeo_client.consumer,
 
64
                             self.vimeo_client.token)
 
65
 
 
66
        # httplib2 doesn't support uploading out of the box, so use our wrap
 
67
        return Http().request_with_files(url=self.endpoint,
 
68
                                         method="POST",
 
69
                                         body=request,
 
70
                                         body_files={"file_data" : open_file},
 
71
                                         headers=headers)
 
72
 
 
73
    def upload(self, file_path, chunk=False, chunk_size=2*1024*1024,
 
74
               chunk_complete_hook=lambda chunk_info : None):
 
75
        """
 
76
        Performs the steps of an upload. Checks file size and can handle
 
77
        splitting into chunks.
 
78
        """
 
79
 
 
80
        file_size = getsize(file_path)
 
81
        self._check_file_size(file_size)
 
82
 
 
83
        if chunk:
 
84
            with open(file_path) as video:
 
85
                this_chunk = video.read(chunk_size)
 
86
                while this_chunk:
 
87
                    this_chunk = StringIO(this_chunk)
 
88
                    self._post_to_endpoint(this_chunk)
 
89
 
 
90
                    chunk_info = {"total_size" : file_size,
 
91
                                  "chunk_size" : chunk_size,
 
92
                                  "chunk_id" : self.chunk_id,
 
93
                                  "file" : file_path}
 
94
                    chunk_complete_hook(chunk_info)
 
95
                    self.chunk_id += 1
 
96
                    this_chunk = video.read(chunk_size)
 
97
        else:
 
98
            self._post_to_endpoint(open(file_path))
 
99
        return self.vimeo_client.vimeo_videos_upload_verifyChunks(
 
100
                                                ticket_id=self.ticket_id)
 
101
 
 
102
    def complete(self):
 
103
        """
 
104
        Finish an upload.
 
105
        """
 
106
        return self.vimeo_client.vimeo_videos_upload_complete(
 
107
                                                ticket_id=self.ticket_id)