Python API example

From Wikanual

Jump to: navigation, search

While the Api and Python API pages describe functions in detail this example will show you how you can create a python program that uploads a file into INTERDUBS.

You start by downloading the the python module.

The next step is to request an API KEY in your INTERDUBS admin segment under utilities -> API


You then create the following python program. (Don't forget to replace the key)


#!/usr/bin/env python 
from interdubs_api import Interdubs
IDX_PATH            = 'http://www.interdubs.com/api/'
IDX_KEY             = 'your Key goes here'
idx = Interdubs(IDX_PATH, IDX_KEY)
idx.check_api()


That is already a working pythong program. If you have entered the right key. It outputs nothing, which is rather dull. So lets see how many API uses are left:

#!/usr/bin/env python 
from interdubs_api import Interdubs
IDX_PATH            = 'http://www.interdubs.com/api/'
IDX_KEY             = 'your Key goes here'
idx = Interdubs(IDX_PATH, IDX_KEY)
quotaleft = idx.check_api()
print "quota left " + quotaleft

As you can see just querying information is not decreasing your quota counter. Only operations changing data will do that.

So lets do something real. Lets assume you have this file '/your/local/path/file.mov' and that you like to upload into INTERDUBS. Actually you it to appear in the folder '/uploaded/test01'

This is simpler you might think:

#!/usr/bin/env python 
from interdubs_api import Interdubs
IDX_PATH            = 'http://www.interdubs.com/api/'
IDX_KEY             = 'your Key goes here'
idx = Interdubs(IDX_PATH, IDX_KEY)
quotaleft = idx.check_api()
ipath = "/uploaded/test01"
idx.chkmkpath(ipath)
idx.uploadfile_into_path("/your/local/path/file.mov" , ipath)


We removed the output of the quota, and added three lines:

ipath = "/uploaded/test01"

We assign the INTERDUBS target path to a variable since we will be using it twice. Then we see if the directory exists, and have INTERDUBS create it if not:

idx.chkmkpath(ipath)

Now we can upload the file into INTERDUBS with:

idx.uploadfile_into_path("/your/local/path/file.mov" , ipath)

Of course you need to change your local path. As with the Ftp and Droplet it will be a minute before the file actually appears.

You can assign various kinds of meta data to the uploaded file. If you like to use a different displayed title then you would simply change the upload line to:


idx.uploadfile_into_path("/your/local/path/file.mov" , ipath , "Title of File in INTERDUBS")
Personal tools