public
Last active

PushBullet notifications when files change in Dropbox

  • Download Gist
PushBox.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#!/usr/bin/env python
 
import time
import shelve
# dropbox uses posix paths and this script should work on windows too
import posixpath
from StringIO import StringIO
 
from dropbox import client, rest, session
import requests
 
 
WATCH_DIR = '/'
 
 
 
 
# dropbox is case insensitive
WATCH_DIR = WATCH_DIR.lower()
# tailing / so that foo doesn't match foobar
if not WATCH_DIR.endswith('/'):
WATCH_DIR += '/'
 
shelf = shelve.open('secrets')
if 'pushbullet_api_key' not in shelf:
shelf['pushbullet_api_key'] = raw_input('PushBullet API Key: ')
if 'api_key' not in shelf:
shelf['api_key'] = raw_input('Dropbox API Key: ')
if 'api_secret' not in shelf:
shelf['api_secret'] = raw_input('Dropbox API Secret: ')
 
sess = session.DropboxSession(shelf['api_key'], shelf['api_secret'], 'dropbox')
 
if 'token_key' not in shelf:
request_token = sess.obtain_request_token()
 
# Make the user sign in and authorize this token
print "url:", sess.build_authorize_url(request_token)
print "Please visit this website and press the 'Allow' button, then hit",
print "'Enter' here."
raw_input()
 
# This will fail if the user didn't visit the above URL and hit 'Allow'
access_token = sess.obtain_access_token(request_token)
shelf['token_key'] = access_token.key
shelf['token_secret'] = access_token.secret
 
sess.set_token(shelf['token_key'], shelf['token_secret'])
 
device_id = requests.get('https://www.pushbullet.com/api/devices',
auth=(shelf['pushbullet_api_key'], ''))\
.json()['devices'][0]['id']
 
client = client.DropboxClient(sess)
resp = client.delta()
 
while True:
resp = client.delta(cursor=resp['cursor'])
changes = []
for entry, meta in resp['entries']:
if meta is None or meta['is_dir'] or \
not (posixpath.split(entry)[0] + '/').startswith(WATCH_DIR):
continue
file_ = StringIO(client.get_file(entry).read())
file_.name = posixpath.split(entry)[1]
 
requests.post('https://www.pushbullet.com/api/pushes',
auth=(shelf['pushbullet_api_key'], ''),
files=dict(file=file_),
data=dict(device_id=device_id, type='file'))
 
 
time.sleep(5*60)
requirements.txt
1 2
dropbox
requests

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.