first work on page import via atom feed
authorChristian Weiske <cweiske@cweiske.de>
Wed, 23 Nov 2011 05:39:54 +0000 (06:39 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 23 Nov 2011 05:39:54 +0000 (06:39 +0100)
src/AtomFeed.py [new file with mode: 0644]
src/CurlyTxSettings.py
src/config.py

diff --git a/src/AtomFeed.py b/src/AtomFeed.py
new file mode 100644 (file)
index 0000000..07a6345
--- /dev/null
@@ -0,0 +1,41 @@
+from twisted.web.client import getPage
+from xml.etree.cElementTree import fromstring
+
+class AtomFeed:
+    """Simple XML parser that extracts pages from a atom feed
+    """
+    ns = "{http://www.w3.org/2005/Atom}"
+    def __init__(self, url, callback):
+        getPage(url).addCallback(self.parse, callback)
+
+    def parse(self, data, callback):
+        xml = fromstring(data)
+        pages = []
+        for entry in xml.findall("{0}entry".format(self.ns)):
+            titleE = entry.find("{0}title".format(self.ns))
+            url   = self.bestLink(entry.findall("{0}link".format(self.ns)))
+            if titleE != None and titleE.text != "" and url != None:
+                pages.append({"title": titleE.text, "url": url})
+
+        callback(pages)
+
+    def bestLink(self, list):
+        foundLevel = -1
+        foundHref = None
+        for link in list:
+            if link.get("rel") != "alternate" and link.get("rel") != "":
+                continue
+            level = self.level(link)
+            if foundLevel > level:
+                continue
+            foundLevel = level
+            foundHref = link.get("href")
+        return foundHref
+
+    def level(self, link):
+        type = link.get("type")
+        if type == "text/plain":
+            return 3
+        elif type == "":
+            return 2
+        return 1
index 14e1242..686212c 100644 (file)
@@ -63,6 +63,7 @@ class CurlyTxSettings(ConfigListScreen, HelpableScreen, Screen):
         list.append(getConfigListEntry(_("Show in main menu"), config.plugins.CurlyTx.menuMain))
         list.append(getConfigListEntry(_("Show in extensions menu"), config.plugins.CurlyTx.menuExtensions))
         list.append(getConfigListEntry(_("Menu title"), config.plugins.CurlyTx.menuTitle))
+        list.append(getConfigListEntry(_("Page feed URL"), config.plugins.CurlyTx.feedUrl))
         return list
 
     def loadHelp(self):
@@ -89,7 +90,7 @@ class CurlyTxSettings(ConfigListScreen, HelpableScreen, Screen):
         ConfigListScreen.keyRight(self)
 
     def deletePage(self):
-        if len(config.plugins.CurlyTx.pages) == 0:
+        if len(config.plugins.CurlyTx.pages) == 0 or self["config"].getCurrentIndex() >= len(config.plugins.CurlyTx.pages):
             return
 
         from Screens.MessageBox import MessageBox
@@ -121,6 +122,9 @@ class CurlyTxSettings(ConfigListScreen, HelpableScreen, Screen):
                 self.pageEdited, CurlyTxPageEdit,
                 config.plugins.CurlyTx.pages[id], False
                 )
+        else:
+            from AtomFeed import AtomFeed
+            AtomFeed(config.plugins.CurlyTx.feedUrl.value, self.feedPagesReceived)
 
     def pageEdited(self, page, new):
         if not page:
@@ -131,6 +135,19 @@ class CurlyTxSettings(ConfigListScreen, HelpableScreen, Screen):
 
         self["config"].setList(self.getConfigList())
 
+    def feedPagesReceived(self, pages):
+        if len(pages) == 0:
+            return
+
+        del config.plugins.CurlyTx.pages[:]
+        config.plugins.CurlyTx.pages.save()
+        print("CurlyTx", len(config.plugins.CurlyTx.pages))
+        for pageData in pages:
+            page = createPage()
+            page.title.value = pageData["title"]
+            page.uri.value   = pageData["url"]
+            config.plugins.CurlyTx.pages.append(page)
+        self["config"].setList(self.getConfigList())
 
     def keySave(self):
         for i in range(0, len(config.plugins.CurlyTx.pages)):
index 5f4c5eb..4314f08 100644 (file)
@@ -24,6 +24,7 @@ config.plugins.CurlyTx = ConfigSubsection()
 config.plugins.CurlyTx.menuMain = ConfigYesNo(default = True)
 config.plugins.CurlyTx.menuExtensions = ConfigYesNo(default = False)
 config.plugins.CurlyTx.menuTitle = ConfigText(default = "CurlyTx", fixed_size = False)
+config.plugins.CurlyTx.feedUrl = ConfigText(default = "", fixed_size = False)
 config.plugins.CurlyTx.pages = ConfigSubList()
 for id,value in config.plugins.CurlyTx.pages.stored_values.iteritems():
     config.plugins.CurlyTx.pages.append(createPage())