comparison 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
comparison
equal deleted inserted replaced
301:e33b3a777f10 302:9f3a6cf91668
37 PLUGIN_INFO = { 37 PLUGIN_INFO = {
38 "name": "Microblogging over XMPP Plugin", 38 "name": "Microblogging over XMPP Plugin",
39 "import_name": "XEP-0277", 39 "import_name": "XEP-0277",
40 "type": "XEP", 40 "type": "XEP",
41 "protocols": [], 41 "protocols": [],
42 "dependencies": ["XEP-0163"], 42 "dependencies": ["XEP-0163","XEP-0060"],
43 "main": "XEP_0277", 43 "main": "XEP_0277",
44 "handler": "no", 44 "handler": "no",
45 "description": _("""Implementation of microblogging Protocol""") 45 "description": _("""Implementation of microblogging Protocol""")
46 } 46 }
47 47
49 49
50 def __init__(self, host): 50 def __init__(self, host):
51 info(_("Microblogging plugin initialization")) 51 info(_("Microblogging plugin initialization"))
52 self.host = host 52 self.host = host
53 self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog) 53 self.host.plugins["XEP-0163"].addPEPEvent("MICROBLOG", NS_MICROBLOG, self.microblogCB, self.sendMicroblog)
54 host.bridge.addMethod("getLastMicroblogs", ".communication",
55 in_sign='sis', out_sign='aa{ss}',
56 method=self.getLastMicroblogs,
57 async = True,
58 doc = { 'summary':'retrieve items',
59 'param_0':'jid: publisher of wanted microblog',
60 'param_1':'max_items: see XEP-0060 #6.5.7',
61 'param_2':'%(doc_profile)s',
62 'return':'list of microblog data (dict)'
63 })
64
65 def _item2mbdata(self, item):
66 """Convert an XML Item to microblog data used in bridge API
67 @param item: domish.Element of microblog item
68 @return: microblog data (dictionary)"""
69 try:
70 entry_elt = filter (lambda x:x.name == "entry", item.children)[0]
71 except KeyError:
72 warning(_('No entry element in microblog item'))
73 return
74 _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8'))
75 microblog_data={}
76 try:
77 microblog_data['content'] = _entry.title.text
78 if len(_entry.authors):
79 microblog_data['author'] = _entry.authors[0].name.text
80 microblog_data['timestamp'] = str(int(_entry.updated.tf))
81 microblog_data['id'] = item['id']
82 except AttributeError, KeyError:
83 error(_('Error while parsing atom entry for microblogging event'))
84 return {}
85
86 ##XXX: workaround for Jappix behaviour
87 if not 'author' in microblog_data:
88 from xe import NestElement
89 try:
90 author=NestElement('author')
91 author.import_xml(str(_entry))
92 microblog_data['author'] = author.nick.text
93 except:
94 error(_('Cannot find author'))
95 ##end workaround Jappix
96 return microblog_data
54 97
55 def microblogCB(self, itemsEvent, profile): 98 def microblogCB(self, itemsEvent, profile):
56 _entry = None
57 for item in itemsEvent.items: 99 for item in itemsEvent.items:
58 try: 100 microblog_data = self._item2mbdata(item)
59 entry_elt = filter (lambda x:x.name == "entry", item.children)[0]
60 except KeyError:
61 warning(_('No entry element in microblog item'))
62 return
63 _entry = Entry().import_xml(entry_elt.toXml().encode('utf-8'))
64 microblog_data={}
65 try:
66 microblog_data['content'] = _entry.title.text
67 if len(_entry.authors):
68 microblog_data['author'] = _entry.authors[0].name.text
69 microblog_data['timestamp'] = str(int(_entry.updated.tf))
70 microblog_data['id'] = item['id']
71 except AttributeError, KeyError:
72 error(_('Error while parsing atom entry for microblogging event'))
73 return
74
75 ##XXX: workaround for Jappix behaviour
76 if not 'author' in microblog_data:
77 from xe import NestElement
78 try:
79 author=NestElement('author')
80 author.import_xml(str(_entry))
81 microblog_data['author'] = author.nick.text
82 except:
83 error(_('Cannot find author'))
84 ##end workaround Jappix
85
86 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile) 101 self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
87 102
88 def sendMicroblog(self, data, profile): 103 def sendMicroblog(self, data, profile):
89 """Send XEP-0277's microblog data 104 """Send XEP-0277's microblog data
90 @param data: must include content 105 @param data: must include content
107 item = pubsub.Item(payload=_entry_elt) 122 item = pubsub.Item(payload=_entry_elt)
108 item['id'] = _uuid 123 item['id'] = _uuid
109 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile) 124 self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile)
110 return 0 125 return 0
111 126
127 def getLastMicroblogs(self, pub_jid, max_items=1, profile_key='@DEFAULT@', callback=None, errback=None):
128 assert(callback)
129 d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
130 d.addCallbacks(lambda items: callback(map(self._item2mbdata, items)), errback)
131
132
133