comparison plugins/plugin_misc_cs.py @ 108:e24e080e6b16

CS plugin: unread messages can now be openned in external web browser
author Goffi <goffi@goffi.org>
date Mon, 28 Jun 2010 17:19:35 +0800
parents 5ae370c71803
children 18b0cf49a6f1
comparison
equal deleted inserted replaced
107:5ae370c71803 108:e24e080e6b16
33 from zope.interface import implements 33 from zope.interface import implements
34 34
35 from wokkel import disco, iwokkel, data_form 35 from wokkel import disco, iwokkel, data_form
36 from tools.xml_tools import XMLUI 36 from tools.xml_tools import XMLUI
37 import urllib 37 import urllib
38 import webbrowser
38 39
39 from BeautifulSoup import BeautifulSoup 40 from BeautifulSoup import BeautifulSoup
40 import re 41 import re
41 42
42 43
73 host.memory.importParams(CS_Plugin.params) 74 host.memory.importParams(CS_Plugin.params)
74 #menu 75 #menu
75 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface")) 76 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface"))
76 self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while 77 self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while
77 self.host.registerGeneralCB("plugin_CS_sendMessage", self.sendMessage) 78 self.host.registerGeneralCB("plugin_CS_sendMessage", self.sendMessage)
79 self.host.registerGeneralCB("plugin_CS_showUnreadMessages", self.showUnreadMessages)
78 80
79 def erroCB(self, e, id): 81 def erroCB(self, e, id):
80 """Called when something is going wrong when contacting CS website""" 82 """Called when something is going wrong when contacting CS website"""
81 #pdb.set_trace() 83 #pdb.set_trace()
82 message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")} 84 message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")}
162 friends_list = data['friends'].keys() 164 friends_list = data['friends'].keys()
163 friends_list.sort() 165 friends_list.sort()
164 interface = XMLUI('window','tabs', title='CouchSurfing management') 166 interface = XMLUI('window','tabs', title='CouchSurfing management')
165 interface.addCategory(_("Messages"), "vertical") 167 interface.addCategory(_("Messages"), "vertical")
166 interface.addText(_("G'day %(name)s, you have %(nb_message)i unread message%(plural_mess)s and %(unread_CR_mess)s unread couch request message%(plural_CR)s\nIf you want to send a message, select the recipient(s) in the list below") % {'name':user_name, 'nb_message':unread_mess, 'plural_mess':'s' if unread_mess>1 else '', 'unread_CR_mess': unread_CR_mess, 'plural_CR':'s' if unread_CR_mess>1 else ''}) 168 interface.addText(_("G'day %(name)s, you have %(nb_message)i unread message%(plural_mess)s and %(unread_CR_mess)s unread couch request message%(plural_CR)s\nIf you want to send a message, select the recipient(s) in the list below") % {'name':user_name, 'nb_message':unread_mess, 'plural_mess':'s' if unread_mess>1 else '', 'unread_CR_mess': unread_CR_mess, 'plural_CR':'s' if unread_CR_mess>1 else ''})
169 if unread_mess:
170 interface.addButton('plugin_CS_showUnreadMessages', 'showUnreadMessages', _('Show unread message%(plural)s in external web browser') % {'plural':'s' if unread_mess>1 else ''})
167 interface.addList(friends_list, 'friends', style=['multi']) 171 interface.addList(friends_list, 'friends', style=['multi'])
168 interface.changeLayout('pairs') 172 interface.changeLayout('pairs')
169 interface.addLabel(_("Subject")) 173 interface.addLabel(_("Subject"))
170 interface.addString('subject') 174 interface.addString('subject')
171 interface.changeLayout('vertical') 175 interface.changeLayout('vertical')
238 else: 242 else:
239 interface = XMLUI('window', title=_('Message sent')) #TODO: create particular actionResult for alerts ? 243 interface = XMLUI('window', title=_('Message sent')) #TODO: create particular actionResult for alerts ?
240 interface.addText(_('The message has been sent to every recipients')) 244 interface.addText(_('The message has been sent to every recipients'))
241 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":interface.toXml()}) 245 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":interface.toXml()})
242 246
243
244
245
246
247 def sendMessage(self, id, data, profile): 247 def sendMessage(self, id, data, profile):
248 """Called to send a message to a friend 248 """Called to send a message to a friend
249 @param data: dict with the following keys: 249 @param data: dict with the following keys:
250 friend: name of the recipient 250 friend: name of the recipient
251 subject: subject of the message 251 subject: subject of the message
261 subject = data['subject'] 261 subject = data['subject']
262 message = data['message'] 262 message = data['message']
263 print "send message \o/ :) :) :)" 263 print "send message \o/ :) :) :)"
264 info(_("sending message to %(friends)s with subject [%(subject)s]" % {'friends':friends, 'subject':subject})) 264 info(_("sending message to %(friends)s with subject [%(subject)s]" % {'friends':friends, 'subject':subject}))
265 self.__sendMessage(None, subject, message, self.data[profile], friends, id, profile) 265 self.__sendMessage(None, subject, message, self.data[profile], friends, id, profile)
266 266
267 def __showUnreadMessages2(self, html, id, profile):
268 """Called when the inbox page has been received"""
269 #FIXME: that's really too fragile, only works if the unread messages are in the first page, and it would be too resources consuming for the website to DL each time all pages. In addition, the show attribute doesn't work as expected.
270 soup = BeautifulSoup(html)
271 for tag in soup.findAll(lambda tag: tag.name=='strong' and tag.a and tag.a['href'].startswith('messages.html?message_status=inbox')):
272 link = "http://www.couchsurfing.org/"+str(tag.a['href'])
273 webbrowser.open(link, new=2) #TODO: the web browser need to already have CS cookies (i.e. already be opened & logged on CS, or be permanently loggued), a warning to the user should be sent
274
275 def showUnreadMessages(self, id, data, profile):
276 """Called when user want to see all unread messages in the external browser"""
277 d = getPage("http://www.couchsurfing.org/messages.html?message_status=inbox&show=10000", agent=AGENT, cookies=self.data[profile]['cookies'])
278 d.addCallback(self.__showUnreadMessages2, id, profile)
279 d.addErrback(self.erroCB, id)
280