From 84870c9d7c8da3d57ce273077f1dfc2e9967057f Mon Sep 17 00:00:00 2001 From: localhost_frssoft Date: Wed, 31 Aug 2022 18:24:30 +0300 Subject: [PATCH] Add change thread context on the fly --- src/listener_context.py | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/listener_context.py b/src/listener_context.py index 211a5e7..3de6343 100644 --- a/src/listener_context.py +++ b/src/listener_context.py @@ -5,11 +5,12 @@ from dateutil.relativedelta import relativedelta, TU import re import logging -from config import hour_poll_posting, bot_acct +from config import hour_poll_posting, bot_acct, instance from src.fedi_api import get_status_context, get_status, post_status from src.kinopoisk_api import get_kinopoisk_movie from src.imdb_datasets_worker import get_title_by_id from src.fmn_database import add_movie_to_poll, get_already_watched +from src.fmn_states_db import get_state, add_state from src.fmn_poll import create_poll_movies logger = logging.getLogger('thread_listener') @@ -33,29 +34,28 @@ def parse_links_imdb(text=str): def scan_context_thread(): - status_id = None - poll_created = False while True: + status_id = get_state('last_thread_id') + poll_created = get_state('poll_status_id') + flag_scan = 0 time_now = time.time() - while status_id == None: - try: - with open('last_thread_id', mode='rt') as file: - status_id = file.read().replace('\n', '') - thread_created_at = get_status(status_id)['created_at'] - thread_created_at = dateutilparse(thread_created_at) - delta = relativedelta(hour=hour_poll_posting, minute=0, second=0, weekday=TU(1)) - stop_thread_scan = thread_created_at + delta - logger.info('Сбор будет завершен в ' + stop_thread_scan.strftime('%c')) - stop_thread_scan = time.mktime(time.struct_time(stop_thread_scan.timetuple())) - except Exception as E: - logger.exception(E) - status_id = None + while status_id is None: + status_id = get_state('last_thread_id') + if status_id and flag_scan == 0: + thread_created_at = get_status(status_id)['created_at'] + thread_created_at = dateutilparse(thread_created_at) + delta = relativedelta(hour=hour_poll_posting, minute=0, second=0, weekday=TU(1)) + stop_thread_scan = thread_created_at + delta + logger.info('Сбор будет завершен в ' + stop_thread_scan.strftime('%c')) + stop_thread_scan = time.mktime(time.struct_time(stop_thread_scan.timetuple())) + flag_scan = 1 time.sleep(1) + if int(time_now) >= int(stop_thread_scan): logger.debug('Сбор завершён, сканирование треда на опоздавших') - if poll_created is False: + if poll_created is None: create_poll_movies() - poll_created = True + poll_created = get_state('poll_status_id') else: endings = stop_thread_scan - time_now logger.debug(f'Осталось до закрытия сбора: {endings}') @@ -80,8 +80,8 @@ def scan_context_thread(): if parsed_result is None and parsed_result_imdb is None: continue - if poll_created is True: - post_status('ℹ️ Приём заявок уже окончен', id_st) + if poll_created: + post_status(f'ℹ️ Приём заявок уже окончен.\n\nГолосовалка здесь: https://{instance}/notice/{poll_created}', id_st) continue if parsed_result is not None: