diff plugins/plugin_misc_cs.py @ 102:94011f553cd0

misc bugfixes - wix: added forgotten profile in gateways management - xml_tools: removed XMLClass to the 2 methods as a direct methods - plugin_xep_100 (gateways discovery): added error callback
author Goffi <goffi@goffi.org>
date Tue, 22 Jun 2010 13:58:53 +0800
parents 783e9d6980ec
children 6be927a465ed
line wrap: on
line diff
--- a/plugins/plugin_misc_cs.py	Sat Jun 19 17:15:30 2010 +0800
+++ b/plugins/plugin_misc_cs.py	Tue Jun 22 13:58:53 2010 +0800
@@ -26,7 +26,6 @@
 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 twisted.web.http_headers import Headers
 import os.path
 import pdb
 import random
@@ -34,10 +33,10 @@
 from zope.interface import implements
 
 from wokkel import disco, iwokkel, data_form
-from tools.xml_tools import XMLTools
-#from twisted.web.iweb import IBodyProducer
+from tools.xml_tools import dataForm2xml
 import urllib
 
+from BeautifulSoup import BeautifulSoup
 
 
 
@@ -74,7 +73,13 @@
         host.memory.importParams(CS_Plugin.params)
         #menu
         host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface"))
-        self.session_cookies={} #TODO: delete cookies after a while
+        self.data={} #TODO: delete cookies/data after a while
+
+    def erroCB(self, e, id):
+        """Called when something is going wrong when contacting CS website"""
+        message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")}
+        self.host.bridge.actionResult("ERROR", id, message_data)
+
 
     def menuSelected(self, id, profile):
         """Called when the couchsurfing menu item is selected"""
@@ -87,24 +92,36 @@
 
         post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) 
         
-        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=cookies)
-        #d = getPage('file:///home/goffi/tmp/CS_principale.html', method='POST', postdata=post_data, headers={'Content-Type':'application/x-www-form-urlencoded'} , agent=AGENT, cookies=cookies)
-        
+        self.data[profile] = {'cookies':{}}
 
         def connectionCB(html):
             print 'Response received'
-
-            d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=cookies)
-            #d = getPage('file:///home/goffi/tmp/CS_inbox.html', agent=AGENT, cookies=cookies)
-            def toto(html):
-                print "cookies:",cookies
-                pdb.set_trace()
-            d.addBoth(toto)
-        d.addCallback(connectionCB)
-
-
-        self.host.bridge.actionResult("SUPPRESS", id, {})
+            soup = self.data[profile]['soup'] = BeautifulSoup(html)
+            user_nick = soup.find('a','item_link',href='/home.html').contents[0]
+            user_name = soup.html.head.title.string.split(' - ')[1]
+            unread_messages = int(soup.find('div','item_bubble').a.string)
+            form_xml = """
+            <form>
+                <elem type='text' value='Welcome %(name)s, you have %(nb_message)i unread messages'/>
+            </form>
+            """ % {'name':user_name, 'nb_message':unread_messages}
+            self.host.bridge.actionResult("FORM", id, {"type":"window", "xml":form_xml})
 
 
 
+            #d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=self.session_cookies[profile])
+       
+        #tmp
+        f = open('/home/goffi/tmp/CS_principale.html','r')
+        html = f.read()
+        connectionCB(html)
+
+        """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(connectionCB)
+        d.addErrback(self.erroCB, id)"""
+
+
+        #self.host.bridge.actionResult("SUPPRESS", id, {})
+
+
+