diff src/plugins/plugin_xep_0277.py @ 302:9f3a6cf91668

plugin xep-0277: added getLastMicroblogs method
author Goffi <goffi@goffi.org>
date Mon, 21 Feb 2011 01:38:16 +0100
parents c5554e2939dd
children 2b52a5da0978
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0277.py	Mon Feb 21 01:36:52 2011 +0100
+++ b/src/plugins/plugin_xep_0277.py	Mon Feb 21 01:38:16 2011 +0100
@@ -39,7 +39,7 @@
 "import_name": "XEP-0277",
 "type": "XEP",
 "protocols": [],
-"dependencies": ["XEP-0163"],
+"dependencies": ["XEP-0163","XEP-0060"],
 "main": "XEP_0277",
 "handler": "no",
 "description": _("""Implementation of microblogging Protocol""")
@@ -51,38 +51,53 @@
         info(_("Microblogging plugin initialization"))
         self.host = host
         self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog)
+        host.bridge.addMethod("getLastMicroblogs", ".communication",
+                              in_sign='sis', out_sign='aa{ss}',
+                              method=self.getLastMicroblogs,
+                              async = True,
+                              doc = { 'summary':'retrieve items',
+                                      'param_0':'jid: publisher of wanted microblog',
+                                      'param_1':'max_items: see XEP-0060 #6.5.7',
+                                      'param_2':'%(doc_profile)s',
+                                      'return':'list of microblog data (dict)'
+                                    })
+
+    def _item2mbdata(self, item):
+        """Convert an XML Item to microblog data used in bridge API
+        @param item: domish.Element of microblog item
+        @return: microblog data (dictionary)"""
+        try:
+            entry_elt = filter (lambda x:x.name == "entry", item.children)[0]
+        except KeyError:
+            warning(_('No entry element in microblog item'))
+            return
+        _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8'))
+        microblog_data={}
+        try:
+            microblog_data['content'] = _entry.title.text
+            if len(_entry.authors):
+                microblog_data['author'] = _entry.authors[0].name.text
+            microblog_data['timestamp'] = str(int(_entry.updated.tf))
+            microblog_data['id'] = item['id']
+        except AttributeError, KeyError:
+            error(_('Error while parsing atom entry for microblogging event'))
+            return {}
+
+        ##XXX: workaround for Jappix behaviour
+        if not 'author' in microblog_data:
+            from xe import NestElement
+            try:
+                author=NestElement('author')
+                author.import_xml(str(_entry))
+                microblog_data['author'] = author.nick.text
+            except:
+                error(_('Cannot find author'))
+        ##end workaround Jappix
+        return microblog_data
 
     def microblogCB(self, itemsEvent, profile):
-        _entry = None
         for item in itemsEvent.items:
-            try:
-                entry_elt = filter (lambda x:x.name == "entry", item.children)[0]
-            except KeyError:
-                warning(_('No entry element in microblog item'))
-                return
-            _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8'))
-            microblog_data={}
-            try:
-                microblog_data['content'] = _entry.title.text
-                if len(_entry.authors):
-                    microblog_data['author'] = _entry.authors[0].name.text
-                microblog_data['timestamp'] = str(int(_entry.updated.tf))
-                microblog_data['id'] = item['id']
-            except AttributeError, KeyError:
-                error(_('Error while parsing atom entry for microblogging event'))
-                return
-
-            ##XXX: workaround for Jappix behaviour
-            if not 'author' in microblog_data:
-                from xe import NestElement
-                try:
-                    author=NestElement('author')
-                    author.import_xml(str(_entry))
-                    microblog_data['author'] = author.nick.text
-                except:
-                    error(_('Cannot find author'))
-            ##end workaround Jappix
-
+            microblog_data = self._item2mbdata(item)
             self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
 
     def sendMicroblog(self, data, profile):
@@ -109,3 +124,10 @@
         self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile)
         return 0
 
+    def getLastMicroblogs(self, pub_jid, max_items=1, profile_key='@DEFAULT@', callback=None, errback=None):
+        assert(callback)
+        d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
+        d.addCallbacks(lambda items: callback(map(self._item2mbdata, items)), errback)
+        
+
+