Last active
February 17, 2019 13:30
-
-
Save dschep/5017093 to your computer and use it in GitHub Desktop.
PushBullet notifications when files change in Dropbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dropbox | |
requests |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment