Query visit a profile for OKC implemented

This commit is contained in:
Ahmed Boussadia 2014-05-07 15:02:07 +02:00 committed by Romain Bignon
commit 641bbf7a7a
4 changed files with 61 additions and 27 deletions

View file

@ -28,7 +28,7 @@ from dateutil.parser import parse as _parse_dt
from weboob.capabilities.base import NotLoaded from weboob.capabilities.base import NotLoaded
from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread
from weboob.capabilities.dating import ICapDating, OptimizationNotFound, Event 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.backend import BaseBackend, BackendConfig
from weboob.tools.value import Value, ValueBackendPassword from weboob.tools.value import Value, ValueBackendPassword
from weboob.tools.misc import local2utc 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): if c and (c.status & status) and (not ids or c.id in ids):
yield c yield c
#def send_query(self, id): def send_query(self, id):
# if isinstance(id, Contact): if isinstance(id, Contact):
# id = id.id id = id.id
# queries_queue = None queries_queue = None
# try: try:
# queries_queue = self.get_optimization('QUERIES_QUEUE') queries_queue = self.get_optimization('QUERIES_QUEUE')
# except OptimizationNotFound: except OptimizationNotFound:
# pass pass
# if queries_queue and queries_queue.is_running(): if queries_queue and queries_queue.is_running():
# if queries_queue.enqueue_query(id): if queries_queue.enqueue_query(id):
# return Query(id, 'A charm has been sent') return Query(id, 'A profile was visited')
# else: else:
# return Query(id, 'Unable to send charm: it has been enqueued') return Query(id, 'Unable to visit profile: it has been enqueued')
# else: else:
# with self.browser: with self.browser:
# if not self.browser.send_charm(id): if not self.browser.visit_profile(id):
# raise QueryError('No enough charms available') raise QueryError('Could not visit profile')
# return Query(id, 'A charm has been sent') return Query(id, 'Profile was visited')
#def get_notes(self, id): #def get_notes(self, id):
# if isinstance(id, Contact): # if isinstance(id, Contact):

View file

@ -252,3 +252,22 @@ class OkCBrowser(BaseBrowser):
# return True # return True
# except ValueError: # except ValueError:
# return False # 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

View file

@ -58,16 +58,16 @@ class QueriesQueue(Optimization):
def enqueue_query(self, id, priority=999): def enqueue_query(self, id, priority=999):
id_queue = [_id[1] for _id in self.queue] 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') raise QueryError('This id is already queued')
self.queue.append((int(priority), int(id))) self.queue.append((int(priority), id))
self.save() self.save()
# Try to flush queue to send it now. # Try to flush queue to send it now.
self.flush_queue() self.flush_queue()
# Check if the enqueued query has been sent # Check if the enqueued query has been sent
for p, i in self.queue: for p, i in self.queue:
if i == int(id): if i == id:
return False return False
return True return True
@ -86,11 +86,11 @@ class QueriesQueue(Optimization):
continue continue
with self.browser: with self.browser:
if self.browser.send_charm(id): if self.browser.visit_profile(id):
self.logger.info('Charm sent to %s' % id) self.logger.info('Profile of %s visited' % id)
else: else:
self.queue.append((priority, id)) 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 break
# As the charm has been correctly sent (no exception raised), # As the charm has been correctly sent (no exception raised),

View file

@ -100,6 +100,21 @@ class MessagesPage(BasePage):
class ProfilePage(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): def get_profile(self):
title = self.parser.select(self.document.getroot(), 'title', 1) title = self.parser.select(self.document.getroot(), 'title', 1)
if title.text == 'OkCupid: Account Not Found': if title.text == 'OkCupid: Account Not Found':