Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
101:783e9d6980ec | 102:94011f553cd0 |
---|---|
24 from twisted.internet import protocol, defer, threads, reactor | 24 from twisted.internet import protocol, defer, threads, reactor |
25 from twisted.words.protocols.jabber import client, jid, xmlstream | 25 from twisted.words.protocols.jabber import client, jid, xmlstream |
26 from twisted.words.protocols.jabber import error as jab_error | 26 from twisted.words.protocols.jabber import error as jab_error |
27 from twisted.words.protocols.jabber.xmlstream import IQ | 27 from twisted.words.protocols.jabber.xmlstream import IQ |
28 from twisted.web.client import getPage | 28 from twisted.web.client import getPage |
29 #from twisted.web.http_headers import Headers | |
30 import os.path | 29 import os.path |
31 import pdb | 30 import pdb |
32 import random | 31 import random |
33 | 32 |
34 from zope.interface import implements | 33 from zope.interface import implements |
35 | 34 |
36 from wokkel import disco, iwokkel, data_form | 35 from wokkel import disco, iwokkel, data_form |
37 from tools.xml_tools import XMLTools | 36 from tools.xml_tools import dataForm2xml |
38 #from twisted.web.iweb import IBodyProducer | |
39 import urllib | 37 import urllib |
40 | 38 |
39 from BeautifulSoup import BeautifulSoup | |
41 | 40 |
42 | 41 |
43 | 42 |
44 PLUGIN_INFO = { | 43 PLUGIN_INFO = { |
45 "name": "CouchSurfing plugin", | 44 "name": "CouchSurfing plugin", |
72 self.host = host | 71 self.host = host |
73 #parameters | 72 #parameters |
74 host.memory.importParams(CS_Plugin.params) | 73 host.memory.importParams(CS_Plugin.params) |
75 #menu | 74 #menu |
76 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface")) | 75 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface")) |
77 self.session_cookies={} #TODO: delete cookies after a while | 76 self.data={} #TODO: delete cookies/data after a while |
77 | |
78 def erroCB(self, e, id): | |
79 """Called when something is going wrong when contacting CS website""" | |
80 message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")} | |
81 self.host.bridge.actionResult("ERROR", id, message_data) | |
82 | |
78 | 83 |
79 def menuSelected(self, id, profile): | 84 def menuSelected(self, id, profile): |
80 """Called when the couchsurfing menu item is selected""" | 85 """Called when the couchsurfing menu item is selected""" |
81 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile) | 86 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile) |
82 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile) | 87 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile) |
85 self.host.bridge.actionResult("ERROR", id, message_data) | 90 self.host.bridge.actionResult("ERROR", id, message_data) |
86 return | 91 return |
87 | 92 |
88 post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) | 93 post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) |
89 | 94 |
90 cookies = {} | 95 self.data[profile] = {'cookies':{}} |
91 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) | |
92 #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) | |
93 | |
94 | 96 |
95 def connectionCB(html): | 97 def connectionCB(html): |
96 print 'Response received' | 98 print 'Response received' |
97 | 99 soup = self.data[profile]['soup'] = BeautifulSoup(html) |
98 d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=cookies) | 100 user_nick = soup.find('a','item_link',href='/home.html').contents[0] |
99 #d = getPage('file:///home/goffi/tmp/CS_inbox.html', agent=AGENT, cookies=cookies) | 101 user_name = soup.html.head.title.string.split(' - ')[1] |
100 def toto(html): | 102 unread_messages = int(soup.find('div','item_bubble').a.string) |
101 print "cookies:",cookies | 103 form_xml = """ |
102 pdb.set_trace() | 104 <form> |
103 d.addBoth(toto) | 105 <elem type='text' value='Welcome %(name)s, you have %(nb_message)i unread messages'/> |
104 d.addCallback(connectionCB) | 106 </form> |
105 | 107 """ % {'name':user_name, 'nb_message':unread_messages} |
106 | 108 self.host.bridge.actionResult("FORM", id, {"type":"window", "xml":form_xml}) |
107 self.host.bridge.actionResult("SUPPRESS", id, {}) | |
108 | 109 |
109 | 110 |
110 | 111 |
112 #d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=self.session_cookies[profile]) | |
113 | |
114 #tmp | |
115 f = open('/home/goffi/tmp/CS_principale.html','r') | |
116 html = f.read() | |
117 connectionCB(html) | |
118 | |
119 """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']) | |
120 d.addCallback(connectionCB) | |
121 d.addErrback(self.erroCB, id)""" | |
122 | |
123 | |
124 #self.host.bridge.actionResult("SUPPRESS", id, {}) | |
125 | |
126 | |
127 |