-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdropbox-pushover.py
executable file
·100 lines (88 loc) · 3.39 KB
/
dropbox-pushover.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#! /usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'policecar'
from config import *
from dropbox import client, rest, session
import sys, re, logging, pickle
import httplib, urllib
# Pushover specifications
PUSHOVER_URL = "api.pushover.net"
PUSHOVER_PATH = "/1/messages.json"
# display progress logs on stdout
# set level to logging.INFO for verbosity
logging.basicConfig( level=logging.ERROR,
format='%(asctime)s %(levelname)s %(message)s' )
log = logging.getLogger(__name__)
# set up session
if not APP_KEY or not APP_SECRET:
print "You have to configure your Dropbox application tokens in config.py for this to work."
sys.exit()
sess = session.DropboxSession( APP_KEY, APP_SECRET, ACCESS_TYPE )
log.info( 'read Dropbox access token from file' )
try:
with open( auth_filename, 'rb' ) as f:
sess.token = pickle.load( f )
except IOError as e:
log.info( 'no access tokens stored; authorize dropbox-pushover app' )
request_token = sess.obtain_request_token()
url = sess.build_authorize_url( request_token )
print "For this to work, please grant your app access to your Dropbox at", url, "then hit 'Enter' here to continue."
raw_input()
print "proceeding to fetch all available deltas now"
print "once done, set up a cronjob on this script to receive notifications upon further changes"
# consider fixing the akward construction above
access_token = sess.obtain_access_token( request_token )
# save access token for future requests
try:
with open( auth_filename, 'wb' ) as f:
pickle.dump( access_token, f, pickle.HIGHEST_PROTOCOL )
except IOError as ioerr:
print "Check your path configurations in config.py - the specified auth path doesn't seem to exist."
# get client
client = client.DropboxClient( sess )
log.info( 'read pull state from file' )
try:
with open( cursor_filename, 'rb' ) as f:
cursor = pickle.load( f )
except IOError as e:
cursor = None
pass
log.info( 'fetch available deltas' )
# ( cf. https://www.dropbox.com/developers/reference/api#delta )
more = True
notify = False
folder_to_watch = folder_to_watch.decode( 'utf-8', 'replace' )
while more:
delta = client.delta( cursor=cursor )
cursor = delta[ 'cursor' ]
more = delta[ 'has_more' ]
# check if delta includes any entries from desired folder
for entry in delta[ 'entries' ]:
if re.match( folder_to_watch, entry[0] ):
notify = True
break
log.info( 'write pull state to file' )
try:
with open( cursor_filename, 'wb' ) as f:
pickle.dump( cursor, f, pickle.HIGHEST_PROTOCOL )
except IOError as ioerr:
print "Check your path configurations in config.py - the specified cursor path doesn't seem to exist."
if notify:
if not USER_TOKEN:
print "You have to configure your Pushover user token in config.py for this to work."
sys.exit()
log.info( 'send message to Pushover' )
conn = httplib.HTTPSConnection( PUSHOVER_URL )
conn.request( 'POST', PUSHOVER_PATH,
urllib.urlencode({
'title' : '( : db ',
'token' : APP_TOKEN,
'user' : USER_TOKEN,
'message': ')',
}), { 'Content-type': 'application/x-www-form-urlencoded' })
# for debugging
res = conn.getresponse()
log.info( 'response: ' + str( res.status ) + ' ' + res.reason )
conn.close()
else:
log.info( "things didn't change" )