Last active
August 29, 2015 14:24
-
-
Save blambi/cca0d4dfff401694cb1e to your computer and use it in GitHub Desktop.
A tiny script for displaying desktop notifications on github activity
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 feedparser | |
import notify2 | |
from time import sleep | |
import cPickle | |
class SeenCache: | |
def __init__(self): | |
try: | |
self.seen_cache = cPickle.load(open('gitnotify.p')) | |
except: | |
self.seen_cache = [] | |
def save(self): | |
cPickle.dump(self.seen_cache, open('gitnotify.p', 'w+')) | |
def got(self, entry_id): | |
try: | |
self.seen_cache.index(entry_id) | |
except ValueError, why: | |
return False | |
return True | |
def add(self, entry_id): | |
if self.got(entry_id): | |
return | |
self.seen_cache.append(entry_id) | |
if len(self.seen_cache) > 250: | |
self.seen_cache[len(self.seen_cache) - 250:] | |
def remove(self, entry_id): | |
self.seen_cache.remove(entry_id) | |
def strip_summary(summary): | |
text = summary[summary.find('<p>') + 3: summary.rfind('</p>')] | |
if text.find('<div c') != -1: # Ignore html | |
return "" | |
return text | |
def notify(title, body): | |
try: | |
n = notify2.Notification(title, body, "/home/blambi/Pictures/icons/github.png") | |
n.show() | |
del n | |
return True | |
except: | |
return False | |
seen_cache = SeenCache() | |
notify2.init("gitnotifier") | |
try: | |
while True: | |
data = feedparser.parse('https://github.com/blambi.private.atom?token=GETYOUROWN') | |
for entry in data.entries: | |
if seen_cache.got(entry.id): | |
continue | |
else: | |
seen_cache.add(entry.id) | |
title = entry.title | |
summary = strip_summary(entry.summary) | |
print "{}: {}".format(entry.date, title) | |
if summary: | |
print summary | |
if not notify(title, summary): | |
print "Warning: trying again" | |
notify2.init("gitnotifier") # Seems wrong to init again.. | |
if not notify(title, summary): | |
print "Error: Failed to send notification aborting this run" | |
seen_cache.remove(entry.id) | |
break | |
sleep(1) # just wait a bit between | |
sleep(30) | |
except KeyboardInterrupt: | |
notify2.uninit() | |
seen_cache.save() | |
print "Saved :D" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment