mirror of
https://gitea.phreedom.club/localhost_frssoft/FMN_bot.git
synced 2024-11-29 15:21:28 +00:00
Compare commits
No commits in common. "c96056f9a42dd7a59524df17cbbf7a5a6331aa57" and "6414cd863b4a0062767eee1abaed1f466f1fadf8" have entirely different histories.
c96056f9a4
...
6414cd863b
|
@ -1,5 +1,6 @@
|
||||||
from config import instance
|
from config import instance
|
||||||
import time
|
import time
|
||||||
|
import json
|
||||||
import requests
|
import requests
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
|
@ -34,6 +35,7 @@ def get_notifications():
|
||||||
logger.info('Retrying get notificatios...')
|
logger.info('Retrying get notificatios...')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def mark_as_read_notification(id_notification):
|
def mark_as_read_notification(id_notification):
|
||||||
success = 0
|
success = 0
|
||||||
while success == 0:
|
while success == 0:
|
||||||
|
@ -49,7 +51,6 @@ def mark_as_read_notification(id_notification):
|
||||||
|
|
||||||
|
|
||||||
def get_status_context(status_id):
|
def get_status_context(status_id):
|
||||||
retry = 0
|
|
||||||
success = 0
|
success = 0
|
||||||
while success == 0:
|
while success == 0:
|
||||||
try:
|
try:
|
||||||
|
@ -57,14 +58,11 @@ def get_status_context(status_id):
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
success = 1
|
success = 1
|
||||||
return r.json()
|
return r.json()
|
||||||
except Exception as E:
|
except:
|
||||||
logger.exception(f'Ошибка получения контекста треда {status_id}')
|
logger.exception(f'Ошибка получения контекста треда {status_id}')
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
logger.info('Повторный запрос треда...')
|
logger.info('Повторный запрос треда...')
|
||||||
retry += 1
|
|
||||||
if retry > 5:
|
|
||||||
raise IOError(f'Фетчинг треда поломан! {E}')
|
|
||||||
|
|
||||||
|
|
||||||
def get_status(status_id):
|
def get_status(status_id):
|
||||||
success = 0
|
success = 0
|
||||||
|
@ -80,6 +78,7 @@ def get_status(status_id):
|
||||||
logger.info(f'Retrying get status {status_id}')
|
logger.info(f'Retrying get status {status_id}')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def post_status(text, reply_to_status_id=None, poll_options=None, poll_expires=345600, attachments=None):
|
def post_status(text, reply_to_status_id=None, poll_options=None, poll_expires=345600, attachments=None):
|
||||||
poll = None
|
poll = None
|
||||||
if poll_options is not None:
|
if poll_options is not None:
|
||||||
|
@ -137,3 +136,4 @@ def mute_user(acct_id=str, acct=str, duration=None):
|
||||||
logger.exception(f'Ошибка глушения {acct}')
|
logger.exception(f'Ошибка глушения {acct}')
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
logger.info(f'Повторное глушение {acct}...')
|
logger.info(f'Повторное глушение {acct}...')
|
||||||
|
|
||||||
|
|
|
@ -113,3 +113,4 @@ def reset_poll():
|
||||||
'''Сброс содержимого предложки-опроса'''
|
'''Сброс содержимого предложки-опроса'''
|
||||||
c.execute("DELETE FROM poll")
|
c.execute("DELETE FROM poll")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ def create_poll_movies(text=text_create_poll(), poll_expires=345600):
|
||||||
attaches = [upload_attachment('src/FMN.webp')]
|
attaches = [upload_attachment('src/FMN.webp')]
|
||||||
except Exception as E:
|
except Exception as E:
|
||||||
logger.error(f"attachements can't do upload: {E}")
|
logger.error(f"attachements can't do upload: {E}")
|
||||||
|
|
||||||
poll_status_id = post_status(text, None, formated_poll_options,
|
poll_status_id = post_status(text, None, formated_poll_options,
|
||||||
poll_expires=poll_expires, attachments=attaches)
|
poll_expires=poll_expires, attachments=attaches)
|
||||||
logger.success('Голосовалка создана')
|
logger.success('Голосовалка создана')
|
||||||
|
@ -62,7 +62,7 @@ def get_winner_movie(poll_status_id=str):
|
||||||
for option in poll['options']:
|
for option in poll['options']:
|
||||||
votes_count = option['votes_count']
|
votes_count = option['votes_count']
|
||||||
votes_counters.append(votes_count)
|
votes_counters.append(votes_count)
|
||||||
|
|
||||||
write_votes(votes_counters)
|
write_votes(votes_counters)
|
||||||
voted_movies = read_votes()
|
voted_movies = read_votes()
|
||||||
max_vote = voted_movies[0][4]
|
max_vote = voted_movies[0][4]
|
||||||
|
@ -108,3 +108,4 @@ def create_tie_breaker(count_tie=1):
|
||||||
else:
|
else:
|
||||||
poll_expires = 4*60*60
|
poll_expires = 4*60*60
|
||||||
tie_poll = create_poll_movies("TIE BREAKER!!!\n\nВыбираем из победителей!", poll_expires)
|
tie_poll = create_poll_movies("TIE BREAKER!!!\n\nВыбираем из победителей!", poll_expires)
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,6 @@ from loguru import logger
|
||||||
|
|
||||||
states_file = 'fmn_states.json'
|
states_file = 'fmn_states.json'
|
||||||
|
|
||||||
|
|
||||||
class states_stor:
|
class states_stor:
|
||||||
states = None
|
states = None
|
||||||
|
|
||||||
|
|
|
@ -3,10 +3,13 @@ from src.fedi_api import get_status_context, get_status, post_status, mute_user
|
||||||
from src.kinopoisk_api import get_kinopoisk_movie_to_imdb
|
from src.kinopoisk_api import get_kinopoisk_movie_to_imdb
|
||||||
from src.imdb_datasets_worker import get_title_by_id
|
from src.imdb_datasets_worker import get_title_by_id
|
||||||
from src.fmn_database import add_movie_to_poll, get_already_watched, get_suggested_movies_count
|
from src.fmn_database import add_movie_to_poll, get_already_watched, get_suggested_movies_count
|
||||||
from src.fmn_states_db import states_stor
|
from src.fmn_states_db import states_stor, write_states
|
||||||
from src.fmn_poll import create_poll_movies, get_winner_movie
|
from src.fmn_poll import create_poll_movies, get_winner_movie
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
from datetime import datetime
|
||||||
|
from dateutil.parser import parse as dateutilparse
|
||||||
|
from dateutil.relativedelta import relativedelta, TU
|
||||||
from collections import Counter
|
from collections import Counter
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
|
@ -26,7 +29,7 @@ def parse_links_imdb(text=str):
|
||||||
if imdb_ids != []:
|
if imdb_ids != []:
|
||||||
return imdb_ids[:limit_movies_per_user]
|
return imdb_ids[:limit_movies_per_user]
|
||||||
|
|
||||||
|
|
||||||
def scan_context_thread():
|
def scan_context_thread():
|
||||||
fail_limit = Counter()
|
fail_limit = Counter()
|
||||||
while True:
|
while True:
|
||||||
|
@ -55,7 +58,7 @@ def scan_context_thread():
|
||||||
else:
|
else:
|
||||||
endings = int(stop_thread_scan) - time_now
|
endings = int(stop_thread_scan) - time_now
|
||||||
logger.debug(f'Осталось до закрытия сбора: {endings}')
|
logger.debug(f'Осталось до закрытия сбора: {endings}')
|
||||||
if reserve_time: # Reduce instance load
|
if reserve_time: # Reduce instance load
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
get_thread_time = time.time()
|
get_thread_time = time.time()
|
||||||
descendants = get_status_context(status_id)['descendants']
|
descendants = get_status_context(status_id)['descendants']
|
||||||
|
@ -69,10 +72,6 @@ def scan_context_thread():
|
||||||
|
|
||||||
for status in descendants:
|
for status in descendants:
|
||||||
id_st = status['id']
|
id_st = status['id']
|
||||||
visibility = status['visibility']
|
|
||||||
if visibility == 'direct':
|
|
||||||
# Игнорируем личку
|
|
||||||
continue
|
|
||||||
in_reply_acct = status['in_reply_to_account_id']
|
in_reply_acct = status['in_reply_to_account_id']
|
||||||
in_reply_id = status['in_reply_to_id']
|
in_reply_id = status['in_reply_to_id']
|
||||||
muted = status['muted']
|
muted = status['muted']
|
||||||
|
@ -80,14 +79,14 @@ def scan_context_thread():
|
||||||
acct_id = status['account']['id']
|
acct_id = status['account']['id']
|
||||||
content = status['pleroma']['content']['text/plain']
|
content = status['pleroma']['content']['text/plain']
|
||||||
|
|
||||||
if id_st in replyed: # Игнорировать уже отвеченное
|
if id_st in replyed: # Игнорировать уже отвеченное
|
||||||
continue
|
continue
|
||||||
if muted is True:
|
if muted is True:
|
||||||
continue
|
continue
|
||||||
if fail_limit[acct] >= max_fail_limit: # Игнорировать пользователя если он превысил fail limit
|
if fail_limit[acct] >= max_fail_limit: # Игнорировать пользователя если он превысил fail limit
|
||||||
mute_user(acct_id, acct, int(states.get('max_mute_time')) - time_now)
|
mute_user(acct_id, acct, int(states.get('max_mute_time')) - time_now)
|
||||||
logger.warning(f'{acct} игнорируется - превышение fail limit')
|
logger.warning(f'{acct} игнорируется - превышение fail limit')
|
||||||
break # Нужно обновить тред, чтобы muted на заглушенном стал True
|
break # Нужно обновить тред, чтобы muted на заглушенном стал True
|
||||||
|
|
||||||
parsed_result = parse_links(content)
|
parsed_result = parse_links(content)
|
||||||
parsed_result_imdb = parse_links_imdb(content)
|
parsed_result_imdb = parse_links_imdb(content)
|
||||||
|
@ -100,7 +99,7 @@ def scan_context_thread():
|
||||||
logger.info(f'{acct} был уведомлен о завершенной голосовалке')
|
logger.info(f'{acct} был уведомлен о завершенной голосовалке')
|
||||||
fail_limit[acct] += 1
|
fail_limit[acct] += 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
index_type = 1
|
index_type = 1
|
||||||
index_name = 2
|
index_name = 2
|
||||||
index_ru_name = 3
|
index_ru_name = 3
|
||||||
|
@ -108,10 +107,6 @@ def scan_context_thread():
|
||||||
|
|
||||||
message_writer = []
|
message_writer = []
|
||||||
success = False
|
success = False
|
||||||
if parsed_result and parsed_result_imdb:
|
|
||||||
post_status('❌ Не смешивайте IMDB и кинопоиск в одном посте, пожалуйста.', id_st)
|
|
||||||
fail_limit[acct] += 1
|
|
||||||
continue
|
|
||||||
if parsed_result is not None:
|
if parsed_result is not None:
|
||||||
print(parsed_result)
|
print(parsed_result)
|
||||||
suggested_movies = get_kinopoisk_movie_to_imdb(parsed_result)
|
suggested_movies = get_kinopoisk_movie_to_imdb(parsed_result)
|
||||||
|
@ -122,12 +117,13 @@ def scan_context_thread():
|
||||||
continue
|
continue
|
||||||
elif parsed_result_imdb is not None:
|
elif parsed_result_imdb is not None:
|
||||||
suggested_movies = get_title_by_id(parsed_result_imdb)
|
suggested_movies = get_title_by_id(parsed_result_imdb)
|
||||||
|
|
||||||
if suggested_movies is None:
|
if suggested_movies is None:
|
||||||
post_status('❌ Фильм(ы) не найден в базе данных IMDB, пожалуйста обратитесь к администратору, чтобы обновить базу. Примечание: IMDB выкладывает новые изменения не сразу.', id_st)
|
post_status('❌ Фильм(ы) не найден в базе данных IMDB, пожалуйста обратитесь к администратору, чтобы обновить базу. Примечание: IMDB выкладывает новые изменения не сразу.', id_st)
|
||||||
fail_limit[acct] += 1
|
fail_limit[acct] += 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
for movie in suggested_movies:
|
for movie in suggested_movies:
|
||||||
logger.debug(str(movie))
|
logger.debug(str(movie))
|
||||||
if movie[index_type] == "404":
|
if movie[index_type] == "404":
|
||||||
|
@ -144,7 +140,7 @@ def scan_context_thread():
|
||||||
name_ru = movie[index_ru_name]
|
name_ru = movie[index_ru_name]
|
||||||
year = movie[index_year]
|
year = movie[index_year]
|
||||||
movie_string = f"{name_ru} / {name}, {year}"
|
movie_string = f"{name_ru} / {name}, {year}"
|
||||||
|
|
||||||
if name is None:
|
if name is None:
|
||||||
movie_string = f"{name_ru}, {year}"
|
movie_string = f"{name_ru}, {year}"
|
||||||
if name_ru is None:
|
if name_ru is None:
|
||||||
|
@ -158,8 +154,8 @@ def scan_context_thread():
|
||||||
logger.warning(f'Предложение {acct} было отклонено: количество уже предложенных фильмов превышает\равно {limit_all_movies_poll}')
|
logger.warning(f'Предложение {acct} было отклонено: количество уже предложенных фильмов превышает\равно {limit_all_movies_poll}')
|
||||||
fail_limit[acct] += 1
|
fail_limit[acct] += 1
|
||||||
break
|
break
|
||||||
|
|
||||||
if get_already_watched(name, name_ru, year) is True:
|
if get_already_watched(name, name_ru, year) == True:
|
||||||
message_writer.append(f"ℹ️ Этот фильм уже был на FMN: {movie_string}")
|
message_writer.append(f"ℹ️ Этот фильм уже был на FMN: {movie_string}")
|
||||||
logger.info(f'Попытка предложить уже просмотренный фильм: {acct} {name} {name_ru} {year}')
|
logger.info(f'Попытка предложить уже просмотренный фильм: {acct} {name} {name_ru} {year}')
|
||||||
fail_limit[acct] += 1
|
fail_limit[acct] += 1
|
||||||
|
@ -185,3 +181,5 @@ def scan_context_thread():
|
||||||
post_status('\n'.join(message_writer) + message, id_st)
|
post_status('\n'.join(message_writer) + message, id_st)
|
||||||
|
|
||||||
time.sleep(30)
|
time.sleep(30)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,7 @@ from src.fedi_api import get_notifications, mark_as_read_notification, post_stat
|
||||||
from src.fmn_states_db import write_states, states_stor
|
from src.fmn_states_db import write_states, states_stor
|
||||||
from config import admins_bot, limit_movies_per_user, limit_all_movies_poll, hour_poll_posting, fmn_next_watching_hour
|
from config import admins_bot, limit_movies_per_user, limit_all_movies_poll, hour_poll_posting, fmn_next_watching_hour
|
||||||
|
|
||||||
import threading
|
import threading, time
|
||||||
import time
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from dateutil.parser import parse as dateutilparse
|
from dateutil.parser import parse as dateutilparse
|
||||||
from dateutil.relativedelta import relativedelta, TU, SU
|
from dateutil.relativedelta import relativedelta, TU, SU
|
||||||
|
@ -20,8 +19,7 @@ def get_control_mention():
|
||||||
now_hour = time_now.hour
|
now_hour = time_now.hour
|
||||||
if now_week not in (0, 6):
|
if now_week not in (0, 6):
|
||||||
continue
|
continue
|
||||||
if now_week == 6 and now_hour < fmn_next_watching_hour:
|
if now_week == 6 and now_hour < fmn_next_watching_hour: # Предотвращение работы в холстую до начала сеанса
|
||||||
# Предотвращение работы в холстую до начала сеанса
|
|
||||||
continue
|
continue
|
||||||
post_exists = states.get('last_thread_id')
|
post_exists = states.get('last_thread_id')
|
||||||
if post_exists:
|
if post_exists:
|
||||||
|
@ -34,7 +32,7 @@ def get_control_mention():
|
||||||
seen = i['pleroma']['is_seen']
|
seen = i['pleroma']['is_seen']
|
||||||
acct_mention = i['account']['acct']
|
acct_mention = i['account']['acct']
|
||||||
reply_to_id = i['status']['in_reply_to_id']
|
reply_to_id = i['status']['in_reply_to_id']
|
||||||
if acct_mention in admins_bot and seen is False and reply_to_id is None and now_week in (0, 6):
|
if acct_mention in admins_bot and seen == False and reply_to_id == None and now_week in (0, 6):
|
||||||
logger.success(f'Найдено упоминание от {acct_mention}')
|
logger.success(f'Найдено упоминание от {acct_mention}')
|
||||||
st_id = i['status']['id']
|
st_id = i['status']['id']
|
||||||
st_date = i['status']['created_at']
|
st_date = i['status']['created_at']
|
||||||
|
@ -44,7 +42,7 @@ def get_control_mention():
|
||||||
stop_thread_scan = thread_created_at + delta
|
stop_thread_scan = thread_created_at + delta
|
||||||
movies_accept_time = stop_thread_scan.strftime('%H:%M %d.%m.%Y по Москве')
|
movies_accept_time = stop_thread_scan.strftime('%H:%M %d.%m.%Y по Москве')
|
||||||
stop_thread_scan = time.mktime(time.struct_time(stop_thread_scan.timetuple()))
|
stop_thread_scan = time.mktime(time.struct_time(stop_thread_scan.timetuple()))
|
||||||
|
|
||||||
if now_week == 6: # Фикс стыков двух недель. Если вс, то расчитываем на следующую неделю
|
if now_week == 6: # Фикс стыков двух недель. Если вс, то расчитываем на следующую неделю
|
||||||
next_week = 2
|
next_week = 2
|
||||||
else:
|
else:
|
||||||
|
@ -85,7 +83,7 @@ def start_collect_movies_text(movies_accept_time=str, next_movie_watching=str):
|
||||||
'''.replace('\t', '')
|
'''.replace('\t', '')
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
def run_scan_notif():
|
def run_scan_notif():
|
||||||
scan_notif = threading.Thread(target=get_control_mention, daemon=True)
|
scan_notif = threading.Thread(target=get_control_mention, daemon=True)
|
||||||
scan_notif.start()
|
scan_notif.start()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue