Skip to content

Instantly share code, notes, and snippets.

@LindseyB
Created May 6, 2015 20:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LindseyB/977c1df95795c6c3ab24 to your computer and use it in GitHub Desktop.
Save LindseyB/977c1df95795c6c3ab24 to your computer and use it in GitHub Desktop.
# The following is a quick and dirty example of uploading to vimeo using ruby
# It assumes that you already have an access token for a user
require 'net/http'
require 'httparty'
ACCESS_TOKEN = "your access token here"
auth = "Bearer #{ACCESS_TOKEN}"
# get ticket
resp = HTTParty.post "https://api.vimeo.com/me/videos", headers: { "Authorization" => auth, "Accept" => "application/vnd.vimeo.*+json;version=3.2" }, body: { type: "streaming"}
ticket = JSON.parse(resp.body)
target = ticket["upload_link"]
size = File.size("movie.mp4")
last_byte = 0
# do upload
File.open("movie.mp4", "rb") do |f|
uri = URI(target)
while last_byte < size do
# use Net::HTTP for this because HTTParty doesn't appear to work for this and sends too much data
req = Net::HTTP::Put.new("#{uri.path}?#{uri.query}", initheader = { "Authorization" => auth, "Content-Length" => size.to_s, "Content-Range" => "bytes: #{last_byte}-#{size}/#{size}"} )
req.body = f.read
begin
# upload some data
response = Net::HTTP.new(uri.host, uri.port).start {|http| http.request(req) }
rescue Errno::EPIPE
puts "error'd"
end
# check to see how much data was uploaded
progress_resp = HTTParty.put target, headers: { "Content-Range" => 'bytes */*' }
last_byte = progress_resp.headers["range"].split("-").last.to_i
puts last_byte
end
end
# delete ticket
HTTParty.delete "https://api.vimeo.com#{ticket["complete_uri"]}", headers: { "Authorization" => auth }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment