diff --git a/weboob/backends/aum/backend.py b/weboob/backends/aum/backend.py index 72adcf83..a7600c47 100644 --- a/weboob/backends/aum/backend.py +++ b/weboob/backends/aum/backend.py @@ -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) diff --git a/weboob/backends/aum/optim/profiles_walker.py b/weboob/backends/aum/optim/profiles_walker.py index 6903e7c9..54ffbb29 100644 --- a/weboob/backends/aum/optim/profiles_walker.py +++ b/weboob/backends/aum/optim/profiles_walker.py @@ -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: diff --git a/weboob/backends/aum/optim/visibility.py b/weboob/backends/aum/optim/visibility.py new file mode 100644 index 00000000..a5110abf --- /dev/null +++ b/weboob/backends/aum/optim/visibility.py @@ -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 diff --git a/weboob/capabilities/dating.py b/weboob/capabilities/dating.py index e64cb3ce..b34124fe 100644 --- a/weboob/capabilities/dating.py +++ b/weboob/capabilities/dating.py @@ -47,10 +47,41 @@ class Profile(object): return body +class OptimizationNotFound(Exception): pass + +class Optimization(object): + def start(self): + raise NotImplementedError() + + def stop(self): + raise NotImplementedError() class ICapDating(ICap): def get_profile(self, _id): raise NotImplementedError() - def start_profile_walker(self): + OPTIM_PROFILE_WALKER = None + OPTIM_VISIBILITY = None + + def init_optimizations(self): raise NotImplementedError() + + def get_optim(self, optim): + if not hasattr(self, 'OPTIM_%s' % optim): + raise OptimizationNotFound() + + return getattr(self, 'OPTIM_%s' % optim) + + def start_optimization(self, optim): + optim = self.get_optim(optim) + if not optim: + return False + + return optim.start() + + def stop_optimization(self, optim): + optim = self.get_optim(optim) + if not optim: + return False + + return optim.stop() diff --git a/weboob/frontends/havesex/havesex.py b/weboob/frontends/havesex/havesex.py index a7f2c50c..8e83e91c 100644 --- a/weboob/frontends/havesex/havesex.py +++ b/weboob/frontends/havesex/havesex.py @@ -18,11 +18,11 @@ from __future__ import with_statement -import logging import sys +import weboob from weboob.tools.application import PromptApplication -from weboob.capabilities.dating import ICapDating +from weboob.capabilities.dating import ICapDating, OptimizationNotFound __all__ = ['HaveSex'] @@ -38,6 +38,8 @@ class HaveSex(PromptApplication): self.load_config() self.load_backends(ICapDating, storage=self.create_storage(self.STORAGE_FILENAME)) + self.weboob.do('init_optimizations') + return self.loop() def split_id(self, id): @@ -67,18 +69,27 @@ class HaveSex(PromptApplication): print profile.get_profile_text() return True - def service(self, action, function): + def service(self, action, function, *params): sys.stdout.write('%s:' % action) - for backend, result in self.weboob.do(function): - sys.stdout.write(' ' + backend.name) - sys.stdout.flush() + for backend, result in self.weboob.do(function, *params): + if result: + sys.stdout.write(' ' + backend.name) + sys.stdout.flush() sys.stdout.write('.\n') - @PromptApplication.command("start profiles walker") - def command_walker(self, action): - if action == 'start': - self.service('Starting walker', 'start_profiles_walker') - elif action == 'stop': - self.service('Stopping walker', 'stop_profiles_walker') - else: - logging.error(u'Syntax: walker (start|stop)') + def optims(self, action, function, optims): + for optim in optims: + try: + self.service('Starting %s' % optim, 'start_optimization', optim) + except weboob.CallErrors, errors: + for backend, error, backtrace in errors: + if isinstance(error, OptimizationNotFound): + print 'Optimization "%s" not found' % optim + + @PromptApplication.command("start optimizations") + def command_start(self, *optims): + self.optims('Starting', 'start_optimization', optims) + + @PromptApplication.command("stop optimizations") + def command_stop(self, *optims): + self.optims('Stopping', 'stop_optimization', optims) diff --git a/weboob/tools/browser/browser.py b/weboob/tools/browser/browser.py index b988b719..4f66e11e 100644 --- a/weboob/tools/browser/browser.py +++ b/weboob/tools/browser/browser.py @@ -148,6 +148,7 @@ class BaseBrowser(mechanize.Browser): ] # Use a proxy + self.proxy = proxy if proxy: proto = 'http' if proxy.find('://') >= 0: