Last active
December 17, 2015 08:39
-
-
Save kwellman/5581830 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 urllib2, urllib, base64, json | |
from datetime import datetime | |
from time import mktime | |
import feedparser | |
def days_since_last_post(rss_url): | |
d = feedparser.parse(rss_url) | |
if not len(d.entries): | |
return -1 | |
# get date of last blog post and convert to datetime object | |
last_published = datetime.fromtimestamp( | |
mktime(d.entries[0].published_parsed)) | |
now = datetime.utcnow() | |
return (now.date() - last_published.date()).days | |
def send_to_ducksboard(data, widget_url, api_key, is_delta=False): | |
"""Pushes data to your ducksboard widget. Data should be numeric, | |
or a dict if widget is a text/leaderboard/timeline/etc... widget (see | |
documentation). Use is_delta if you're dealing with incremental | |
values (eg. you are specifying differences from the current value). | |
""" | |
if is_delta: | |
msg = {'delta': data} | |
else: | |
msg = {'value': data} | |
request = urllib2.Request(widget_url) | |
# ducksboard uses http basic auth, so setup headers | |
auth = base64.encodestring('%s:x' % api_key) | |
auth = auth.replace('\n', '') | |
request.add_header('Authorization', 'Basic %s' % auth) | |
return urllib2.urlopen(request, json.dumps({'value': data})).read() | |
if __name__ == '__main__': | |
api_key = 'xxxx' | |
widget_url = 'https://push.ducksboard.com/v/72221' | |
days = days_since_last_post('http://blog.interstellr.com/rss') | |
send_to_ducksboard(days, widget_url, api_key) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment