From 39625632a10e12d7b079f8d4f04dc839cef839d9 Mon Sep 17 00:00:00 2001 From: Roger Philibert Date: Thu, 29 Mar 2012 23:10:01 +0200 Subject: [PATCH] [okc] Reply sending --- modules/okc/backend.py | 10 +++++++++- modules/okc/browser.py | 19 ++++++++++++++++++- modules/okc/pages.py | 26 ++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/modules/okc/backend.py b/modules/okc/backend.py index a1d94693..03a38694 100644 --- a/modules/okc/backend.py +++ b/modules/okc/backend.py @@ -218,8 +218,16 @@ class OkCBackend(BaseBackend, ICapMessages, ICapContact, ICapMessagesPost): # ---- ICapMessagesPost methods --------------------- def post_message(self, message): + content = message.content.replace('\n', '\r\n').encode('utf-8', 'replace') with self.browser: - self.browser.post_mail(message.thread.id, message.content) + # Check wether we already have a thread with this user + threads = self.browser.get_threads_list() + for thread in threads: + if thread['username'] == message.thread.id: + self.browser.post_reply(thread['id'], content) + break + else: + self.browser.post_mail(message.thread.id, content) # ---- ICapContact methods --------------------- diff --git a/modules/okc/browser.py b/modules/okc/browser.py index cec6b72f..061d9dc8 100644 --- a/modules/okc/browser.py +++ b/modules/okc/browser.py @@ -17,6 +17,7 @@ # You should have received a copy of the GNU Affero General Public License # along with weboob. If not, see . +import urllib from weboob.tools.browser import BaseBrowser from weboob.tools.ordereddict import OrderedDict @@ -144,9 +145,25 @@ class OkCBrowser(BaseBrowser): @check_login def post_mail(self, id, content): self.location(self.absurl('/messages?compose=1')) - content = content.replace('\n', '\r\n').encode('Windows-1252', 'replace') self.page.post_mail(id, content) + @check_login + def post_reply(self, thread_id, content): + self.location(self.absurl('/messages?readmsg=true&threadid=%s&folder=1' % thread_id)) + username, key = self.page.get_post_params() + data = urllib.urlencode({ + 'ajax' : 1, + 'sendmsg' : 1, + 'r1' : username, + 'subject' : '', + 'body' : content, + 'threadid' : thread_id, + 'authcode' : key, + 'reply' : 1, + }) + self.addheaders = [('Referer', self.page.url), ('Content-Type', 'application/x-www-form-urlencoded')] + self.open('http://m.okcupid.com/mailbox', data=data) + #@check_login #@url2id #def delete_thread(self, id): diff --git a/modules/okc/pages.py b/modules/okc/pages.py index e3a9500f..23f8f37c 100644 --- a/modules/okc/pages.py +++ b/modules/okc/pages.py @@ -17,6 +17,7 @@ # You should have received a copy of the GNU Affero General Public License # along with weboob. If not, see . +import re from weboob.tools.browser import BasePage from weboob.tools.ordereddict import OrderedDict @@ -67,6 +68,31 @@ class MessagesPage(BasePage): return mails + def get_post_params(self): + # http://m.okcupid.com/mailbox + # Paramètresapplication/x-www-form-urlencoded + # ajax 1 + # authcode 1,0,1332766806,0x154df106d5af5993;51e1fa019f3423831e5b95c9c91346e5138f99cf + # body Ah bah ça y'est, ça marche ?! + # r1 jeanneplop + # reply 1 + # sendmsg 1 + # subject + # threadid 154028265985080112 + # ajax=1&sendmsg=1&r1=jeanneplop&subject=&body=Ah%20bah%20%C3%A7a%20y'est%2C%20%C3%A7a%20marche%20%3F!&threadid=154028265985080112&authcode=1%2C0%2C1332766806%2C0x154df106d5af5993%3B51e1fa019f3423831e5b95c9c91346e5138f99cf&reply=1 + js = self.parser.select(self.document.getroot(), "//script", method='xpath') + for script in js: + script = script.text + + if script is None: + continue + for line in script.splitlines(): + match = re.match(".*Message\.initialize\([^,]*, '([^']*)', \"\", '([^']*)',.*", line) + if match is not None: + return match.groups() + raise Exception('Unexpected reply page') + + class ProfilePage(BasePage): def get_profile(self): title = self.parser.select(self.document.getroot(), 'title', 1)