Mercurial > libervia-backend
view 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 source
#!/usr/bin/python # -*- coding: utf-8 -*- """ SAT plugin for microblogging over XMPP (xep-0277) Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ from logging import debug, info, error from twisted.internet import protocol from twisted.words.protocols.jabber import client, jid from twisted.words.protocols.jabber import error as jab_error import twisted.internet.error from twisted.words.xish import domish from sat.tools.xml_tools import ElementParser from wokkel import disco,pubsub from feed.atom import Entry, Author import uuid from time import time NS_MICROBLOG = 'urn:xmpp:microblog:0' PLUGIN_INFO = { "name": "Microblogging over XMPP Plugin", "import_name": "XEP-0277", "type": "XEP", "protocols": [], "dependencies": ["XEP-0163","XEP-0060"], "main": "XEP_0277", "handler": "no", "description": _("""Implementation of microblogging Protocol""") } class XEP_0277(): def __init__(self, host): 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): for item in itemsEvent.items: microblog_data = self._item2mbdata(item) self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile) def sendMicroblog(self, data, profile): """Send XEP-0277's microblog data @param data: must include content @param profile: profile which send the mood""" if not data.has_key('content'): error(_("Microblog data must contain at least 'content' key")) return 3 content = data['content'] if not content: error(_("Microblog data's content value must not be empty")) _uuid = unicode(uuid.uuid1()) _entry = Entry() #FIXME: need to escape html _entry.title = content.encode('utf-8') _entry.author = Author() _entry.author.name = data.get('author',self.host.getJidNStream(profile)[0].userhost()).encode('utf-8') _entry.updated = float(data.get('timestamp',time())) _entry.id = _uuid _entry_elt = ElementParser()(str(_entry)) item = pubsub.Item(payload=_entry_elt) item['id'] = _uuid 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)