diff --git a/modules/okc/backend.py b/modules/okc/backend.py index 2d7b8ace..0cd995db 100644 --- a/modules/okc/backend.py +++ b/modules/okc/backend.py @@ -28,7 +28,7 @@ from dateutil.parser import parse as _parse_dt from weboob.capabilities.base import NotLoaded from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread from weboob.capabilities.dating import ICapDating, OptimizationNotFound, Event -from weboob.capabilities.contact import ICapContact, ContactPhoto, Contact +from weboob.capabilities.contact import ICapContact, ContactPhoto, Contact, Query, QueryError from weboob.tools.backend import BaseBackend, BackendConfig from weboob.tools.value import Value, ValueBackendPassword from weboob.tools.misc import local2utc @@ -358,26 +358,26 @@ class OkCBackend(BaseBackend, ICapMessages, ICapContact, ICapMessagesPost, ICapD if c and (c.status & status) and (not ids or c.id in ids): yield c - #def send_query(self, id): - # if isinstance(id, Contact): - # id = id.id + def send_query(self, id): + if isinstance(id, Contact): + id = id.id - # queries_queue = None - # try: - # queries_queue = self.get_optimization('QUERIES_QUEUE') - # except OptimizationNotFound: - # pass + queries_queue = None + try: + queries_queue = self.get_optimization('QUERIES_QUEUE') + except OptimizationNotFound: + pass - # if queries_queue and queries_queue.is_running(): - # if queries_queue.enqueue_query(id): - # return Query(id, 'A charm has been sent') - # else: - # return Query(id, 'Unable to send charm: it has been enqueued') - # else: - # with self.browser: - # if not self.browser.send_charm(id): - # raise QueryError('No enough charms available') - # return Query(id, 'A charm has been sent') + if queries_queue and queries_queue.is_running(): + if queries_queue.enqueue_query(id): + return Query(id, 'A profile was visited') + else: + return Query(id, 'Unable to visit profile: it has been enqueued') + else: + with self.browser: + if not self.browser.visit_profile(id): + raise QueryError('Could not visit profile') + return Query(id, 'Profile was visited') #def get_notes(self, id): # if isinstance(id, Contact): diff --git a/modules/okc/browser.py b/modules/okc/browser.py index 177a6deb..d049727f 100644 --- a/modules/okc/browser.py +++ b/modules/okc/browser.py @@ -252,3 +252,22 @@ class OkCBrowser(BaseBrowser): # return True # except ValueError: # return False + + @check_login + def visit_profile(self, id): + self.location(self.absurl('/profile/%s' % id)) + stalk, u, tuid = self.page.get_visit_button_params() + if stalk and u and tuid: + # Premium users, need to click on "visit button" to let the other person know his profile was visited + + data = urllib.urlencode({ + 'ajax' : 1, + 'stalk': stalk, + 'u':u, + 'tuid': tuid + }) + self.addheaders = [('Referer', self.page.url), ('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')] + self.open('http://m.okcupid.com/profile', data=data) + return True + + diff --git a/modules/okc/optim/queries_queue.py b/modules/okc/optim/queries_queue.py index ece864f2..f79ea246 100644 --- a/modules/okc/optim/queries_queue.py +++ b/modules/okc/optim/queries_queue.py @@ -58,16 +58,16 @@ class QueriesQueue(Optimization): def enqueue_query(self, id, priority=999): id_queue = [_id[1] for _id in self.queue] - if int(id) in id_queue: + if id in id_queue: raise QueryError('This id is already queued') - self.queue.append((int(priority), int(id))) + self.queue.append((int(priority), id)) self.save() # Try to flush queue to send it now. self.flush_queue() # Check if the enqueued query has been sent for p, i in self.queue: - if i == int(id): + if i == id: return False return True @@ -86,11 +86,11 @@ class QueriesQueue(Optimization): continue with self.browser: - if self.browser.send_charm(id): - self.logger.info('Charm sent to %s' % id) + if self.browser.visit_profile(id): + self.logger.info('Profile of %s visited' % id) else: self.queue.append((priority, id)) - self.logger.info("Charm can't be send to %s" % id) + self.logger.info("Could not visit profile of %s visited" % id) break # As the charm has been correctly sent (no exception raised), diff --git a/modules/okc/pages.py b/modules/okc/pages.py index aa24fb59..09b57f05 100644 --- a/modules/okc/pages.py +++ b/modules/okc/pages.py @@ -100,6 +100,21 @@ class MessagesPage(BasePage): class ProfilePage(BasePage): + def get_visit_button_params(self): + links = self.parser.select(self.document.getroot(), "//a", method='xpath') + for a in links: + # Premium users can browse anonymusly, need to click on a button to let the other person her profile was visited + onclick = a.get("onclick") + + if onclick is None: + continue + for line in onclick.splitlines(): + match = re.match("^Profile\.action\({stalk:(\d*),u:'(\w*)',tuid:'(\d+)'}", line) + if match is not None: + return match.groups() + # Default case : no premium, profile already visited + return None, None, None + def get_profile(self): title = self.parser.select(self.document.getroot(), 'title', 1) if title.text == 'OkCupid: Account Not Found':