Created
October 6, 2014 18:46
-
-
Save rjames86/1f9ab699d498119d9daa to your computer and use it in GitHub Desktop.
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
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