Skip to content
profiles_walker.py 3.59 KiB
Newer Older
# -*- coding: utf-8 -*-

Romain Bignon's avatar
Romain Bignon committed
# Copyright(C) 2010-2011 Romain Bignon, Christophe Benz
Roger Philibert's avatar
Roger Philibert committed
# This file is part of a woob module.
Roger Philibert's avatar
Roger Philibert committed
# This woob module is free software: you can redistribute it and/or modify
Romain Bignon's avatar
Romain Bignon committed
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
Roger Philibert's avatar
Roger Philibert committed
# This woob module is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Romain Bignon's avatar
Romain Bignon committed
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
Romain Bignon's avatar
Romain Bignon committed
# You should have received a copy of the GNU Affero General Public License
Roger Philibert's avatar
Roger Philibert committed
# along with this woob module. If not, see <http://www.gnu.org/licenses/>.

from random import randint
from woob.exceptions import BrowserUnavailable
from woob.capabilities.dating import Optimization
from woob.tools.log import getLogger
class ProfilesWalker(Optimization):
    def __init__(self, sched, storage, browser):
Romain Bignon's avatar
Romain Bignon committed
        super(ProfilesWalker, self).__init__()
        self._sched = sched
        self._storage = storage
        self._browser = browser
        self._logger = getLogger('walker', browser.logger)

        self._walk_cron = None
        self._view_cron = None
        self._visited_profiles = set(storage.get('profiles_walker', 'viewed'))
        self._logger.info(u'Loaded %d already visited profiles from storage.' % len(self._visited_profiles))
        self._profiles_queue = set()
Romain Bignon's avatar
Romain Bignon committed
        self._storage.set('profiles_walker', 'viewed', list(self._visited_profiles))
        self._storage.save()
Romain Bignon's avatar
Romain Bignon committed
        self._walk_cron = self._sched.repeat(60, self.enqueue_profiles)
        self._view_cron = self._sched.schedule(randint(5, 10), self.view_profile)
Romain Bignon's avatar
Romain Bignon committed
        self._sched.cancel(self._walk_cron)
        self._sched.cancel(self._view_cron)
        self._walk_cron = None
        self._view_cron = None
        return True
Romain Bignon's avatar
Romain Bignon committed
        return self._walk_cron is not None
    def enqueue_profiles(self):
            profiles_to_visit = self._browser.search_profiles().difference(self._visited_profiles)
            self._logger.info(u'Enqueuing profiles to visit: %s' % profiles_to_visit)
            self._profiles_queue = set(profiles_to_visit)
            self.save()
        except BrowserUnavailable:
            return

    def view_profile(self):
        try:
            try:
Romain Bignon's avatar
Romain Bignon committed
                id = self._profiles_queue.pop()
                return  # empty queue
                profile = self._browser.get_profile(id)
Romain Bignon's avatar
Romain Bignon committed
                self._logger.info(u'Visited profile %s (%s)' % (profile['pseudo'], id))

                # Get score from the aum_score module
                #d = self.nucentral_core.callService(context.Context.fromComponent(self), 'aum_score', 'score', profile)
                # d.addCallback(self.score_cb, profile.getID())
                # deferredlist.append(d)

                # do not forget that we visited this profile, to avoid re-visiting it.
Romain Bignon's avatar
Romain Bignon committed
                self._visited_profiles.add(id)
                self.save()

            except BrowserUnavailable:
                # We consider this profil hasn't been [correctly] analysed
Romain Bignon's avatar
Romain Bignon committed
                self._profiles_queue.add(id)
            except Exception as e:
Romain Bignon's avatar
Romain Bignon committed
                self._logger.exception(e)
Romain Bignon's avatar
Romain Bignon committed
            if self._view_cron is not None:
                self._view_cron = self._sched.schedule(randint(5, 10), self.view_profile)