Mercurial > libervia-backend
comparison plugins/plugin_misc_cs.py @ 101:783e9d6980ec
Couchsurfing plugin: first draft
SàT core: adding additionnal menu method
bridge: new methods getMenus, getMenuHelp and callMenu
wix: new menu are added on startup
CS plugin: login on CS
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 19 Jun 2010 17:15:30 +0800 |
parents | |
children | 94011f553cd0 |
comparison
equal
deleted
inserted
replaced
100:50f1591c8fc6 | 101:783e9d6980ec |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for managing xep-0045 | |
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org) | |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from logging import debug, info, warning, error | |
23 from twisted.words.xish import domish | |
24 from twisted.internet import protocol, defer, threads, reactor | |
25 from twisted.words.protocols.jabber import client, jid, xmlstream | |
26 from twisted.words.protocols.jabber import error as jab_error | |
27 from twisted.words.protocols.jabber.xmlstream import IQ | |
28 from twisted.web.client import getPage | |
29 #from twisted.web.http_headers import Headers | |
30 import os.path | |
31 import pdb | |
32 import random | |
33 | |
34 from zope.interface import implements | |
35 | |
36 from wokkel import disco, iwokkel, data_form | |
37 from tools.xml_tools import XMLTools | |
38 #from twisted.web.iweb import IBodyProducer | |
39 import urllib | |
40 | |
41 | |
42 | |
43 | |
44 PLUGIN_INFO = { | |
45 "name": "CouchSurfing plugin", | |
46 "import_name": "CS", | |
47 "type": "Misc", | |
48 "protocols": [], | |
49 "dependencies": [], | |
50 "main": "CS_Plugin", | |
51 "handler": "no", | |
52 "description": _(u"""This plugin allow to manage your CouchSurfing account throught your SàT frontend""") | |
53 } | |
54 | |
55 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' | |
56 | |
57 class CS_Plugin(): | |
58 | |
59 params = """ | |
60 <params> | |
61 <individual> | |
62 <category name="CouchSurfing"> | |
63 <param name="Login" type="string" /> | |
64 <param name="Password" type="password" /> | |
65 </category> | |
66 </individual> | |
67 </params> | |
68 """ | |
69 | |
70 def __init__(self, host): | |
71 info(_("Plugin CS initialization")) | |
72 self.host = host | |
73 #parameters | |
74 host.memory.importParams(CS_Plugin.params) | |
75 #menu | |
76 host.importMenu(_("Plugin"), "CouchSurfing", self.menuSelected, help_string = _("Launch CoushSurfing mangement interface")) | |
77 self.session_cookies={} #TODO: delete cookies after a while | |
78 | |
79 def menuSelected(self, id, profile): | |
80 """Called when the couchsurfing menu item is selected""" | |
81 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile) | |
82 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile) | |
83 if not login or not password: | |
84 message_data={"reason": "uncomplete", "message":_(u"You have to fill your CouchSurfing login & password in parameters before using this interface")} | |
85 self.host.bridge.actionResult("ERROR", id, message_data) | |
86 return | |
87 | |
88 post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) | |
89 | |
90 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 | |
95 def connectionCB(html): | |
96 print 'Response received' | |
97 | |
98 d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=cookies) | |
99 #d = getPage('file:///home/goffi/tmp/CS_inbox.html', agent=AGENT, cookies=cookies) | |
100 def toto(html): | |
101 print "cookies:",cookies | |
102 pdb.set_trace() | |
103 d.addBoth(toto) | |
104 d.addCallback(connectionCB) | |
105 | |
106 | |
107 self.host.bridge.actionResult("SUPPRESS", id, {}) | |
108 | |
109 | |
110 |