helm/bin/prowl-rss
2014-02-07 01:04:38 -05:00

92 lines
2.4 KiB
Python
Executable file

#!/usr/bin/env python
# Checks to see if certain RSS feeds (listed in ~/.prowl-rss/feeds) have been
# updated recently, and if so, send a prowl notification.
# It stores read guids in ~/.prowl-rss/guids.
#
# Intended to be run via cron or on-demand.
from __future__ import print_function
import httplib2
import urllib
import feedparser
import time
import sys
import os
import re
###
API_FILE = os.path.expanduser("~") + "/.prowl-apikey"
GUID_FILE = os.path.expanduser("~") + "/.prowl-rss/guids"
FEEDS_FILE = os.path.expanduser("~") + "/.prowl-rss/feeds"
API_DOMAIN = 'https://prowl.weks.net/publicapi'
# Pulled out of prowlpy @ https://github.com/omgmovieslol/prowlpy/tree/master
def prowl_post(api, app=None, event=None, desc=None, priority=0):
# Create the http object
h = httplib2.Http()
# Set User-Agent
headers = {'User-Agent': "Prowlpy/%s" % "0.41"}
# Perform the request and get the response headers and content
data = {
'apikey': api,
'application': app,
'event': event,
'description': desc,
'priority': priority
}
headers["Content-type"] = "application/x-www-form-urlencoded"
resp,content = h.request("%s/add/" % API_DOMAIN, "POST", headers=headers, body=urllib.urlencode(data))
if resp['status'] == '200':
return True
elif resp['status'] == '401':
raise Exception("Auth Failed: %s" % content)
else:
raise Exception("Failed")
###
try:
guids = [line.strip() for line in open(GUID_FILE)]
conf = [line.strip() for line in open(FEEDS_FILE)]
with open(API_FILE) as f:
apikey = f.read().strip()
feeds = conf
##
fo = open(GUID_FILE, "a")
for feed in feeds:
f = feed.split(' ', 1)
xml = feedparser.parse(f[0])
for e in xml.entries:
if e.guid not in guids:
if len(f) > 1 and not re.match(f[1], e.title):
continue
prowl_post(apikey, "RSS", e.title, "{0} - {1} <{2}>".format(e.title, xml.feed.title, e.link))
fo.write(e.guid+"\n")
guids.append(e.guid)
print("Found: {0} => {1}".format(e.title, e.link))
else:
print("Ignored: {0} => {1}".format(e.title, e.link))
pass
fo.close()
except IOError, e:
print("{0} file couldn't be found/opened".format(e.filename))
sys.exit(1)
except Exception, msg:
print("Error: {0}".format(msg))