1 # -*- coding: utf-8 -*-
2 # CurlyTx Atom feed parser
3 # Copyright (C) 2011 Christian Weiske <cweiske@cweiske.de>
4 # License: GPLv3 or later
6 from twisted.web.client import getPage
7 from xml.etree.cElementTree import fromstring
10 """ Simple XML parser that extracts pages from a atom feed """
11 ns = "{http://www.w3.org/2005/Atom}"
12 def __init__(self, url, callback, errorCallback):
15 Parsed pages are sent back to callback by parse()
17 getPage(url).addCallback(self.parse, callback).addErrback(errorCallback)
19 def parse(self, data, callback):
20 """ Parse atom feed data into pages list and run callback """
21 xml = fromstring(data)
23 for entry in xml.findall("{0}entry".format(self.ns)):
24 titleE = entry.find("{0}title".format(self.ns))
25 url = self.bestLink(entry.findall("{0}link".format(self.ns)))
26 if titleE != None and titleE.text != "" and url != None:
27 pages.append({"title": titleE.text, "url": url})
31 def bestLink(self, list):
32 """ Fetch the best matching link from an atom feed entry """
36 if link.get("rel") != "alternate" and link.get("rel") != "":
38 level = self.level(link)
39 if foundLevel > level:
42 foundHref = link.get("href")
45 def level(self, link):
46 """ Determines the level of a link
48 "text/plain" type links are best, links without type are second.
49 All others have the lowest level 1.
51 type = link.get("type")
52 if type == "text/plain":