Python script for sharing Dropbox files on webpages !

Hi there !
I always wanted to have a download list on my wordpress sidebar but I don’t really like box.net. I’m using dropbox on ubuntu and I found it really handy so I wrote a python script which is generating public urls of files which are in my Dropbox/Public/downloads. I find out that I can have a rss widget on sidebar so I decided to use it to show my downloads. I choose http://www.delicious.com to generate the rss. My script is simply adding a puburl and the filename (from my dropbox directory) to my delicious bookmarks, than wordpress get’s rss on sidebar widget😉

What you need to use this script:
– Linux with dropbox
– delicious account
– Python (almost on every linux)

Scripts works like this:
Checking if there are any new files in predetermined directory ->; if true: gets filenames and puburls -> send it to delicious.com with “download” tag (to keep bookmarks in harmony:p) -> (here I’m using it by rss but you can do what you want with it:p)

Here is my code, enjoy ( of course you can download it from sidebar – droplist.py )
Please post issues in comments or talk to me on gtalk.

#!/usr/bin/python
# -*- coding: utf-8 -*-
# From author: Stop for a sec on every comment I've posted here, and do what it says  -Tetek
# P.S Add alias to .bashrc to make it faster to run

import urllib2, urllib, commands, dircache, os, struct, time
from stat import *

theurl = "https://api.del.icio.us/v1/posts/add?"
#Enter your username and password ( url is constant for everybody)
username = 'tetek'
password = 'xxx'
try:
	passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
	passman.add_password(None, theurl, username, password)
	authhandler = urllib2.HTTPBasicAuthHandler(passman)
	opener = urllib2.build_opener(authhandler)
	urllib2.install_opener(opener)
	
	
except:
	print "Incorrect username or password (or just connection fucked up)"

home = os.getenv("HOME")
# Please enter path to folder that you want to upload
path = home + '/Dropbox/Public/downloads/'
files = dircache.listdir(path)

#I'm using tag: download, just simply to get stuff in right order
#'replace' is optional..
def Upload_File_List(i):
	out = commands.getoutput('dropbox puburl ' + path + i)
	paired = { "url": out, "description": i, "tags":"download" ,"replace":"no" }
	data = urllib.urlencode(paired)
	full_url = str(theurl + data)
	upload_it = urllib2.urlopen(full_url)
	print " - " + i + " added!"

try:
#datekeeper keeps the date when this script was last runned
	date_keeper = open("datekeeper.txt", "rb")	
	handler = date_keeper.read()
	date_keeper.close()
	last_update = struct.unpack('i', handler)
#It checks if there are some new files to update
	for x in files:
		check_stat = os.stat(path + x) 
		if int(check_stat[ST_ATIME]) >= int(last_update[0]):
			Upload_File_List(x)
		
	print "Synchronised"
except:
		
	for y in files:
		Upload_File_List(y)
	print "Success"
	

	
	
	
try:
#Saves the current time to the datakeeper.txt I used struct lib to keep int binary
	time_saver = open("datekeeper.txt", "wb")
	current = struct.pack('i', time.time())
	time_saver.write(current) 
	time_saver.close()
except:
	print  "Can't save current time do datekeeper.txt"

4 thoughts on “Python script for sharing Dropbox files on webpages !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s