# License: GPLv3 or later
from twisted.web.client import getPage
-from xml.etree.cElementTree import fromstring
+from xml.etree.cElementTree import fromstring, ParseError
+
+import os
class AtomFeed:
""" Simple XML parser that extracts pages from a atom feed """
ns = "{http://www.w3.org/2005/Atom}"
+ errorCallback = None
+
def __init__(self, url, callback, errorCallback):
""" Fetches the URL
Parsed pages are sent back to callback by parse()
"""
- getPage(url).addCallback(self.parse, callback).addErrback(errorCallback)
+ self.errorCallback = errorCallback
+ if (url.startswith('file://')):
+ file = url[7:]
+ if not os.path.exists(file):
+ errorCallback('Settings atom feed file does not exist: ' + file)
+ return
+
+ with open(file, 'r') as f:
+ self.parse(f.read(), callback)
+ else:
+ getPage(url).addCallback(self.parse, callback).addErrback(self.onError)
def parse(self, data, callback):
""" Parse atom feed data into pages list and run callback """
- xml = fromstring(data)
+ try:
+ xml = fromstring(data)
+ except ParseError:
+ return self.errorCallback("Invalid XML")
+
pages = []
for entry in xml.findall("{0}entry".format(self.ns)):
titleE = entry.find("{0}title".format(self.ns))
elif type == "":
return 2
return 1
+
+ def onError(self, error):
+ """ Pass the error message only """
+ self.errorCallback(error.getErrorMessage())