new system to manage optimizations (and new optimization VISIBILITY)

This commit is contained in:
Romain Bignon 2010-06-30 03:19:38 +02:00
commit a4a68d96bc
6 changed files with 121 additions and 27 deletions

View file

@ -30,6 +30,7 @@ from weboob.tools.browser import BrowserUnavailable
from .browser import AdopteUnMec
from .exceptions import AdopteWait
from .optim.profiles_walker import ProfilesWalker
from .optim.visibility import Visibility
__all__ = ['AuMBackend']
@ -139,13 +140,9 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapC
except BrowserUnavailable:
return None
def start_profiles_walker(self):
self._profile_walker = ProfilesWalker(self.weboob.scheduler, self.storage, self.browser)
def stop_profiles_walker(self):
if self._profiles_walker:
self._profiles_walker.stop()
self._profiles_walker = None
def init_optimizations(self):
self.OPTIM_PROFILE_WALKER = ProfilesWalker(self.weboob.scheduler, self.storage, self.browser)
self.OPTIM_VISIBILITY = Visibility(self.weboob.scheduler, self.browser)
def iter_chat_contacts(self, online=True, offline=True):
return self.browser.iter_chat_contacts(online=online, offline=offline)

View file

@ -22,12 +22,13 @@ from logging import info
from random import randint
from weboob.tools.browser import BrowserUnavailable
from weboob.capabilities.dating import Optimization
__all__ = ['ProfilesWalker']
class ProfilesWalker(object):
class ProfilesWalker(Optimization):
def __init__(self, sched, storage, browser):
self.sched = sched
self.storage = storage
@ -36,16 +37,21 @@ class ProfilesWalker(object):
self.visited_profiles = set(storage.get('profiles_walker', 'viewed'))
info(u'Loaded %d already visited profiles from storage.' % len(self.visited_profiles))
self.profiles_queue = set()
self.walk_cron = sched.repeat(60, self.enqueue_profiles)
self.view_cron = sched.schedule(randint(10,40), self.view_profile)
def save(self):
self.storage.set('profiles_walker', 'viewed', list(self.visited_profiles))
self.storage.save()
def start(self):
self.walk_cron = self.sched.repeat(60, self.enqueue_profiles)
self.view_cron = self.sched.schedule(randint(10,40), self.view_profile)
return True
def stop(self):
self.event.cancel(self.event)
self.event = None
# TODO
# self.event.cancel(self.event)
# self.event = None
return False
def enqueue_profiles(self):
try:

View file

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2010 Romain Bignon
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
from weboob.tools.browser import BrowserUnavailable
from weboob.capabilities.dating import Optimization
from ..browser import AdopteUnMec
__all__ = ['Visibility']
class Visibility(Optimization):
def __init__(self, sched, browser):
self.sched = sched
self.browser = browser
def start(self):
self.cron = self.sched.repeat(60*5, self.reconnect)
return True
def stop(self):
# TODO
return False
def reconnect(self):
try:
AdopteUnMec(self.browser.username,
self.browser.password,
proxy=self.browser.proxy)
except BrowserUnavailable, e:
print str(e)
pass