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

View file

@ -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()

View file

@ -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)

View file

@ -148,6 +148,7 @@ class BaseBrowser(mechanize.Browser):
]
# Use a proxy
self.proxy = proxy
if proxy:
proto = 'http'
if proxy.find('://') >= 0: