mirror of
https://git.phreedom.club/localhost_frssoft/FMN_bot.git
synced 2024-11-24 17:20:57 +01:00
Add class for states
Lower read\write in states file No attempt force upload attachment (server issues) More logger catchers
This commit is contained in:
parent
3eebf7266c
commit
b166016df8
|
@ -116,17 +116,9 @@ def upload_attachment(file_path):
|
|||
params = {
|
||||
"description": "Fediverse Movie Night\nВоскресенье, 21:00\nLIVE ON XXIV Production",
|
||||
}
|
||||
success = 0
|
||||
while success == 0:
|
||||
try:
|
||||
r = s.post(instance_point + "/media", params, files=file)
|
||||
r.raise_for_status()
|
||||
success = 1
|
||||
return r.json()['id']
|
||||
except:
|
||||
logger.exception(f'Error uploading {file_path} attachment')
|
||||
time.sleep(5)
|
||||
logger.info(f'Retrying upload {file_path}...')
|
||||
r = s.post(instance_point + "/media", params, files=file, timeout=30)
|
||||
r.raise_for_status()
|
||||
return r.json()['id']
|
||||
|
||||
|
||||
def mute_user(acct_id=str, acct=str, duration=None):
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from src.fedi_api import get_status, post_status, upload_attachment
|
||||
from src.fmn_states_db import read_states, write_states
|
||||
from src.fmn_states_db import states_stor, write_states
|
||||
from src.fmn_database import get_movies_for_poll, write_votes, read_votes, mark_as_watched_movie, get_already_watched, rewrite_db, reset_poll, get_count_all_watched_movies, force_commit
|
||||
from collections import Counter
|
||||
from loguru import logger
|
||||
|
@ -16,7 +16,9 @@ def text_create_poll():
|
|||
return text_poll
|
||||
|
||||
|
||||
@logger.catch
|
||||
def create_poll_movies(text=text_create_poll(), poll_expires=345600):
|
||||
logger.debug('Creating poll')
|
||||
formated_poll_options = []
|
||||
raw_poll = get_movies_for_poll()
|
||||
for i in raw_poll:
|
||||
|
@ -25,6 +27,7 @@ def create_poll_movies(text=text_create_poll(), poll_expires=345600):
|
|||
ru_name = i[2]
|
||||
year = i[3]
|
||||
poll_option_string = f"{ru_name} / {orig_name}, {year} ({acct})"
|
||||
logger.debug(f"Adding option in poll: {poll_option_string}")
|
||||
if ru_name is None:
|
||||
poll_option_string = f"{orig_name}, {year} ({acct})"
|
||||
if orig_name is None:
|
||||
|
@ -33,19 +36,25 @@ def create_poll_movies(text=text_create_poll(), poll_expires=345600):
|
|||
poll_option_string = poll_option_string[0:199] # Обрезка на 200 символов.
|
||||
formated_poll_options.append(poll_option_string)
|
||||
|
||||
attaches = []
|
||||
try:
|
||||
attaches = [upload_attachment('src/FMN.png')]
|
||||
except Exception as E:
|
||||
logger.error(f"attachements can't do upload: {E}")
|
||||
|
||||
poll_status_id = post_status(text, None, formated_poll_options,
|
||||
poll_expires=poll_expires, attachments=[upload_attachment('src/FMN.png')])
|
||||
poll_expires=poll_expires, attachments=attaches)
|
||||
logger.success('Голосовалка создана')
|
||||
states = read_states()
|
||||
states['poll_expires_at'] = int(time.time()) + poll_expires
|
||||
states['poll_status_id'] = poll_status_id['id']
|
||||
write_states(states)
|
||||
states_stor.states['poll_expires_at'] = int(time.time()) + poll_expires
|
||||
states_stor.states['poll_status_id'] = poll_status_id['id']
|
||||
write_states(states_stor.states)
|
||||
return poll_status_id
|
||||
|
||||
|
||||
@logger.catch
|
||||
def get_winner_movie(poll_status_id=str):
|
||||
'''Отмечаем победивший фильм на голосовании как просмотренный или постим tie breaker'''
|
||||
states = read_states()
|
||||
states = states_stor.states
|
||||
votes_counters = []
|
||||
status_with_poll = get_status(poll_status_id)
|
||||
poll = status_with_poll['poll']
|
||||
|
@ -88,12 +97,12 @@ def get_winner_movie(poll_status_id=str):
|
|||
reset_poll()
|
||||
|
||||
|
||||
@logger.catch
|
||||
def create_tie_breaker(count_tie=1):
|
||||
'''Создание tie breaker'''
|
||||
if count_tie == 1:
|
||||
states = read_states()
|
||||
states['tie_breaker'] = 1
|
||||
write_states(states)
|
||||
states_stor.states['tie_breaker'] = 1
|
||||
write_states(states_stor.states)
|
||||
poll_expires = 8*60*60
|
||||
else:
|
||||
poll_expires = 4*60*60
|
||||
|
|
|
@ -3,6 +3,10 @@ from loguru import logger
|
|||
|
||||
states_file = 'fmn_states.json'
|
||||
|
||||
class states_stor:
|
||||
states = None
|
||||
|
||||
|
||||
@logger.catch
|
||||
def read_states():
|
||||
try:
|
||||
|
@ -16,10 +20,13 @@ def read_states():
|
|||
|
||||
|
||||
@logger.catch
|
||||
def write_states(states={}):
|
||||
def write_states(new_states={}):
|
||||
with open(states_file, 'wt') as f:
|
||||
f.write(json.dumps(states, indent=4))
|
||||
if states == {}:
|
||||
f.write(json.dumps(new_states, indent=4))
|
||||
if new_states == {}:
|
||||
logger.info('states empty wrote')
|
||||
return states
|
||||
return new_states
|
||||
|
||||
|
||||
if not states_stor.states:
|
||||
states_stor.states = read_states()
|
||||
|
|
|
@ -3,7 +3,7 @@ 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.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_states_db import read_states, write_states
|
||||
from src.fmn_states_db import states_stor, write_states
|
||||
from src.fmn_poll import create_poll_movies, get_winner_movie
|
||||
import re
|
||||
import time
|
||||
|
@ -33,14 +33,14 @@ def parse_links_imdb(text=str):
|
|||
def scan_context_thread():
|
||||
fail_limit = Counter()
|
||||
while True:
|
||||
states = read_states()
|
||||
states = states_stor.states
|
||||
status_id = states.get('last_thread_id')
|
||||
poll_created = states.get('poll_status_id')
|
||||
stop_thread_scan = states.get('stop_thread_scan')
|
||||
time_now = int(time.time())
|
||||
reserve_time = False
|
||||
while status_id is None or stop_thread_scan is None:
|
||||
states = read_states()
|
||||
states = states_stor.states
|
||||
fail_limit = Counter()
|
||||
status_id = states.get('last_thread_id')
|
||||
stop_thread_scan = states.get('stop_thread_scan')
|
||||
|
@ -51,7 +51,7 @@ def scan_context_thread():
|
|||
logger.debug('Сбор завершён, сканирование треда на опоздавших')
|
||||
if poll_created is None:
|
||||
create_poll_movies()
|
||||
poll_created = states.get('poll_status_id')
|
||||
poll_created = states_stor.states.get('poll_status_id')
|
||||
else:
|
||||
if time_now >= int(states.get('poll_expires_at')):
|
||||
get_winner_movie(poll_created)
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from src.fedi_api import get_notifications, mark_as_read_notification, post_status, upload_attachment
|
||||
from src.fmn_states_db import write_states, read_states
|
||||
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
|
||||
|
||||
import threading, time
|
||||
|
@ -9,9 +9,10 @@ from dateutil.relativedelta import relativedelta, TU, SU
|
|||
from loguru import logger
|
||||
|
||||
|
||||
@logger.catch
|
||||
def get_control_mention():
|
||||
while True:
|
||||
states = read_states()
|
||||
states = states_stor.states
|
||||
time.sleep(30)
|
||||
time_now = datetime.now()
|
||||
now_week = time_now.weekday()
|
||||
|
@ -26,7 +27,7 @@ def get_control_mention():
|
|||
logger.debug('Wait for from admin mention...')
|
||||
notif = get_notifications()
|
||||
for i in notif:
|
||||
if i['type'] != "mention":
|
||||
if i['type'] not in ("mention"):
|
||||
continue
|
||||
seen = i['pleroma']['is_seen']
|
||||
acct_mention = i['account']['acct']
|
||||
|
@ -55,10 +56,10 @@ def get_control_mention():
|
|||
time.sleep(0.2)
|
||||
mark_as_read_notification(i['id'])
|
||||
|
||||
states['max_mute_time'] = int(max_mute_time)
|
||||
states['stop_thread_scan'] = int(stop_thread_scan)
|
||||
states['last_thread_id'] = st_id
|
||||
write_states(states)
|
||||
states_stor.states['max_mute_time'] = int(max_mute_time)
|
||||
states_stor.states['stop_thread_scan'] = int(stop_thread_scan)
|
||||
states_stor.states['last_thread_id'] = st_id
|
||||
write_states(states_stor.states)
|
||||
break
|
||||
time.sleep(30)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user