mirror of
https://gitea.phreedom.club/localhost_frssoft/FMN_bot
synced 2024-06-18 14:09:20 +03:00
69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
|
from collections import Counter
|
|||
|
from src.fedi_api import get_status, post_status
|
|||
|
from src.fmn_database import get_movies_for_poll, write_votes, read_votes, mark_as_watched_movie, get_already_watched, rewrite_db
|
|||
|
import logging
|
|||
|
|
|||
|
logger = logging.getLogger('fmn_poll')
|
|||
|
|
|||
|
text_create_poll = '''Друзья, голосование за следующий Fediverse Movie Night объявляю открытым!
|
|||
|
|
|||
|
Ставки сделаны, ставок больше нет
|
|||
|
'''.replace('\t', '')
|
|||
|
|
|||
|
|
|||
|
def create_poll_movies(text=text_create_poll, poll_expires=345600):
|
|||
|
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})"
|
|||
|
formated_poll_options.append(poll_option_string)
|
|||
|
|
|||
|
poll_status_id = post_status(text, None, formated_poll_options, poll_expires=poll_expires)['id']
|
|||
|
logger.info('Голосовалка создана')
|
|||
|
with open('poll_status_id', mode='wt') as file:
|
|||
|
file.write(poll_status_id)
|
|||
|
return poll_status_id
|
|||
|
|
|||
|
|
|||
|
def get_winner_movie(poll_status_id=str):
|
|||
|
'''Отмечаем победивший фильм на голосовании как просмотренный и постим об этом'''
|
|||
|
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)
|
|||
|
create_tie_breaker()
|
|||
|
else:
|
|||
|
movie = winned_movies[0]
|
|||
|
logger.warning("Победил " + str(movie))
|
|||
|
mark_as_watched_movie(movie[1], movie[2], movie[3])
|
|||
|
|
|||
|
|
|||
|
def create_tie_breaker():
|
|||
|
tie_poll = create_poll_movies("TIE BREAKER!!!", 8*60*60)
|
|||
|
time.sleep(8*60*60)
|
|||
|
get_winner_movie(tie_poll)
|
|||
|
|