Skip to content

Instantly share code, notes, and snippets.

@joshcartme
Created April 19, 2013 20:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshcartme/5422913 to your computer and use it in GitHub Desktop.
Save joshcartme/5422913 to your computer and use it in GitHub Desktop.
Uploads a video to Vimeo using https://github.com/dkm/python-vimeo
import os
import time
import urllib2
try:
import simplejson as json
except ImportError:
import json
import vimeo
from vimeo.convenience import VimeoUploader
from django.core.mail import mail_managers
from mezzanine.conf import settings
def upload(file_path, title):
'''
Given a file path uploads a video to vimeo and returns it's vimeo id
'''
client = vimeo.VimeoClient(
key=settings.VIMEO_API_KEY,
secret=settings.VIMEO_API_SECRET,
token=settings.VIMEO_ACCESS_TOKEN,
token_secret=settings.VIMEO_ACCESS_TOKEN_SECRET,
format="json")
quota = client.vimeo_videos_upload_getQuota()
# mail managers if space is running out
if (int(quota['upload_space']['free']) / (1024 * 1024)) < 1000:
mail_managers('YSYS - Vimeo running out of space',
'While uploading a video Vimeo reported that the '
'Vimeo account has less than 1GB of space left.')
f = open(file_path)
# mail managers if Vimeo is out of space
if int(quota['upload_space']['free'] < os.fstat(f.fileno()).st_size):
mail_managers('YSYS - Vimeo out of space',
'While attempting to upload a video, Vimeo reported '
'that the Vimeo account does not have enough space.')
return False
#print "Your current quota is", int(quota['upload_space']['free']) / (1024 * 1024), "MiB"
t = client.vimeo_videos_upload_getTicket()
#print t
vup = VimeoUploader(client, t, quota=quota)
vup.upload(file_path)
retval = vup.complete()
#print retval
vid = retval['video_id']
# do we need to wait a bit for vimeo servers ?
time.sleep(5)
client.vimeo_videos_setTitle(video_id=vid, title=title)
#client.vimeo_videos_setPrivacy(video_id=vid, privacy="anybody")
#client.vimeo_videos_setDescription(description, vid)
return vid
def get_thumbnail(vid):
'''
Returns the thumbnail of Vimeo video
'''
client = vimeo.VimeoClient(
key=settings.VIMEO_API_KEY,
secret=settings.VIMEO_API_SECRET,
token=settings.VIMEO_ACCESS_TOKEN,
token_secret=settings.VIMEO_ACCESS_TOKEN_SECRET,
format="json")
data = client.vimeo_videos_getInfo(video_id=vid)
thumbnail = ''
width = 0
#print data[0]
for thumb in data[0]['thumbnails']['thumbnail']:
if int(thumb['width']) > width:
width = int(thumb['width'])
thumbnail = thumb['_content']
if thumb['width'] == "640":
thumbnail = thumb['_content']
break
thumbnail = thumbnail.replace('\\', '')
return thumbnail
if __name__ == "__main__":
upload()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment