Example #1
0
 def __init__(self):
     self.logger = Logger(self.__class__.__name__)
     self.updating_followers = set()
     self.candidates = []
     # Load candidates from db and create objects to access their elements
     self.candidates = CandidateDAO().all()
     ConcurrencyUtils().create_lock('candidate_for_update')
Example #2
0
 def get_necessary_data(cls):
     """ Retrieve db data and create candidates list. """
     candidate_index, candidate_group = CandidateDAO(
     ).get_required_candidates()
     candidates_list = list(candidate_index.keys())
     candidates_rt_cursor = RawTweetDAO().get_rt_to_candidates_cursor(
         candidates_list)
     return candidate_index, candidates_list, candidate_group, candidates_rt_cursor
Example #3
0
def create_indexes():
    """ Create all required collection indexes. """
    CandidateDAO().create_indexes()
    RawFollowerDAO().create_indexes()
    # RawTweetDAO().create_indexes()
    UserHashtagDAO().create_indexes()
    CooccurrenceGraphDAO().create_indexes()
Example #4
0
 def add_candidate(self, screen_name, nickname=None):
     """ Add a candidate with given screen name and nickname to the database and to the json file. """
     try:
         CandidateDAO().find(screen_name)
     except NonExistentCandidateError:
         self.logger.info(f'Adding candidate {screen_name} to database.')
         candidate = Candidate(**{
             'screen_name': screen_name,
             'nickname': nickname
         })
         # Store in database
         CandidateDAO().save(candidate)
         # Update json resource
         CandidateDAO().update_json_resource(candidate)
         # Update current structure
         self.candidates.append(candidate)
         return
     raise CandidateAlreadyExistsError(screen_name)
Example #5
0
 def finish_follower_updating(self, candidate):
     """ Unlock user for follower updating and update last updating time. """
     if candidate not in self.updating_followers:
         raise CandidateCurrentlyAvailableForUpdateError(
             candidate.screen_name)
     # Update last updated followers date
     self.logger.info(
         f'Removing candidate {candidate.screen_name} from currently updating set.'
     )
     candidate.last_updated_followers = datetime.now()
     CandidateDAO().overwrite(candidate)
     # Remove from set to not be polled again
     self.updating_followers.remove(candidate)
Example #6
0
 def initialize_context(cls):
     """ Create instances of all environment services in a Spring-like fashion."""
     cls.LOGGER.info('Instantiating context services and components.')
     ConfigurationManager()
     ConcurrencyUtils()
     Scheduler()
     CandidateDAO()
     RawFollowerDAO()
     CandidatesFollowersDAO()
     CredentialService()
     CandidateService()
     FollowerUpdateService()
     TweetUpdateService()
     FollowersQueueService()
Example #7
0
class CandidateService(metaclass=Singleton):
    def __init__(self):
        self.logger = Logger(self.__class__.__name__)
        self.updating_followers = set()
        self.candidates = []
        # Load candidates from db and create objects to access their elements
        self.candidates = CandidateDAO().all()
        ConcurrencyUtils().create_lock('candidate_for_update')

    def get_all(self):
        """ Returns all candidates currently in the list. """
        return self.candidates

    def get_for_follower_updating(self):
        """ Polls a candidate for updating its follower list. """
        # Lock to avoid concurrency issues when retrieving candidates across threads
        ConcurrencyUtils().acquire_lock('candidate_for_update')
        for candidate in self.candidates:
            # We will only return a candidate if it was not updated today and is not being currently updated
            if candidate not in self.updating_followers and not DateUtils.is_today(
                    candidate.last_updated_followers):
                self.logger.info(
                    f'Returning candidate {candidate.screen_name} for follower retrieval.'
                )
                self.updating_followers.add(candidate)
                # Unlock
                ConcurrencyUtils().release_lock('candidate_for_update')
                return candidate
        # Unlock
        ConcurrencyUtils().release_lock('candidate_for_update')
        raise FollowerUpdatingNotNecessaryError()

    def finish_follower_updating(self, candidate):
        """ Unlock user for follower updating and update last updating time. """
        if candidate not in self.updating_followers:
            raise CandidateCurrentlyAvailableForUpdateError(
                candidate.screen_name)
        # Update last updated followers date
        self.logger.info(
            f'Removing candidate {candidate.screen_name} from currently updating set.'
        )
        candidate.last_updated_followers = datetime.now()
        CandidateDAO().overwrite(candidate)
        # Remove from set to not be polled again
        self.updating_followers.remove(candidate)

    def add_candidate(self, screen_name, nickname=None):
        """ Add a candidate with given screen name and nickname to the database and to the json file. """
        try:
            CandidateDAO().find(screen_name)
        except NonExistentCandidateError:
            self.logger.info(f'Adding candidate {screen_name} to database.')
            candidate = Candidate(**{
                'screen_name': screen_name,
                'nickname': nickname
            })
            # Store in database
            CandidateDAO().save(candidate)
            # Update json resource
            CandidateDAO().update_json_resource(candidate)
            # Update current structure
            self.candidates.append(candidate)
            return
        raise CandidateAlreadyExistsError(screen_name)
Example #8
0
def create_base_entries():
    """ Create all required entries. """
    CandidateDAO().create_base_entries()