Update: It seems the script had gone missing at some point. It's
back :download:<revver-upload-video>.
As you may or may not have noticed, I do a bunch of stuff for Revver.
I ended up writing a sort of a tutorial to the Revver API, and as I
like to collect all kinds of code samples here, I thought I should
crossblog it here. The original is on the Revver developer blog.
One day, I was on a slow internet connection and wanted to upload a
few files. I wanted something more batch-oriented than the web-based
upload, and I have a personal bias against most current Java runtimes.
So I decided to use the cool new API and write a video
upload client, and will walk you through what
it does in this blog entry. Feel free to "just" use the tool, but
hopefully this will also help you in writing your own API clients.
First of all, I wanted to write something that's usable just about
everywhere. I tend to use Python, so that's what the tool is written
in. The Python standard library didn't seem to be able to do HTTP POST
file upload (think web forms) of large files, so I ended up using
curl for that. This should work on any Linux/OS X/etc box with Python
and curl installed. All you Ubuntu/Debian people just get to say
sudo apt-get install curl and that's it.
So, let's dive right in. The tool is imaginatively named
revver-upload-video. The first bit is the command line
parser. Don't be intimidated by the length, this is pretty much
boilerplate code, the actual API-using bits are really small. The
full file is 155 lines, total.
There are basically three kinds of options: mandatory, optional and
for developer use. Mandatory options are enforced later on, and
developer options are mostly meant for playing with the staging
environment and reusing upload tokens from previous, failed,
uploads.
If you've used optparse before, there's not much interesting
there. It just instantiates a parser object and returns it, for the
main function to use. Nothing there touches the Revver API yet.
Next up, we have some utility functions. getPassphrase will read a
passphrase form the file given to --passphrase-file=, or prompt
the user for one. getAPI instantiates an XML-RPC client object
with the login and passphrase, and caches it in options so if you
call getAPI more than once, you're still only prompted for the
passphrase at most once. Nothing in revver-upload-video uses that,
but these are meant to be reusable functions.
All right, now we're getting to the actual meat. getToken calls
the API method video.getUploadTokens to allocate an upload
token, that lets you upload a file to the Revver archive.
createMedia creates a new video in the archive from your uploaded
file by calling video.create in the API. It also adds metadata
like your website URL to the video. Finally, it returns the media id
of the newly-created video.
Finally, we have the main function, and the bits that call it when
you run the tool. Here, we actually parse the command line arguments,
enforce the presence of the mandatory options, and bail out unless you
gave it actual files to upload.
For each file given on the command line, we either use one of the
tokens given to us with --upload-token=, or get one from the API
with getToken. Then we join the upload URL and the token to get
the place to upload the file to, and run curl as a subprocess to
do the actual upload. Checking that curl worked takes 8 lines, and
then we use createMedia to actually create the video. And that's
it!
At this point, we have all we need to do the same thing as the
web-based upload, or the Java upload client. And you can do something
similar, by yourself. This file is copyright Revver, Inc, but licensed
under the MIT license -- that means you can use it as a base for
writing your software, without any real restrictions. Download the
whole thing here: revver-upload-video.