annotate plugins/plugin_misc_cs.py @ 106:138d82f64b6f

plugin CS: friends parsing
author Goffi <goffi@goffi.org>
date Sat, 26 Jun 2010 15:33:16 +0800
parents 6be927a465ed
children 5ae370c71803
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
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
36 from tools.xml_tools import XMLUI
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
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
40 import re
101
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"))
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
76 self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while
102
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 def menuSelected(self, id, profile):
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 """Called when the couchsurfing menu item is selected"""
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
85 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
86 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 if not login or not password:
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 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
89 self.host.bridge.actionResult("ERROR", id, message_data)
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
90 return
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
91
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
92 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
93
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
94 if not self.data.has_key(profile):
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
95 self.data[profile] = {'cookies':{}}
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
96 else:
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
97 self.data[profile]['cookies'] = {}
101
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
99
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100
783e9d6980ec Couchsurfing plugin: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
102
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
103 #tmp
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
104 f = open('/home/goffi/tmp/CS_principale.html','r')
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
105 html = f.read()
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
106 self.__connectionCB(html, id, profile)
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
107
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
108 """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'])
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
109 d.addCallback(self.__connectionCB, id, profile)
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
110 d.addErrback(self.erroCB, id)"""
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
111
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
112
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
113 #self.host.bridge.actionResult("SUPPRESS", id, {})
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
114
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
115
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
116 #pages parsing callbacks
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
117 def savePage(self, name, html):
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
118 f = open ('/home/goffi/tmp/CS_'+name+'.html','w')
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
119 f.write(html)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
120 f.close()
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
121 print "page [%s] sauvee" % name
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
122 #pdb.set_trace()
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
123
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
124 def __connectionCB(self, html, id, profile):
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
125 print 'Response received'
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
126 self.savePage('principale',html)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
127 soup = BeautifulSoup(html)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
128 self.data[profile]['user_nick'] = soup.find('a','item_link',href='/home.html').contents[0]
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
129 self.data[profile]['user_name'] = soup.html.head.title.string.split(' - ')[1]
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
130 #unread messages
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
131 try:
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
132 self.data[profile]['unread_messages'] = int(soup.find(lambda tag: tag.name=='div' and ('class','item_bubble') in tag.attrs and tag.find('a', href="/messages.html?message_status=inbox")).find(text=True))
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
133 except:
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
134 self.data[profile]['unread_messages'] = 0
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
135 #unread couchrequest messages
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
136 try:
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
137 self.data[profile]['unread_CR_messages'] = int(soup.find(lambda tag: tag.name=='div' and ('class','item_bubble') in tag.attrs and tag.find('a', href="/couchmanager")).find(text=True))
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
138 except:
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
139 self.data[profile]['unread_CR_messages'] = 0
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
140
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
141 #if we have already the list of friend, no need to make new requests
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
142 if not self.data[profile].has_key('friends'):
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
143 self.data[profile]['friends'] = {}
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
144 """f = open('/home/goffi/tmp/CS_friends.html','r')
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
145 html = f.read()
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
146 self.__friendsPageCB(html, id, profile)"""
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
147 d = getPage('http://www.couchsurfing.org/connections.html?type=myfriends&show=10000', agent=AGENT, cookies=self.data[profile]['cookies'])
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
148 d.addCallback(self.__friendsPageCB, id=id, profile=profile)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
149 d.addErrback(self.erroCB, id)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
150 else:
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
151 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])})
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
152
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
153 def __buildUI(self, data):
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
154 """Build the XML UI of the plugin
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
155 @param data: data store for the profile"""
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
156 user_nick = data['user_nick']
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
157 user_name = data['user_name']
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
158 unread_mess = data['unread_messages']
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
159 unread_CR_mess = data['unread_CR_messages']
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
160 friends_list = data['friends'].keys()
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
161 friends_list.sort()
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
162 interface = XMLUI('window','tabs')
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
163 interface.addCategory(_("Messages"), "vertical")
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
164 interface.addText(_("G'day %(name)s, you have %(nb_message)i unread message%(plural_mess)s and %(unread_CR_mess)s unread couch request message%(plural_CR)s") % {'name':user_name, 'nb_message':unread_mess, 'plural_mess':'s' if unread_mess>1 else '', 'unread_CR_mess': unread_CR_mess, 'plural_CR':'s' if unread_CR_mess>1 else ''})
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
165 interface.addList(friends_list, 'friends')
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
166 interface.addCategory(_("Events"), "vertical")
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
167 interface.addCategory(_("Couch search"), "vertical")
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
168 return interface.toXml()
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
169
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
170 def __meetingPageCB(self, html):
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
171 """Called when the meeting page has been received"""
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
172
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
173 def __friendsPageCB(self, html, id, profile):
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
174 """Called when the friends list page has been received"""
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
175 self.savePage('friends',html)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
176 soup = BeautifulSoup(html.replace('"formtable width="400','"formtable" width="400"'))
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
177 friends = self.data[profile]['friends']
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
178 for _tr in soup.findAll('tr', {'class':re.compile("^msgRow*")}): #we parse the row with friends infos
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
179 _nobr = _tr.find('nobr') #contain the friend name
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
180 friend_name = unicode(_nobr.string)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
181 friend_link = u'http://www.couchsurfing.org'+_nobr.parent['href']
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
182 regex_href = re.compile(r'/connections\.html\?id=([^&]+)')
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
183 a_tag = _tr.find('a',href=regex_href)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
184 friend_id = regex_href.search(unicode(a_tag)).groups()[0]
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
185
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
186 debug(_("CS friend found: %(friend_name)s (id: %(friend_id)s, link: %(friend_link)s)") % {'friend_name':friend_name, 'friend_id':friend_id, 'friend_link':friend_link})
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
187 friends[friend_name] = {'link':friend_link,'id':friend_id}
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
188 a = soup.find('td','barmiddle next').a #is there several pages ?
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
189 if a:
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
190 #yes, we parse the next page
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
191 d = getPage('http://www.couchsurfing.org/'+str(a['href']), agent=AGENT, cookies=self.data[profile]['cookies'])
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
192 d.addCallback(self.__friendsPageCB, id=id, profile=profile)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
193 d.addErrback(self.erroCB, id)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
194 else:
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
195 #no, we show the result
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
196 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])})
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
197 #and save the data
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
198 self.host.memory.setPrivate('plugin_cs_data', self.data)
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
199
102
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
200
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
201
94011f553cd0 misc bugfixes
Goffi <goffi@goffi.org>
parents: 101
diff changeset
202
106
138d82f64b6f plugin CS: friends parsing
Goffi <goffi@goffi.org>
parents: 103
diff changeset
203