new optimization QUERIES_QUEUE to enqueue queries in case no charms are available
This commit is contained in:
parent
430ef6cb25
commit
b4fb1a43fa
4 changed files with 130 additions and 9 deletions
|
|
@ -25,8 +25,8 @@ from dateutil import tz
|
||||||
from weboob.capabilities.base import NotLoaded
|
from weboob.capabilities.base import NotLoaded
|
||||||
from weboob.capabilities.chat import ICapChat
|
from weboob.capabilities.chat import ICapChat
|
||||||
from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread
|
from weboob.capabilities.messages import ICapMessages, ICapMessagesPost, Message, Thread
|
||||||
from weboob.capabilities.dating import ICapDating, StatusField
|
from weboob.capabilities.dating import ICapDating, StatusField, OptimizationNotFound
|
||||||
from weboob.capabilities.contact import ICapContact, Contact, ProfileNode
|
from weboob.capabilities.contact import ICapContact, Contact, ProfileNode, Query, QueryError
|
||||||
from weboob.capabilities.account import ICapAccount
|
from weboob.capabilities.account import ICapAccount
|
||||||
from weboob.tools.backend import BaseBackend
|
from weboob.tools.backend import BaseBackend
|
||||||
from weboob.tools.browser import BrowserUnavailable
|
from weboob.tools.browser import BrowserUnavailable
|
||||||
|
|
@ -40,6 +40,7 @@ from .exceptions import AdopteWait
|
||||||
from .optim.profiles_walker import ProfilesWalker
|
from .optim.profiles_walker import ProfilesWalker
|
||||||
from .optim.visibility import Visibility
|
from .optim.visibility import Visibility
|
||||||
from .optim.priority_connection import PriorityConnection
|
from .optim.priority_connection import PriorityConnection
|
||||||
|
from .optim.queries_queue import QueriesQueue
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['AuMBackend']
|
__all__ = ['AuMBackend']
|
||||||
|
|
@ -57,6 +58,7 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
|
||||||
ValueBool('antispam', label='Enable anti-spam', default=False))
|
ValueBool('antispam', label='Enable anti-spam', default=False))
|
||||||
STORAGE = {'profiles_walker': {'viewed': []},
|
STORAGE = {'profiles_walker': {'viewed': []},
|
||||||
'priority_connection': {'config': {}, 'fakes': {}},
|
'priority_connection': {'config': {}, 'fakes': {}},
|
||||||
|
'queries_queue': {'queue': []},
|
||||||
'sluts': {},
|
'sluts': {},
|
||||||
}
|
}
|
||||||
BROWSER = AuMBrowser
|
BROWSER = AuMBrowser
|
||||||
|
|
@ -85,6 +87,7 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
|
||||||
self.add_optimization('PROFILE_WALKER', ProfilesWalker(self.weboob.scheduler, self.storage, self.browser))
|
self.add_optimization('PROFILE_WALKER', ProfilesWalker(self.weboob.scheduler, self.storage, self.browser))
|
||||||
self.add_optimization('VISIBILITY', Visibility(self.weboob.scheduler, self.browser))
|
self.add_optimization('VISIBILITY', Visibility(self.weboob.scheduler, self.browser))
|
||||||
self.add_optimization('PRIORITY_CONNECTION', PriorityConnection(self.weboob.scheduler, self.storage, self.browser))
|
self.add_optimization('PRIORITY_CONNECTION', PriorityConnection(self.weboob.scheduler, self.storage, self.browser))
|
||||||
|
self.add_optimization('QUERIES_QUEUE', QueriesQueue(self.weboob.scheduler, self.storage, self.browser))
|
||||||
|
|
||||||
def get_status(self):
|
def get_status(self):
|
||||||
with self.browser:
|
with self.browser:
|
||||||
|
|
@ -365,6 +368,27 @@ class AuMBackend(BaseBackend, ICapMessages, ICapMessagesPost, ICapDating, ICapCh
|
||||||
c.set_photo(contact['cover'].split('/')[-1].replace('thumb0_', 'image'), thumbnail_url=contact['cover'])
|
c.set_photo(contact['cover'].split('/')[-1].replace('thumb0_', 'image'), thumbnail_url=contact['cover'])
|
||||||
yield c
|
yield c
|
||||||
|
|
||||||
|
def send_query(self, id):
|
||||||
|
if isinstance(id, Contact):
|
||||||
|
id = id.id
|
||||||
|
|
||||||
|
queries_queue = None
|
||||||
|
try:
|
||||||
|
queries_queue = self.get_optimization('QUERIES_QUEUE')
|
||||||
|
except OptimizationNotFound:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if queries_queue and queries_queue.is_running():
|
||||||
|
if queries_queue.enqueue_query(id):
|
||||||
|
return Query(id, 'A charm has been sent')
|
||||||
|
else:
|
||||||
|
return Query(id, 'Unable to send charm: it has been enqueued')
|
||||||
|
else:
|
||||||
|
with self.browser:
|
||||||
|
if not self.browser.send_charm(id):
|
||||||
|
raise QueryError('No enough charms available')
|
||||||
|
return Query(id, 'A charm has been sent')
|
||||||
|
|
||||||
# ---- ICapChat methods ---------------------
|
# ---- ICapChat methods ---------------------
|
||||||
|
|
||||||
def iter_chat_messages(self, _id=None):
|
def iter_chat_messages(self, _id=None):
|
||||||
|
|
|
||||||
|
|
@ -240,13 +240,13 @@ class AuMBrowser(BaseBrowser):
|
||||||
@pageaccess
|
@pageaccess
|
||||||
def send_charm(self, id):
|
def send_charm(self, id):
|
||||||
result = self.openurl('http://www.adopteunmec.com/fajax_addBasket.php?id=%s' % id).read()
|
result = self.openurl('http://www.adopteunmec.com/fajax_addBasket.php?id=%s' % id).read()
|
||||||
self.logger.warning('Charm: %s' % result)
|
self.logger.debug('Charm: %s' % result)
|
||||||
return result.find('noMoreFlashes') < 0
|
return result.find('noMoreFlashes') < 0
|
||||||
|
|
||||||
@pageaccess
|
@pageaccess
|
||||||
def add_basket(self, id):
|
def add_basket(self, id):
|
||||||
result = self.openurl('http://www.adopteunmec.com/fajax_addBasket.php?id=%s' % id).read()
|
result = self.openurl('http://www.adopteunmec.com/fajax_addBasket.php?id=%s' % id).read()
|
||||||
self.logger.warning('Basket: %s' % result)
|
self.logger.debug('Basket: %s' % result)
|
||||||
# TODO check if it works (but it should)
|
# TODO check if it works (but it should)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ class PriorityConnection(Optimization):
|
||||||
self.sched = sched
|
self.sched = sched
|
||||||
self.storage = storage
|
self.storage = storage
|
||||||
self.browser = browser
|
self.browser = browser
|
||||||
self.logger = getLogger('walker', browser.logger)
|
self.logger = getLogger('priorityconn', browser.logger)
|
||||||
|
|
||||||
self.config = storage.get('priority_connection', 'config', default=None)
|
self.config = storage.get('priority_connection', 'config', default=None)
|
||||||
if self.config == {}:
|
if self.config == {}:
|
||||||
|
|
@ -53,10 +53,6 @@ class PriorityConnection(Optimization):
|
||||||
self.check_cron = None
|
self.check_cron = None
|
||||||
self.activity_cron = None
|
self.activity_cron = None
|
||||||
|
|
||||||
def save(self):
|
|
||||||
self.storage.set('profiles_walker', 'viewed', list(self.visited_profiles))
|
|
||||||
self.storage.save()
|
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
if self.config is None:
|
if self.config is None:
|
||||||
return False
|
return False
|
||||||
|
|
|
||||||
101
weboob/backends/aum/optim/queries_queue.py
Normal file
101
weboob/backends/aum/optim/queries_queue.py
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
# -*- 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 __future__ import with_statement
|
||||||
|
|
||||||
|
from weboob.tools.browser import BrowserUnavailable
|
||||||
|
from weboob.capabilities.dating import Optimization
|
||||||
|
from weboob.tools.log import getLogger
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ['QueriesQueue']
|
||||||
|
|
||||||
|
|
||||||
|
class QueriesQueue(Optimization):
|
||||||
|
def __init__(self, sched, storage, browser):
|
||||||
|
self.sched = sched
|
||||||
|
self.storage = storage
|
||||||
|
self.browser = browser
|
||||||
|
self.logger = getLogger('queriesqueue', browser.logger)
|
||||||
|
|
||||||
|
self.queue = storage.get('queries_queue', 'queue', default=[])
|
||||||
|
|
||||||
|
self.check_cron = None
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
self.storage.set('queries_queue', 'queue', self.queue)
|
||||||
|
self.storage.save()
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
self.check_cron = self.sched.repeat(3600, self.flush_queue)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
self.sched.cancel(self.check_cron)
|
||||||
|
self.check_cron = None
|
||||||
|
return True
|
||||||
|
|
||||||
|
def is_running(self):
|
||||||
|
return self.check_cron is not None
|
||||||
|
|
||||||
|
def enqueue_query(self, id, priority=999):
|
||||||
|
self.queue.append((int(priority), int(id)))
|
||||||
|
self.save()
|
||||||
|
# Try to flush queue to send it now.
|
||||||
|
self.flush_queue()
|
||||||
|
|
||||||
|
# Check if the enqueued query has been sent
|
||||||
|
for p, i in self.queue:
|
||||||
|
if i == int(id):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def flush_queue(self):
|
||||||
|
self.queue.sort()
|
||||||
|
|
||||||
|
priority = 0
|
||||||
|
id = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
try:
|
||||||
|
while len(self.queue) > 0:
|
||||||
|
priority, id = self.queue.pop()
|
||||||
|
|
||||||
|
if not id:
|
||||||
|
continue
|
||||||
|
|
||||||
|
with self.browser:
|
||||||
|
if self.browser.send_charm(id):
|
||||||
|
self.logger.info('Charm sent to %s' % id)
|
||||||
|
else:
|
||||||
|
self.queue.append((priority, id))
|
||||||
|
self.logger.info("Charm can't be send to %s" % id)
|
||||||
|
break
|
||||||
|
|
||||||
|
# As the charm has been correctly sent (no exception raised),
|
||||||
|
# we don't store anymore ID, because if nbAvailableCharms()
|
||||||
|
# fails, we don't want to re-queue this ID.
|
||||||
|
id = None
|
||||||
|
priority = 0
|
||||||
|
|
||||||
|
except BrowserUnavailable:
|
||||||
|
# We consider this profil hasn't been [correctly] analysed
|
||||||
|
if not id is None:
|
||||||
|
self.queue.append((priority, id))
|
||||||
|
finally:
|
||||||
|
self.save()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue