new system to manage optimizations (and new optimization VISIBILITY)
This commit is contained in:
parent
3de979dd9b
commit
a4a68d96bc
6 changed files with 121 additions and 27 deletions
|
|
@ -30,6 +30,7 @@ from weboob.tools.browser import BrowserUnavailable
|
||||||
from .browser import AdopteUnMec
|
from .browser import AdopteUnMec
|
||||||
from .exceptions import AdopteWait
|
from .exceptions import AdopteWait
|
||||||
from .optim.profiles_walker import ProfilesWalker
|
from .optim.profiles_walker import ProfilesWalker
|
||||||
|
from .optim.visibility import Visibility
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['AuMBackend']
|
__all__ = ['AuMBackend']
|
||||||
|
|
@ -139,13 +140,9 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesReply, ICapDating, ICapC
|
||||||
except BrowserUnavailable:
|
except BrowserUnavailable:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def start_profiles_walker(self):
|
def init_optimizations(self):
|
||||||
self._profile_walker = ProfilesWalker(self.weboob.scheduler, self.storage, self.browser)
|
self.OPTIM_PROFILE_WALKER = ProfilesWalker(self.weboob.scheduler, self.storage, self.browser)
|
||||||
|
self.OPTIM_VISIBILITY = Visibility(self.weboob.scheduler, self.browser)
|
||||||
def stop_profiles_walker(self):
|
|
||||||
if self._profiles_walker:
|
|
||||||
self._profiles_walker.stop()
|
|
||||||
self._profiles_walker = None
|
|
||||||
|
|
||||||
def iter_chat_contacts(self, online=True, offline=True):
|
def iter_chat_contacts(self, online=True, offline=True):
|
||||||
return self.browser.iter_chat_contacts(online=online, offline=offline)
|
return self.browser.iter_chat_contacts(online=online, offline=offline)
|
||||||
|
|
|
||||||
|
|
@ -22,12 +22,13 @@ from logging import info
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
from weboob.tools.browser import BrowserUnavailable
|
from weboob.tools.browser import BrowserUnavailable
|
||||||
|
from weboob.capabilities.dating import Optimization
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['ProfilesWalker']
|
__all__ = ['ProfilesWalker']
|
||||||
|
|
||||||
|
|
||||||
class ProfilesWalker(object):
|
class ProfilesWalker(Optimization):
|
||||||
def __init__(self, sched, storage, browser):
|
def __init__(self, sched, storage, browser):
|
||||||
self.sched = sched
|
self.sched = sched
|
||||||
self.storage = storage
|
self.storage = storage
|
||||||
|
|
@ -36,16 +37,21 @@ class ProfilesWalker(object):
|
||||||
self.visited_profiles = set(storage.get('profiles_walker', 'viewed'))
|
self.visited_profiles = set(storage.get('profiles_walker', 'viewed'))
|
||||||
info(u'Loaded %d already visited profiles from storage.' % len(self.visited_profiles))
|
info(u'Loaded %d already visited profiles from storage.' % len(self.visited_profiles))
|
||||||
self.profiles_queue = set()
|
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):
|
def save(self):
|
||||||
self.storage.set('profiles_walker', 'viewed', list(self.visited_profiles))
|
self.storage.set('profiles_walker', 'viewed', list(self.visited_profiles))
|
||||||
self.storage.save()
|
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):
|
def stop(self):
|
||||||
self.event.cancel(self.event)
|
# TODO
|
||||||
self.event = None
|
# self.event.cancel(self.event)
|
||||||
|
# self.event = None
|
||||||
|
return False
|
||||||
|
|
||||||
def enqueue_profiles(self):
|
def enqueue_profiles(self):
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
48
weboob/backends/aum/optim/visibility.py
Normal file
48
weboob/backends/aum/optim/visibility.py
Normal 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
|
||||||
|
|
@ -47,10 +47,41 @@ class Profile(object):
|
||||||
|
|
||||||
return body
|
return body
|
||||||
|
|
||||||
|
class OptimizationNotFound(Exception): pass
|
||||||
|
|
||||||
|
class Optimization(object):
|
||||||
|
def start(self):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
class ICapDating(ICap):
|
class ICapDating(ICap):
|
||||||
def get_profile(self, _id):
|
def get_profile(self, _id):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def start_profile_walker(self):
|
OPTIM_PROFILE_WALKER = None
|
||||||
|
OPTIM_VISIBILITY = None
|
||||||
|
|
||||||
|
def init_optimizations(self):
|
||||||
raise NotImplementedError()
|
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()
|
||||||
|
|
|
||||||
|
|
@ -18,11 +18,11 @@
|
||||||
|
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement
|
||||||
|
|
||||||
import logging
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import weboob
|
||||||
from weboob.tools.application import PromptApplication
|
from weboob.tools.application import PromptApplication
|
||||||
from weboob.capabilities.dating import ICapDating
|
from weboob.capabilities.dating import ICapDating, OptimizationNotFound
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['HaveSex']
|
__all__ = ['HaveSex']
|
||||||
|
|
@ -38,6 +38,8 @@ class HaveSex(PromptApplication):
|
||||||
self.load_config()
|
self.load_config()
|
||||||
self.load_backends(ICapDating, storage=self.create_storage(self.STORAGE_FILENAME))
|
self.load_backends(ICapDating, storage=self.create_storage(self.STORAGE_FILENAME))
|
||||||
|
|
||||||
|
self.weboob.do('init_optimizations')
|
||||||
|
|
||||||
return self.loop()
|
return self.loop()
|
||||||
|
|
||||||
def split_id(self, id):
|
def split_id(self, id):
|
||||||
|
|
@ -67,18 +69,27 @@ class HaveSex(PromptApplication):
|
||||||
print profile.get_profile_text()
|
print profile.get_profile_text()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def service(self, action, function):
|
def service(self, action, function, *params):
|
||||||
sys.stdout.write('%s:' % action)
|
sys.stdout.write('%s:' % action)
|
||||||
for backend, result in self.weboob.do(function):
|
for backend, result in self.weboob.do(function, *params):
|
||||||
sys.stdout.write(' ' + backend.name)
|
if result:
|
||||||
sys.stdout.flush()
|
sys.stdout.write(' ' + backend.name)
|
||||||
|
sys.stdout.flush()
|
||||||
sys.stdout.write('.\n')
|
sys.stdout.write('.\n')
|
||||||
|
|
||||||
@PromptApplication.command("start profiles walker")
|
def optims(self, action, function, optims):
|
||||||
def command_walker(self, action):
|
for optim in optims:
|
||||||
if action == 'start':
|
try:
|
||||||
self.service('Starting walker', 'start_profiles_walker')
|
self.service('Starting %s' % optim, 'start_optimization', optim)
|
||||||
elif action == 'stop':
|
except weboob.CallErrors, errors:
|
||||||
self.service('Stopping walker', 'stop_profiles_walker')
|
for backend, error, backtrace in errors:
|
||||||
else:
|
if isinstance(error, OptimizationNotFound):
|
||||||
logging.error(u'Syntax: walker (start|stop)')
|
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)
|
||||||
|
|
|
||||||
|
|
@ -148,6 +148,7 @@ class BaseBrowser(mechanize.Browser):
|
||||||
]
|
]
|
||||||
|
|
||||||
# Use a proxy
|
# Use a proxy
|
||||||
|
self.proxy = proxy
|
||||||
if proxy:
|
if proxy:
|
||||||
proto = 'http'
|
proto = 'http'
|
||||||
if proxy.find('://') >= 0:
|
if proxy.find('://') >= 0:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue