2
Module providing convenience classes and methods for some basic API procedures.
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.
11
from os.path import getsize
12
from cStringIO import StringIO
13
from urllib import urlencode
18
from . import VimeoClient, VimeoError, API_REST_URL
19
from httplib2wrap import Http
21
class VimeoUploader(object):
23
A convenience uploader class to be used alongside a client.
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.
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"]
35
self.user = getattr(vimeo_client, "user", None)
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", {})
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.")
48
def _post_to_endpoint(self, open_file, **kwargs):
49
params = {"chunk_id" : self.chunk_id,
50
"ticket_id" : self.ticket_id}
52
headers = kwargs.get("headers",
53
dict(self.vimeo_client._CLIENT_HEADERS))
55
request = oauth2.Request.from_consumer_and_token(
56
consumer=self.vimeo_client.consumer,
57
token=self.vimeo_client.token,
59
http_url=self.endpoint,
62
request.sign_request(self.vimeo_client.signature_method,
63
self.vimeo_client.consumer,
64
self.vimeo_client.token)
66
# httplib2 doesn't support uploading out of the box, so use our wrap
67
return Http().request_with_files(url=self.endpoint,
70
body_files={"file_data" : open_file},
73
def upload(self, file_path, chunk=False, chunk_size=2*1024*1024,
74
chunk_complete_hook=lambda chunk_info : None):
76
Performs the steps of an upload. Checks file size and can handle
77
splitting into chunks.
80
file_size = getsize(file_path)
81
self._check_file_size(file_size)
84
with open(file_path) as video:
85
this_chunk = video.read(chunk_size)
87
this_chunk = StringIO(this_chunk)
88
self._post_to_endpoint(this_chunk)
90
chunk_info = {"total_size" : file_size,
91
"chunk_size" : chunk_size,
92
"chunk_id" : self.chunk_id,
94
chunk_complete_hook(chunk_info)
96
this_chunk = video.read(chunk_size)
98
self._post_to_endpoint(open(file_path))
99
return self.vimeo_client.vimeo_videos_upload_verifyChunks(
100
ticket_id=self.ticket_id)
106
return self.vimeo_client.vimeo_videos_upload_complete(
107
ticket_id=self.ticket_id)