def __process_conversation(self, conversation_summary): login = get_login_from_conversation_summary(conversation_summary) try: conversation = self.api.conversation(login) if has_new_messages(conversation): logger.debug(f'Processing conversation with {login}') self.__parse_messages(conversation, login) except ReceiverProbablyDoesNotExist: return
def __parse_messages(self, conversation: Conversation, login: str): new_messages = take_new_messages(conversation) entry_ids_from_removed_observations = [] for message in take_messages_body(new_messages): removed_id = self.__remove_observation_from_message(login, message) if removed_id: entry_ids_from_removed_observations += removed_id logger.debug(f'Removed ids: {entry_ids_from_removed_observations}') self.__send_summary_to_login(entry_ids_from_removed_observations, login)
def create_wykop_api(use_login_and_password: bool) -> WykopAPI: logger.debug( f'creating WykopAPI. use_login_and_password={use_login_and_password}') if os.path.isfile(KEYS_FILE_NAME) and not use_login_and_password: keys = read_keys_from_file() api = MultiKeyWykopAPI(keys) else: login, password = read_login_and_password() api = ApiClientWrapper(WYKOP_APP_KEY, output='clear') api.authenticate(login=login, password=password) return api
def save_new_observations(self): observations_candidates = self.__new_observations_candidates() logger.debug( f'Found {len(observations_candidates)} observations candidates') for observation_candidate in observations_candidates: login, entry_id, comment_id, comments_count, mode = observation_candidate logger.debug(f'Processing {login} to {entry_id} entry id') if self.repo.has_entry(entry_id): login_observation = LoginObservation(login, comment_id, mode) self.__update_login_observation(comments_count, entry_id, login_observation) else: self.__save_new_observation(observation_candidate)
def create_dirs(): if not os.path.isdir(LOG_DIR): logger.debug(f'Creating log dir in:{LOG_DIR}') os.mkdir(LOG_DIR) logger.debug('Created') if not os.path.isdir(REPOSITORY_DIR): logger.debug(f'Creating repo dir in:{REPOSITORY_DIR}') os.mkdir(REPOSITORY_DIR) logger.debug('Created')
def main() -> NoReturn: use_login_and_password, interval = load_program_args( create_argument_parser()) logger.debug('Program arguments loaded and parsed') create_dirs() add_console_logger(logging.INFO) add_file_logger(LOG_FILE, logging.DEBUG) add_error_file_logger(ERROR_LOG_FILE) api = create_wykop_api(use_login_and_password) bot = TaktykBot(api, ShelveObservationRepository(REPOSITORY_FILE)) logger.debug('Setup done') while True: try: main_loop(bot) except WykopAPIError as e: logger.error(f'Wykop API error during main_loop: {type(e)}') except Exception as e: logger.error(f'Error during main_loop: {e}', exc_info=True) bot.api = create_wykop_api(use_login_and_password) time.sleep(interval)
def should_send_message_with_all_observation_mode( entry_info: EntryInfo, login_observation: LoginObservation): login = login_observation.login if login_is_author_of_last_comment(login, entry_info): logger.debug( f'Should not send message to {login} because it\'s author of last comment' ) return False if login_is_mentioned_in_last_comment(login, entry_info): logger.debug( f'Should not send message to {login} because it\'s mentioned in last comment' ) return False if new_messages_are_only_observation_requests(login_observation, entry_info): logger.debug( f'Should not send message to {login} because new messages are observation requests' ) return False logger.debug(f'Should send message to {login}') return True
def process_observation(self, observation) -> NoReturn: entry_id = observation.entry_id logger.debug(f'Processing entry with id: {entry_id}') try: entry = self.api.entry(entry_id) except EntryDoesNotExistError: logger.info( f'Entry with id: {entry_id} no longer exists. Marking observation as inactive' ) self.repo.mark_as_inactive(entry_id) return entry_info = EntryInfo(entry) if observation.comments_count < entry_info.current_comments_count: logger.debug(f'New comments to entry with id: {entry_id}') logins = filter_logins_to_send_message(observation, entry_info) logger.debug(f'Logins to send message: {logins}') last_comment_id = entry_info.comments[-1].comment_id self.__send_message_to_logins_about_entity(last_comment_id, observation, logins) self.repo.set_observation_comment_count( observation.entry_id, entry_info.current_comments_count) else: logger.debug(f'No new comments to entry with id: {entry_id}')
def main_loop(bot: TaktykBot): logger.debug('start main loop') bot.run() logger.debug('end main loop')
def send_observations(self) -> None: active_observations = self.repo.get_all_actives() logger.debug( f'Found {len(active_observations)} active entry observations') for observation in active_observations: self.process_observation(observation)
def remove_observations_from_messages(self): for conversation_summary in self.api.conversations_list(): self.__process_conversation(conversation_summary) logger.debug('End of conversations')