2022-08-31 23:06:06 +00:00
|
|
|
from src.fedi_api import get_status, post_status, upload_attachment
|
2022-11-21 20:33:00 +00:00
|
|
|
from src.fmn_states_db import read_states, write_states
|
2022-11-01 16:05:18 +00:00
|
|
|
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
|
2022-08-31 19:22:04 +00:00
|
|
|
from collections import Counter
|
2022-09-06 23:55:12 +00:00
|
|
|
from loguru import logger
|
2022-08-31 19:22:04 +00:00
|
|
|
import time
|
2022-08-31 10:20:49 +00:00
|
|
|
|
|
|
|
|
2022-10-02 16:27:00 +00:00
|
|
|
def text_create_poll():
|
2022-11-01 16:05:18 +00:00
|
|
|
force_commit()
|
2022-11-01 14:56:07 +00:00
|
|
|
count_poll = get_count_all_watched_movies()
|
2022-10-02 16:27:00 +00:00
|
|
|
text_poll = f'''Друзья, {count_poll} голосование за следующий Fediverse Movie Night объявляю открытым!
|
2022-08-31 10:20:49 +00:00
|
|
|
|
2022-10-02 16:27:00 +00:00
|
|
|
Ставки сделаны, ставок больше нет
|
|
|
|
'''.replace('\t', '')
|
|
|
|
return text_poll
|
2022-08-31 10:20:49 +00:00
|
|
|
|
|
|
|
|
2022-10-02 16:27:00 +00:00
|
|
|
def create_poll_movies(text=text_create_poll(), poll_expires=345600):
|
2022-08-31 10:20:49 +00:00
|
|
|
formated_poll_options = []
|
|
|
|
raw_poll = get_movies_for_poll()
|
|
|
|
for i in raw_poll:
|
|
|
|
acct = i[0]
|
|
|
|
orig_name = i[1]
|
|
|
|
ru_name = i[2]
|
|
|
|
year = i[3]
|
|
|
|
poll_option_string = f"{ru_name} / {orig_name}, {year} ({acct})"
|
|
|
|
if ru_name is None:
|
|
|
|
poll_option_string = f"{orig_name}, {year} ({acct})"
|
|
|
|
if orig_name is None:
|
|
|
|
poll_option_string = f"{ru_name}, {year} ({acct})"
|
2022-09-11 21:51:28 +00:00
|
|
|
if len(poll_option_string) >= 200:
|
|
|
|
poll_option_string = poll_option_string[0:199] # Обрезка на 200 символов.
|
2022-08-31 10:20:49 +00:00
|
|
|
formated_poll_options.append(poll_option_string)
|
2022-10-02 16:27:00 +00:00
|
|
|
|
2022-08-31 23:06:06 +00:00
|
|
|
poll_status_id = post_status(text, None, formated_poll_options,
|
|
|
|
poll_expires=poll_expires, attachments=[upload_attachment('src/FMN.png')])
|
2022-09-07 00:05:18 +00:00
|
|
|
logger.success('Голосовалка создана')
|
2022-11-21 20:33:00 +00:00
|
|
|
states = read_states()
|
|
|
|
states['poll_expires_at'] = int(time.time()) + poll_expires
|
|
|
|
states['poll_status_id'] = poll_status_id['id']
|
|
|
|
write_states(states)
|
2022-08-31 10:20:49 +00:00
|
|
|
return poll_status_id
|
|
|
|
|
|
|
|
|
|
|
|
def get_winner_movie(poll_status_id=str):
|
2022-08-31 19:22:04 +00:00
|
|
|
'''Отмечаем победивший фильм на голосовании как просмотренный или постим tie breaker'''
|
2022-11-21 20:33:00 +00:00
|
|
|
states = read_states()
|
2022-08-31 10:20:49 +00:00
|
|
|
votes_counters = []
|
|
|
|
status_with_poll = get_status(poll_status_id)
|
|
|
|
poll = status_with_poll['poll']
|
|
|
|
votes_counter = Counter()
|
|
|
|
for option in poll['options']:
|
|
|
|
votes_count = option['votes_count']
|
|
|
|
votes_counters.append(votes_count)
|
|
|
|
|
|
|
|
write_votes(votes_counters)
|
|
|
|
voted_movies = read_votes()
|
|
|
|
max_vote = voted_movies[0][4]
|
|
|
|
winned_movies = []
|
|
|
|
for i in voted_movies:
|
|
|
|
if max_vote == i[4]:
|
|
|
|
winned_movies.append(i)
|
|
|
|
|
|
|
|
if len(winned_movies) > 1:
|
|
|
|
logger.warning('Будет создан tie breaker')
|
|
|
|
rewrite_db(winned_movies)
|
2022-11-21 20:33:00 +00:00
|
|
|
if states.get('tie_breaker'):
|
2022-08-31 19:22:04 +00:00
|
|
|
create_tie_breaker(2)
|
|
|
|
else:
|
|
|
|
create_tie_breaker()
|
2022-08-31 10:20:49 +00:00
|
|
|
else:
|
|
|
|
movie = winned_movies[0]
|
|
|
|
mark_as_watched_movie(movie[1], movie[2], movie[3])
|
2022-09-06 19:16:30 +00:00
|
|
|
|
2022-09-17 13:18:42 +00:00
|
|
|
acct_suggested, orig_name, ru_name, year, votes_count_win = movie
|
2022-09-10 20:30:08 +00:00
|
|
|
win_variant = f"{ru_name} / {orig_name}, {year}"
|
2022-09-06 19:16:30 +00:00
|
|
|
if ru_name is None:
|
|
|
|
win_variant = f"{orig_name}, {year}"
|
|
|
|
if orig_name is None:
|
|
|
|
win_variant = f"{ru_name}, {year}"
|
2022-10-02 16:27:00 +00:00
|
|
|
expired_poll_count = get_count_all_watched_movies() - 1
|
|
|
|
text_winned = f"{expired_poll_count} голосование завершилось! Победил вариант предложенный @{acct_suggested}:\n{win_variant}"
|
2022-09-06 19:16:30 +00:00
|
|
|
|
2022-09-07 00:05:18 +00:00
|
|
|
logger.success("Победил " + str(movie))
|
2022-09-06 19:16:30 +00:00
|
|
|
post_status(text_winned, attachments=[upload_attachment('src/FMN.png')])
|
2022-11-21 20:33:00 +00:00
|
|
|
write_states()
|
2022-08-31 19:22:04 +00:00
|
|
|
reset_poll()
|
2022-08-31 10:20:49 +00:00
|
|
|
|
|
|
|
|
2022-08-31 19:22:04 +00:00
|
|
|
def create_tie_breaker(count_tie=1):
|
|
|
|
'''Создание tie breaker'''
|
|
|
|
if count_tie == 1:
|
2022-11-21 20:33:00 +00:00
|
|
|
states = read_states()
|
|
|
|
states['tie_breaker'] = 1
|
|
|
|
write_states(states)
|
2022-08-31 19:22:04 +00:00
|
|
|
poll_expires = 8*60*60
|
|
|
|
else:
|
|
|
|
poll_expires = 4*60*60
|
2022-08-31 23:06:06 +00:00
|
|
|
tie_poll = create_poll_movies("TIE BREAKER!!!\n\nВыбираем из победителей!", poll_expires)
|
2022-08-31 10:20:49 +00:00
|
|
|
|