helm/bin/prowl-rss

104 lines
2.9 KiB
Plaintext
Raw Normal View History

2013-12-16 22:29:52 -08:00
#!/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.
2013-12-16 22:29:52 -08:00
from __future__ import print_function
import httplib2
import urllib
import feedparser
import time
import sys
import os
2014-02-06 21:53:48 -08:00
import re
2013-12-16 22:29:52 -08:00
2013-12-16 22:45:04 -08:00
###
API_FILE = os.path.expanduser("~") + "/.prowl-apikey"
GUID_FILE = os.path.expanduser("~") + "/.prowl-rss/guids"
FEEDS_FILE = os.path.expanduser("~") + "/.prowl-rss/feeds"
2013-12-16 22:45:04 -08:00
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':
2014-03-17 14:24:26 -07:00
raise Exception("Auth Failed: {0}".format(content))
2013-12-16 22:45:04 -08:00
else:
raise Exception("Failed")
###
2013-12-16 22:29:52 -08:00
try:
2013-12-16 22:45:04 -08:00
2013-12-16 22:29:52 -08:00
guids = [line.strip() for line in open(GUID_FILE)]
conf = [line.strip() for line in open(FEEDS_FILE)]
2013-12-16 22:29:52 -08:00
with open(API_FILE) as f:
apikey = f.read().strip()
2013-12-20 10:22:59 -08:00
feeds = conf
2013-12-16 22:29:52 -08:00
2013-12-16 22:45:04 -08:00
##
2013-12-16 22:29:52 -08:00
fo = open(GUID_FILE, "a")
for feed in feeds:
2014-02-06 21:53:48 -08:00
f = feed.split(' ', 1)
2014-03-17 14:24:26 -07:00
print("-- Parsing {0}".format(f[0]))
2014-02-06 21:53:48 -08:00
xml = feedparser.parse(f[0])
2013-12-16 22:29:52 -08:00
for e in xml.entries:
2014-03-17 14:24:26 -07:00
title = e.title.encode('utf-8')
try:
guid = e.guid
except Exception:
guid = e.link
if guid not in guids:
print("-- Looking")
2014-02-06 21:53:48 -08:00
if len(f) > 1 and not re.match(f[1], e.title):
2014-03-17 14:24:26 -07:00
print("-- Does not match! Ignoring")
2014-02-06 21:53:48 -08:00
continue
2014-03-17 14:24:26 -07:00
print("-- Found, reporting: {0}".format(title))
2013-12-16 22:29:52 -08:00
2014-03-17 14:24:26 -07:00
msg = "{0} - {1}".format(xml.feed.title.encode('utf-8'), title)
prowl_post(apikey, "RSS", title, msg)
2013-12-16 22:29:52 -08:00
2014-03-17 14:24:26 -07:00
print("-- Writing to guids")
fo.write("{0}\n".format(guid))
guids.append(guid)
print("Found: {0} => {1}".format(title, e.link))
2013-12-16 22:29:52 -08:00
else:
2014-03-17 14:24:26 -07:00
print("Ignored: {0} => {1}".format(title, e.link))
2013-12-16 22:29:52 -08:00
pass
fo.close()
2013-12-16 22:45:04 -08:00
except IOError, e:
print("{0} file couldn't be found/opened".format(e.filename))
sys.exit(1)
2013-12-16 22:29:52 -08:00
except Exception, msg:
2014-02-06 22:04:38 -08:00
print("Error: {0}".format(msg))