Skip to content

Instantly share code, notes, and snippets.

@pslobo
Forked from rjames86/dropbox_events.py
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pslobo/fce1b1a665d540b7c4d1 to your computer and use it in GitHub Desktop.
Save pslobo/fce1b1a665d540b7c4d1 to your computer and use it in GitHub Desktop.
import feedparser
import time
import datetime
from dateutil import tz
import re
import httplib
import urllib
DROPBOX_PERSONAL_FEED = ''
DROPBOX_WORK_FEED = ''
def pushover(title, message):
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": "",
"user": "",
"title": title,
"message": message,
}),{ "Content-type": "application/x-www-form-urlencoded" })
def is_deleted(entry):
return True if 'deleted' in entry['summary'] else False
def cleanup(summary):
return re.sub('<[^>]*>', '', summary)
def deletion_count(entry):
deletion_search = re.search(r'and (?P<deletion_count>\d+) more files', entry['summary'])
if deletion_search:
return int(deletion_search.group('deletion_count'))
else:
return 1
def make_time(timestamp):
from_zone = tz.gettz('GMT')
to_zone = tz.tzlocal()
dt = datetime.datetime.fromtimestamp(time.mktime(timestamp))
local_time = dt.replace(tzinfo=from_zone)
return local_time.astimezone(to_zone)
if __name__ == '__main__':
for account in [DROPBOX_PERSONAL_FEED, DROPBOX_WORK_FEED]:
to_ret = []
d = feedparser.parse(account)
for entry in d['entries']:
if is_deleted(entry):
to_ret.append(
dict(
entry=cleanup(entry['summary']),
date=make_time(entry['published_parsed']).strftime("%Y-%m-%d %H:%M:%S"),
deletion_count=deletion_count(entry),
)
)
large_deletions = [entry for entry in to_ret if entry['deletion_count'] >= 49]
if large_deletions:
pushover(
'{count}Large {deletions} on Dropbox'.format(
count="%s " % len(large_deletions) if len(large_deletions) > 1 else "",
deletions="Deletions" if len(large_deletions) > 1 else "Deletion"),
large_deletions[0]['entry']
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment