annotate plugins/plugin_misc_cs.py @ 103:6be927a465ed

XMLUI refactoring, step 1
author Goffi <goffi@goffi.org>
date Wed, 23 Jun 2010 00:23:26 +0800
parents 94011f553cd0
children 138d82f64b6f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
101
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT plugin for managing xep-0045
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, warning, error
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import protocol, defer, threads, reactor
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import client, jid, xmlstream
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import error as jab_error
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.xmlstream import IQ
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.web.client import getPage
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 import os.path
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import pdb
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 import random
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from zope.interface import implements
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from wokkel import disco, iwokkel, data_form
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
36 from tools.xml_tools import dataForm2xml
101
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 import urllib
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
39 from BeautifulSoup import BeautifulSoup
101
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 PLUGIN_INFO = {
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "name": "CouchSurfing plugin",
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "import_name": "CS",
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "type": "Misc",
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "protocols": [],
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "dependencies": [],
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "main": "CS_Plugin",
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "handler": "no",
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "description": _(u"""This plugin allow to manage your CouchSurfing account throught your SàT frontend""")
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 }
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.3) Gecko/20100423 Ubuntu/10.04 (lucid) Firefox/3.6.3'
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 class CS_Plugin():
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 params = """
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 <params>
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 <individual>
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 <category name="CouchSurfing">
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 <param name="Login" type="string" />
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 <param name="Password" type="password" />
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 </category>
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
65 </individual>
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
66 </params>
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
67 """
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69 def __init__(self, host):
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 info(_("Plugin CS initialization"))
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.host = host
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 #parameters
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 host.memory.importParams(CS_Plugin.params)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74 #menu
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface"))
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
76 self.data={} #TODO: delete cookies/data after a while
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
77
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
78 def erroCB(self, e, id):
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
79 """Called when something is going wrong when contacting CS website"""
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
80 message_data={"reason": "connection error", "message":_(u"Impossible to contact CS website, please check your login/password, connection or try again later")}
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
81 self.host.bridge.actionResult("ERROR", id, message_data)
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
82
101
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 def menuSelected(self, id, profile):
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 """Called when the couchsurfing menu item is selected"""
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 if not login or not password:
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
89 message_data={"reason": "uncomplete", "message":_(u"You have to fill your CouchSurfing login & password in parameters before using this interface")}
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.host.bridge.actionResult("ERROR", id, message_data)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91 return
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
93 post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'})
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
94
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
95 self.data[profile] = {'cookies':{}}
101
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def connectionCB(html):
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 print 'Response received'
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
99 soup = self.data[profile]['soup'] = BeautifulSoup(html)
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
100 user_nick = soup.find('a','item_link',href='/home.html').contents[0]
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
101 user_name = soup.html.head.title.string.split(' - ')[1]
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
102 unread_messages = int(soup.find('div','item_bubble').a.string)
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
103 form_xml = """
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
104 <form>
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
105 <elem type='text' value='Welcome %(name)s, you have %(nb_message)i unread messages'/>
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
106 </form>
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
107 """ % {'name':user_name, 'nb_message':unread_messages}
103
6be927a465ed XMLUI refactoring, step 1
Goffi <goffi@goffi.org>
parents: 102
diff changeset
108 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":form_xml})
101
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
112 #d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=self.session_cookies[profile])
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
113
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
114 #tmp
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
115 f = open('/home/goffi/tmp/CS_principale.html','r')
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
116 html = f.read()
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
117 connectionCB(html)
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
118
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
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'])
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
120 d.addCallback(connectionCB)
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
121 d.addErrback(self.erroCB, id)"""
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
122
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
123
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
124 #self.host.bridge.actionResult("SUPPRESS", id, {})
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
125
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
126
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
127