comparison 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
comparison
equal deleted inserted replaced
105:d2630fba8dfd 106:138d82f64b6f
31 import random 31 import random
32 32
33 from zope.interface import implements 33 from zope.interface import implements
34 34
35 from wokkel import disco, iwokkel, data_form 35 from wokkel import disco, iwokkel, data_form
36 from tools.xml_tools import dataForm2xml 36 from tools.xml_tools import XMLUI
37 import urllib 37 import urllib
38 38
39 from BeautifulSoup import BeautifulSoup 39 from BeautifulSoup import BeautifulSoup
40 40 import re
41 41
42 42
43 PLUGIN_INFO = { 43 PLUGIN_INFO = {
44 "name": "CouchSurfing plugin", 44 "name": "CouchSurfing plugin",
45 "import_name": "CS", 45 "import_name": "CS",
71 self.host = host 71 self.host = host
72 #parameters 72 #parameters
73 host.memory.importParams(CS_Plugin.params) 73 host.memory.importParams(CS_Plugin.params)
74 #menu 74 #menu
75 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"))
76 self.data={} #TODO: delete cookies/data after a while 76 self.data=self.host.memory.getPrivate('plugin_cs_data') or {} #TODO: delete cookies/data after a while
77 77
78 def erroCB(self, e, id): 78 def erroCB(self, e, id):
79 """Called when something is going wrong when contacting CS website""" 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")} 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) 81 self.host.bridge.actionResult("ERROR", id, message_data)
82
83 82
84 def menuSelected(self, id, profile): 83 def menuSelected(self, id, profile):
85 """Called when the couchsurfing menu item is selected""" 84 """Called when the couchsurfing menu item is selected"""
86 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile) 85 login = self.host.memory.getParamA("Login", "CouchSurfing", profile_key=profile)
87 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile) 86 password = self.host.memory.getParamA("Password", "CouchSurfing", profile_key=profile)
90 self.host.bridge.actionResult("ERROR", id, message_data) 89 self.host.bridge.actionResult("ERROR", id, message_data)
91 return 90 return
92 91
93 post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'}) 92 post_data = urllib.urlencode({'auth_login[un]':login,'auth_login[pw]':password,'auth_login[action]':'Login...'})
94 93
95 self.data[profile] = {'cookies':{}} 94 if not self.data.has_key(profile):
96 95 self.data[profile] = {'cookies':{}}
97 def connectionCB(html): 96 else:
98 print 'Response received' 97 self.data[profile]['cookies'] = {}
99 soup = self.data[profile]['soup'] = BeautifulSoup(html) 98
100 user_nick = soup.find('a','item_link',href='/home.html').contents[0] 99
101 user_name = soup.html.head.title.string.split(' - ')[1] 100
102 unread_messages = int(soup.find('div','item_bubble').a.string) 101
103 form_xml = """ 102
104 <form>
105 <elem type='text' value='Welcome %(name)s, you have %(nb_message)i unread messages'/>
106 </form>
107 """ % {'name':user_name, 'nb_message':unread_messages}
108 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":form_xml})
109
110
111
112 #d = getPage('http://www.couchsurfing.org/messages.html?message_status=inbox', agent=AGENT, cookies=self.session_cookies[profile])
113
114 #tmp 103 #tmp
115 f = open('/home/goffi/tmp/CS_principale.html','r') 104 f = open('/home/goffi/tmp/CS_principale.html','r')
116 html = f.read() 105 html = f.read()
117 connectionCB(html) 106 self.__connectionCB(html, id, profile)
118 107
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']) 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'])
120 d.addCallback(connectionCB) 109 d.addCallback(self.__connectionCB, id, profile)
121 d.addErrback(self.erroCB, id)""" 110 d.addErrback(self.erroCB, id)"""
122 111
123 112
124 #self.host.bridge.actionResult("SUPPRESS", id, {}) 113 #self.host.bridge.actionResult("SUPPRESS", id, {})
125 114
126 115
127 116 #pages parsing callbacks
117 def savePage(self, name, html):
118 f = open ('/home/goffi/tmp/CS_'+name+'.html','w')
119 f.write(html)
120 f.close()
121 print "page [%s] sauvee" % name
122 #pdb.set_trace()
123
124 def __connectionCB(self, html, id, profile):
125 print 'Response received'
126 self.savePage('principale',html)
127 soup = BeautifulSoup(html)
128 self.data[profile]['user_nick'] = soup.find('a','item_link',href='/home.html').contents[0]
129 self.data[profile]['user_name'] = soup.html.head.title.string.split(' - ')[1]
130 #unread messages
131 try:
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))
133 except:
134 self.data[profile]['unread_messages'] = 0
135 #unread couchrequest messages
136 try:
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))
138 except:
139 self.data[profile]['unread_CR_messages'] = 0
140
141 #if we have already the list of friend, no need to make new requests
142 if not self.data[profile].has_key('friends'):
143 self.data[profile]['friends'] = {}
144 """f = open('/home/goffi/tmp/CS_friends.html','r')
145 html = f.read()
146 self.__friendsPageCB(html, id, profile)"""
147 d = getPage('http://www.couchsurfing.org/connections.html?type=myfriends&show=10000', agent=AGENT, cookies=self.data[profile]['cookies'])
148 d.addCallback(self.__friendsPageCB, id=id, profile=profile)
149 d.addErrback(self.erroCB, id)
150 else:
151 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])})
152
153 def __buildUI(self, data):
154 """Build the XML UI of the plugin
155 @param data: data store for the profile"""
156 user_nick = data['user_nick']
157 user_name = data['user_name']
158 unread_mess = data['unread_messages']
159 unread_CR_mess = data['unread_CR_messages']
160 friends_list = data['friends'].keys()
161 friends_list.sort()
162 interface = XMLUI('window','tabs')
163 interface.addCategory(_("Messages"), "vertical")
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 ''})
165 interface.addList(friends_list, 'friends')
166 interface.addCategory(_("Events"), "vertical")
167 interface.addCategory(_("Couch search"), "vertical")
168 return interface.toXml()
169
170 def __meetingPageCB(self, html):
171 """Called when the meeting page has been received"""
172
173 def __friendsPageCB(self, html, id, profile):
174 """Called when the friends list page has been received"""
175 self.savePage('friends',html)
176 soup = BeautifulSoup(html.replace('"formtable width="400','"formtable" width="400"'))
177 friends = self.data[profile]['friends']
178 for _tr in soup.findAll('tr', {'class':re.compile("^msgRow*")}): #we parse the row with friends infos
179 _nobr = _tr.find('nobr') #contain the friend name
180 friend_name = unicode(_nobr.string)
181 friend_link = u'http://www.couchsurfing.org'+_nobr.parent['href']
182 regex_href = re.compile(r'/connections\.html\?id=([^&]+)')
183 a_tag = _tr.find('a',href=regex_href)
184 friend_id = regex_href.search(unicode(a_tag)).groups()[0]
185
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})
187 friends[friend_name] = {'link':friend_link,'id':friend_id}
188 a = soup.find('td','barmiddle next').a #is there several pages ?
189 if a:
190 #yes, we parse the next page
191 d = getPage('http://www.couchsurfing.org/'+str(a['href']), agent=AGENT, cookies=self.data[profile]['cookies'])
192 d.addCallback(self.__friendsPageCB, id=id, profile=profile)
193 d.addErrback(self.erroCB, id)
194 else:
195 #no, we show the result
196 self.host.bridge.actionResult("XMLUI", id, {"type":"window", "xml":self.__buildUI(self.data[profile])})
197 #and save the data
198 self.host.memory.setPrivate('plugin_cs_data', self.data)
199
200
201
202
203