# HG changeset patch # User Goffi # Date 1322938324 -3600 # Node ID 48277946348b4b816309c8103550278a9b04dc08 # Parent 866dbb0d7d87aa1ed6a92b2f4f2a9aba29c60a2c plugin CS: use of PersistentBinaryData to store profile's information, and clean up on profile disconnection. /!\ The plugin doesn't work anymore, probably due to some change on CS website. diff -r 866dbb0d7d87 -r 48277946348b src/plugins/plugin_misc_cs.py --- a/src/plugins/plugin_misc_cs.py Sat Dec 03 15:50:09 2011 +0100 +++ b/src/plugins/plugin_misc_cs.py Sat Dec 03 19:52:04 2011 +0100 @@ -26,6 +26,7 @@ from twisted.words.protocols.jabber import error as jab_error from twisted.words.protocols.jabber.xmlstream import IQ from twisted.web.client import getPage +from sat.memory.persistent import PersistentBinaryDict import os.path import pdb @@ -73,10 +74,21 @@ host.memory.importParams(CS_Plugin.params) #menu host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing management interface")) - self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while + self.data={} #TODO: delete cookies/data after a while self.host.registerGeneralCB("plugin_CS_sendMessage", self.sendMessage) self.host.registerGeneralCB("plugin_CS_showUnreadMessages", self.showUnreadMessages) + def profileConnected(self, profile): + self.data[profile] = PersistentBinaryDict("plugin_CS", profile) + def dataLoaded(ignore): + if not self.data[profile]: + self.data[profile] = {'cookies':{}} + + self.data[profile].load().addCallback(dataLoaded) + + def profileDisconnected(self, profile): + del self.data[profile] + def erroCB(self, e, id): """Called when something is going wrong when contacting CS website""" #pdb.set_trace() @@ -94,10 +106,7 @@ post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) - if not self.data.has_key(profile): - self.data[profile] = {'cookies':{}} - else: - self.data[profile]['cookies'] = {} + self.data[profile]['cookies'] = {} d = getPage('http://www.couchsurfing.org/login.html', method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=self.data[profile]['cookies']) d.addCallback(self.__connectionCB, id, profile) @@ -194,8 +203,6 @@ else: #no, we show the result self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])}) - #and save the data - self.host.memory.setPrivate('plugin_cs_data', self.data) def __sendMessage(self, answer, subject, message, data, recipient_list, id, profile): """Send actually the message