Changed notifications management to Mastodon API

This commit is contained in:
spla 2022-03-17 12:53:30 +01:00
pare eb2df2d0a8
commit 6a5638cfb7
S'han modificat 2 arxius amb 115 adicions i 338 eliminacions

Veure arxiu

@ -4,6 +4,7 @@ import os.path
import re import re
import unidecode import unidecode
from datetime import datetime, timedelta from datetime import datetime, timedelta
import time
from mastodon import Mastodon from mastodon import Mastodon
from email.mime.multipart import MIMEMultipart from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText from email.mime.text import MIMEText
@ -20,6 +21,7 @@ from cairosvg import svg2png
import chess.pgn import chess.pgn
from PIL import Image, ImageFont, ImageDraw from PIL import Image, ImageFont, ImageDraw
import math import math
import pdb
def cleanhtml(raw_html): def cleanhtml(raw_html):
cleanr = re.compile('<.*?>') cleanr = re.compile('<.*?>')
@ -131,75 +133,6 @@ def create_panel(username, rating, played_games, wins):
out = Image.alpha_composite(base, txt) out = Image.alpha_composite(base, txt)
out.save('app/panel/' + username + '_panel.png') out.save('app/panel/' + username + '_panel.png')
def get_bot_id():
###################################################################################################################################
# get bot_id from bot's username
try:
conn = None
conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
cur.execute("select id from accounts where username = (%s) and domain is null", (bot_username,))
row = cur.fetchone()
if row != None:
bot_id = row[0]
cur.close()
return bot_id
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def get_user_domain(account_id):
try:
conn = None
conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
cur.execute("select username, domain from accounts where id=(%s)", (account_id,))
row = cur.fetchone()
if row != None:
username = row[0]
domain = row[1]
cur.close()
return (username, domain)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def get_piece_name(captured_piece): def get_piece_name(captured_piece):
if captured_piece == 1: if captured_piece == 1:
@ -240,53 +173,7 @@ def get_piece_name(captured_piece):
return piece_name return piece_name
def get_mentions(): def get_player_langs(account_id):
account_id_lst = []
status_id_lst = []
conn = None
try:
conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
select_query = "select account_id, id from statuses where created_at + interval '60 minutes' > now() - interval '5 minutes'"
select_query += " and id=any (select status_id from mentions where account_id=(%s)) order by created_at asc"
cur.execute(select_query, (str(bot_id),))
rows = cur.fetchall()
for row in rows:
replied = check_replies(row[1])
if not replied:
account_id_lst.append(row[0])
status_id_lst.append(row[1])
cur.close()
return (account_id_lst, status_id_lst)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def get_mention_langs(account_id):
lang_changed = False lang_changed = False
@ -360,7 +247,6 @@ def get_lang(player):
conn.close() conn.close()
def set_lang(account_id, username, new_lang): def set_lang(account_id, username, new_lang):
lang_changed = False lang_changed = False
@ -441,119 +327,6 @@ def get_locale( parameter, player_lang):
print(language_filepath + " Missing parameter %s "%parameter) print(language_filepath + " Missing parameter %s "%parameter)
sys.exit(0) sys.exit(0)
def get_notification_data(status_id):
conn = None
try:
conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
select_query = "select text, visibility, url from statuses where id=(%s)"
cur.execute(select_query, (status_id,))
row = cur.fetchone()
text = row[0]
if row[1] == 0:
visibility = 'public'
elif row[1] == 1:
visibility = 'unlisted'
elif row[1] == 2:
visibility = 'private'
elif row[1] == 3:
visibility = 'direct'
url = row[2]
cur.close()
return (text, visibility, url)
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def update_replies(status_id, username, now):
post_id = status_id
try:
conn = None
conn = psycopg2.connect(database = chess_db, user = chess_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
insert_sql = "insert into botreplies(status_id, query_user, status_created_at) values(%s, %s, %s) ON CONFLICT DO NOTHING"
cur.execute(insert_sql, (post_id, username, now))
conn.commit()
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
sys.exit(error)
finally:
if conn is not None:
conn.close()
def check_replies(status_id):
post_id = status_id
replied = False
try:
conn = None
conn = psycopg2.connect(database = chess_db, user = chess_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor()
cur.execute("select status_id from botreplies where status_id=(%s)", (post_id,))
row = cur.fetchone()
if row != None:
replied = True
else:
replied = False
cur.close()
return replied
except (Exception, psycopg2.DatabaseError) as error:
sys.exit(error)
finally:
if conn is not None:
conn.close()
def current_games(): def current_games():
player1_name_lst = [] player1_name_lst = []
@ -1635,24 +1408,46 @@ if __name__ == '__main__':
now = datetime.now() now = datetime.now()
bot_id = get_bot_id() ####################################################################
# get notifications
account_id_lst, status_id_lst = get_mentions() notifications = mastodon.notifications()
if len(account_id_lst) == 0: if len(notifications) == 0:
print('No mentions') print('No mentions')
sys.exit(0) sys.exit(0)
i = 0 i = 0
while i < len(account_id_lst): while i < len(notifications):
account_id = account_id_lst[i] notification_id = notifications[i].id
username, domain = get_user_domain(account_id) if notifications[i].type != 'mention':
lang_changed, player_lang = get_mention_langs(account_id) i += 1
print(f'dismissing notification {notification_id}')
mastodon.notifications_dismiss(notification_id)
continue
account_id = notifications[i].account.id
username = notifications[i].account.acct
status_id = notifications[i].status.id
text = notifications[i].status.content
visibility = notifications[i].status.visibility
url = notifications[i].status.uri
lang_changed, player_lang = get_player_langs(account_id)
if player_lang == 'ca': if player_lang == 'ca':
@ -1686,24 +1481,6 @@ if __name__ == '__main__':
sys.exit(0) sys.exit(0)
status_id = status_id_lst[i]
text, visibility, url = get_notification_data(status_id)
if domain != None:
username = username + '@' + domain
status_id = status_id_lst[i]
replied = check_replies(status_id)
if replied == True:
i += 1
continue
# listen them or not # listen them or not
search_new = get_locale("search_new", player_lang) search_new = get_locale("search_new", player_lang)
@ -1746,7 +1523,7 @@ if __name__ == '__main__':
new_game_started = get_locale("new_game_started", player_lang) new_game_started = get_locale("new_game_started", player_lang)
toot_text = '@'+username + ' ' + new_game_started + '\n' toot_text = f'@{username} {new_game_started} \n'
toot_text += '\n' toot_text += '\n'
@ -1758,7 +1535,7 @@ if __name__ == '__main__':
new_game(toot_url) new_game(toot_url)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
elif query_word == search_new and game_waiting: elif query_word == search_new and game_waiting:
@ -1780,22 +1557,18 @@ if __name__ == '__main__':
playing_with = get_locale("playing_with", player_lang1) playing_with = get_locale("playing_with", player_lang1)
toot_text = '@'+username + ' ' + playing_with + ' ' + white_user + "\n" toot_text = f'@{username} {playing_with} {white_user} \n\n'
toot_text += '\n'
player_lang2 = get_lang(white_user) player_lang2 = get_lang(white_user)
your_turn = get_locale("your_turn", player_lang2) your_turn = get_locale("your_turn", player_lang2)
toot_text += '@'+white_user + ': ' + your_turn + "\n" toot_text += f'@{white_user}: {your_turn}\n\n'
toot_text += '\n'
game_name = get_locale("game_name", player_lang2) game_name = get_locale("game_name", player_lang2)
chess_hashtag = get_locale("chess_hashtag", player_lang) chess_hashtag = get_locale("chess_hashtag", player_lang)
toot_text += game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' toot_text += f"{game_name}: {str(game_id)} {chess_hashtag} \n"
image_id = mastodon.media_post(board_file, "image/png").id image_id = mastodon.media_post(board_file, "image/png").id
@ -1805,7 +1578,7 @@ if __name__ == '__main__':
update_moves(username, game_moves) update_moves(username, game_moves)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
elif query_word[:search_send_slicing] == search_send: elif query_word[:search_send_slicing] == search_send:
@ -1820,7 +1593,7 @@ if __name__ == '__main__':
username_lang = get_lang(username) username_lang = get_lang(username)
send_error = get_locale("send_error", username_lang) send_error = get_locale("send_error", username_lang)
toot_text = '@'+username + ' ' + send_error toot_text = f'@{username} {send_error}'
elif emailed == True and game_found == True: elif emailed == True and game_found == True:
@ -1828,7 +1601,7 @@ if __name__ == '__main__':
game_number_anotations = get_locale("game_number_anotations", username_lang) game_number_anotations = get_locale("game_number_anotations", username_lang)
anotations_sent = get_locale("anotations_sent", username_lang) anotations_sent = get_locale("anotations_sent", username_lang)
toot_text = '@'+username + ' ' + game_number_anotations + str(game_id) + ' ' + anotations_sent toot_text = f'@{username} {game_number_anotations} {str(game_id)} {anotations_sent}'
elif emailed == False and game_found == False: elif emailed == False and game_found == False:
@ -1837,7 +1610,7 @@ if __name__ == '__main__':
username_lang = get_lang(username) username_lang = get_lang(username)
cant_send_to_fediverse_account = get_locale("cant_send_to_fediverse_account", username_lang) cant_send_to_fediverse_account = get_locale("cant_send_to_fediverse_account", username_lang)
toot_text = '@'+username + ' ' + cant_send_to_fediverse_account toot_text = f'@{username} {cant_send_to_fediverse_account}'
else: else:
@ -1845,11 +1618,11 @@ if __name__ == '__main__':
game_no_exists = get_locale("game_no_exists", username_lang) game_no_exists = get_locale("game_no_exists", username_lang)
it_not_exists = get_locale("it_not_exists", username_lang) it_not_exists = get_locale("it_not_exists", username_lang)
toot_text = '@'+username + ' ' + game_no_exists + str(game_id) + ' ' + it_not_exists toot_text = f'@{username} {game_no_exists} {str(game_id)} {it_not_exists}'
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
elif query_word == search_panel: elif query_word == search_panel:
@ -1857,7 +1630,7 @@ if __name__ == '__main__':
create_panel(username, rating, played_games, wins) create_panel(username, rating, played_games, wins)
toot_text = '@'+username toot_text = f'@{username}'
saved_panel = 'app/panel/' + username + '_panel.png' saved_panel = 'app/panel/' + username + '_panel.png'
@ -1865,7 +1638,7 @@ if __name__ == '__main__':
mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility, media_ids={image_id}) mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility, media_ids={image_id})
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
elif query_word[:4] == search_config: elif query_word[:4] == search_config:
@ -1873,13 +1646,13 @@ if __name__ == '__main__':
lang_changed, player_lang = set_lang(account_id, username, new_lang) lang_changed, player_lang = set_lang(account_id, username, new_lang)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
if lang_changed: if lang_changed:
locale_change_successfully = get_locale("locale_change_successfully", new_lang) locale_change_successfully = get_locale("locale_change_successfully", new_lang)
toot_text = '@'+username + ' ' + locale_change_successfully + ' ' + new_lang toot_text = f'@{username} {locale_change_successfully} {new_lang}'
mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility)
@ -1887,7 +1660,7 @@ if __name__ == '__main__':
locale_not_changed = get_locale("locale_not_changed", player_lang) locale_not_changed = get_locale("locale_not_changed", player_lang)
toot_text = '@'+username + ' ' + new_lang + ' ' + locale_not_changed toot_text = f'@{username} {new_lang} {locale_not_changed}'
mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility)
@ -1895,7 +1668,7 @@ if __name__ == '__main__':
toot_help() toot_help()
help_text = '@'+username help_text = f'@{username}'
help_panel = 'app/panel/help_panel.png' help_panel = 'app/panel/help_panel.png'
@ -1903,11 +1676,11 @@ if __name__ == '__main__':
mastodon.status_post(help_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) mastodon.status_post(help_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id})
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
else: else:
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
elif reply and is_playing: elif reply and is_playing:
@ -1917,11 +1690,11 @@ if __name__ == '__main__':
game_already_started = get_locale("game_already_started", player_lang1) game_already_started = get_locale("game_already_started", player_lang1)
toot_text = '@'+username + ' ' + game_already_started + '\n' toot_text = f'@{username} {game_already_started} \n'
if black_user != '': if black_user != '':
toot_text += '@'+white_user + ' / ' + '@'+black_user + '\n' toot_text += f'@{white_user} / @{black_user}\n'
else: else:
@ -1934,7 +1707,7 @@ if __name__ == '__main__':
game_name = get_locale("game_name", player_lang1) game_name = get_locale("game_name", player_lang1)
chess_hashtag = get_locale("chess_hashtag", player_lang1) chess_hashtag = get_locale("chess_hashtag", player_lang1)
toot_text += game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' toot_text += f'{game_name}: {str(game_id)} {chess_hashtag} \n'
board = chess.Board(on_going_game) board = chess.Board(on_going_game)
@ -1948,7 +1721,7 @@ if __name__ == '__main__':
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id})
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
elif query_word[:search_move_slicing] == search_move and playing_user == username: elif query_word[:search_move_slicing] == search_move and playing_user == username:
@ -1996,11 +1769,11 @@ if __name__ == '__main__':
is_not_legal_move = get_locale("is_not_legal_move", player_lang) is_not_legal_move = get_locale("is_not_legal_move", player_lang)
toot_text = '@'+username + ': ' + moving + ' ' + is_not_legal_move + '\n' toot_text = f'@{username}: {moving} {is_not_legal_move} \n'
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
else: else:
@ -2061,7 +1834,7 @@ if __name__ == '__main__':
player_lang = get_lang(playing_user) player_lang = get_lang(playing_user)
check_done = get_locale("check_done", player_lang) check_done = get_locale("check_done", player_lang)
toot_text = "@"+playing_user + " " + username + ' ' + check_done + '\n' toot_text = f"@{playing_user} {username} {check_done}\n"
elif check == True and checkmate == True: elif check == True and checkmate == True:
@ -2071,23 +1844,23 @@ if __name__ == '__main__':
check_mate_movements = get_locale("check_mate_movements", player_lang1) check_mate_movements = get_locale("check_mate_movements", player_lang1)
the_winner_is = get_locale("the_winner_is", player_lang1) the_winner_is = get_locale("the_winner_is", player_lang1)
toot_text = '\n' + check_mate + ' ' + str(game_moves) + ' ' + check_mate_movements + '\n\n' + the_winner_is + ' ' + "@"+username + '\n' toot_text = f'\n{check_mate} {str(game_moves)} {check_mate_movements}\n\n{the_winner_is} @{username}\n'
winned_games = get_locale("winned_games", player_lang1) winned_games = get_locale("winned_games", player_lang1)
toot_text += '\n' + winned_games + "\n" toot_text += f'\n{winned_games}\n'
rating, played_games, wins = get_stats(username) rating, played_games, wins = get_stats(username)
wins_of_many = get_locale("wins_of_many", player_lang1) wins_of_many = get_locale("wins_of_many", player_lang1)
toot_text += username + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + ' (Elo: ' + str(round(rating)) + ')' + '\n' toot_text += f'{username}: {str(wins)} {wins_of_many} {str(played_games)} (Elo: {str(round(rating))})\n'
player_lang2 = get_lang(playing_user) player_lang2 = get_lang(playing_user)
well_done = get_locale("well_done", player_lang2) well_done = get_locale("well_done", player_lang2)
toot_text += "\n@"+playing_user + ': ' + well_done + "\n" toot_text += f"\n@{playing_user}: {well_done}\n"
rating, played_games, wins = get_stats(playing_user) rating, played_games, wins = get_stats(playing_user)
@ -2095,25 +1868,25 @@ if __name__ == '__main__':
wins_of_many = get_locale("wins_of_many", player_lang2) wins_of_many = get_locale("wins_of_many", player_lang2)
toot_text += '\n\n' + winned_games + "\n" toot_text += f'\n\n{winned_games}\n'
toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + ' (Elo: ' + str(round(rating)) + ')' + '\n' toot_text += f'{playing_user}: {str(wins)} {wins_of_many} {str(played_games)} (Elo: {str(round(rating))})\n'
elif check == False and stalemate == True: elif check == False and stalemate == True:
toot_text = stalemate_str + ' (' + str(game_moves) + ')' + '\n' toot_text = stalemate_str + ' (' + str(game_moves) + ')' + '\n'
toot_text += '\n@'+playing_user + ', ' + '@'+username + "\n" toot_text += f'\n@{playing_user}, @{username}\n'
toot_text += '\n' + winned_games + "\n" toot_text += f'\n{winned_games}\n'
rating, played_games, wins = get_stats(username) rating, played_games, wins = get_stats(username)
toot_text += username + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" toot_text += f'{username}: {str(wins)} {wins_of_many} {str(played_games)}\n'
rating, played_games, wins = get_stats(playing_user) rating, played_games, wins = get_stats(playing_user)
toot_text += playing_user + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" toot_text += f'{playing_user}: {str(wins)} {wins_of_many} {str(played_games)}\n'
else: else:
@ -2121,20 +1894,20 @@ if __name__ == '__main__':
your_turn = get_locale("your_turn", player_lang) your_turn = get_locale("your_turn", player_lang)
toot_text = '@'+playing_user + ' ' + your_turn + '\n' toot_text = f'@{playing_user} {your_turn}\n'
if capture == True and checkmate == False: if capture == True and checkmate == False:
player_lang = get_lang(playing_user) player_lang = get_lang(playing_user)
lost_piece = get_locale("lost_piece", player_lang) lost_piece = get_locale("lost_piece", player_lang)
toot_text += '\n' + lost_piece + ' ' + piece_name + '!\n' toot_text += f'\n{lost_piece} {piece_name}!\n'
game_name = get_locale("game_name", player_lang) game_name = get_locale("game_name", player_lang)
chess_hashtag = get_locale("chess_hashtag", player_lang) chess_hashtag = get_locale("chess_hashtag", player_lang)
toot_text += '\n' + game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' toot_text += f'\n{game_name}: {str(game_id)} {chess_hashtag}\n'
if username == white_user: if username == white_user:
@ -2176,7 +1949,7 @@ if __name__ == '__main__':
update_moves(username, game_moves) update_moves(username, game_moves)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
except ValueError as v_error: except ValueError as v_error:
@ -2185,11 +1958,11 @@ if __name__ == '__main__':
username_lang = get_lang(username) username_lang = get_lang(username)
not_legal_move_str = get_locale("not_legal_move_str", username_lang) not_legal_move_str = get_locale("not_legal_move_str", username_lang)
toot_text = '@'+username + ' ' + not_legal_move_str + ' ' + moving + '!?)\n' toot_text = f'@{username} {not_legal_move_str} {moving}!?)\n'
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
pass pass
@ -2200,11 +1973,11 @@ if __name__ == '__main__':
username_lang = get_lang(username) username_lang = get_lang(username)
not_legal_move_str = get_locale("not_legal_move_str", username_lang) not_legal_move_str = get_locale("not_legal_move_str", username_lang)
toot_text = '@'+username + ' ' + not_legal_move_str + ' ' + moving + '!?)\n' toot_text = f'@{username} {not_legal_move_str} {moving}!?)\n'
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
pass pass
@ -2220,13 +1993,13 @@ if __name__ == '__main__':
player_leave_game = get_locale("player_leave_game", player_lang) player_leave_game = get_locale("player_leave_game", player_lang)
toot_text = '@'+username + ' ' + player_leave_game + ' ' + '@'+black_user toot_text = f'@{username} {player_leave_game} @{black_user}'
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
close_game(username, checkmate) close_game(username, checkmate)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
i += 1 i += 1
@ -2236,13 +2009,13 @@ if __name__ == '__main__':
player_leave_game = get_locale("player_leave_game", player_lang) player_leave_game = get_locale("player_leave_game", player_lang)
toot_text = '@'+username + ' ' + player_leave_game + ' ' + '@'+white_user toot_text = f'@{username} {player_leave_game} @{white_user}'
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
close_game(username, checkmate) close_game(username, checkmate)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
i += 1 i += 1
@ -2252,13 +2025,13 @@ if __name__ == '__main__':
leave_waiting_game = get_locale("leave_waiting_game", player_lang) leave_waiting_game = get_locale("leave_waiting_game", player_lang)
toot_text = '@'+username + ' ' + leave_waiting_game toot_text = f'@{username} {leave_waiting_game}'
mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility)
close_game(username, checkmate) close_game(username, checkmate)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
i += 1 i += 1
@ -2272,7 +2045,7 @@ if __name__ == '__main__':
started_games = get_locale("started_games", player_lang) started_games = get_locale("started_games", player_lang)
toot_text = "@"+username + ' ' + started_games + "\n" toot_text = f"@{username} {started_games}\n"
i = 0 i = 0
while i < len(player1_name_lst): while i < len(player1_name_lst):
@ -2281,7 +2054,7 @@ if __name__ == '__main__':
game_is_waiting = get_locale("game_is_waiting", player_lang) game_is_waiting = get_locale("game_is_waiting", player_lang)
toot_text += '\n' + player1_name_lst[i] + ' / ' + player2_name_lst[i] + ' ' + game_is_waiting + "\n" toot_text += f'\n{player1_name_lst[i]} / {player2_name_lst[i]} {game_is_waiting}\n'
else: else:
@ -2289,15 +2062,15 @@ if __name__ == '__main__':
if next_move_lst[i] == player1_name_lst[i]: if next_move_lst[i] == player1_name_lst[i]:
toot_text += '\n*' + player1_name_lst[i] + ' / ' + player2_name_lst[i] + ' ' + game_is_on_going + '\n' toot_text += f'\n*{player1_name_lst[i]} / {player2_name_lst[i]} {game_is_on_going}\n'
else: else:
toot_text += '\n' + player1_name_lst[i] + ' / *' + player2_name_lst[i] + ' ' + game_is_on_going + '\n' toot_text += f'\n{player1_name_lst[i]} / *{player2_name_lst[i]} {game_is_on_going}\n'
if game_link_lst[i] != None: if game_link_lst[i] != None:
toot_text += str(game_link_lst[i]) + "\n" toot_text += f'{str(game_link_lst[i])}\n'
i += 1 i += 1
@ -2307,11 +2080,11 @@ if __name__ == '__main__':
no_on_going_games = get_locale("no_on_going_games", player_lang) no_on_going_games = get_locale("no_on_going_games", player_lang)
toot_text = '@'+username + ' ' + no_on_going_games + '\n' toot_text = f'@{username} {no_on_going_games}\n'
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
elif query_word[:search_send_slicing] == search_send: elif query_word[:search_send_slicing] == search_send:
@ -2327,25 +2100,25 @@ if __name__ == '__main__':
send_error = get_locale("send_error", username_lang) send_error = get_locale("send_error", username_lang)
toot_text = '@'+username + ' ' + send_error toot_text = f'@{username} {send_error}'
elif emailed == True and game_found == True: elif emailed == True and game_found == True:
game_number_anotations = get_locale("game_number_anotations", username_lang) game_number_anotations = get_locale("game_number_anotations", username_lang)
anotations_sent = get_locale("anotations_sent", username_lang) anotations_sent = get_locale("anotations_sent", username_lang)
toot_text = '@'+username + ' ' + game_number_anotations + str(game_id) + ' ' + anotations_sent toot_text = f'@{username} {game_number_anotations} {str(game_id)} {anotations_sent}'
elif emailed == False and game_found == False: elif emailed == False and game_found == False:
game_no_exists = get_locale("game_no_exists", username_lang) game_no_exists = get_locale("game_no_exists", username_lang)
it_not_exists = get_locale("it_not_exists", username_lang) it_not_exists = get_locale("it_not_exists", username_lang)
toot_text = '@'+username + ' ' + game_no_exists + str(game_id) + ' ' + it_not_exists toot_text = f'@{username} {game_no_exists} {str(game_id)} {it_not_exists}'
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
elif query_word == search_draw: elif query_word == search_draw:
@ -2363,13 +2136,13 @@ if __name__ == '__main__':
winned_games = get_locale("winned_games", player_lang1) winned_games = get_locale("winned_games", player_lang1)
wins_of_many = get_locale("wins_of_many", player_lang1) wins_of_many = get_locale("wins_of_many", player_lang1)
toot_text = '@'+white_player + ' ' + draw_and_str + ' ' + '@'+black_player + ' ' + agreed_draw_str + '\n\n' toot_text = f'@{white_player} {draw_and_str} @{black_player} {agreed_draw_str}\n\n'
toot_text += '\n' + winned_games + "\n" toot_text += f'\n{winned_games}\n'
rating, played_games, wins = get_stats(white_player) rating, played_games, wins = get_stats(white_player)
toot_text += white_player + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" toot_text += f'{white_player}: {str(wins)} {wins_of_many} {str(played_games)}\n'
player_lang2 = get_lang(black_player) player_lang2 = get_lang(black_player)
draw_and_str = get_locale("draw_and_str", player_lang2) draw_and_str = get_locale("draw_and_str", player_lang2)
@ -2379,13 +2152,13 @@ if __name__ == '__main__':
if player_lang1 != player_lang2: if player_lang1 != player_lang2:
toot_text += '\n@'+white_player + ' ' + draw_and_str + ' ' + '@'+black_player + ' ' + agreed_draw_str + '\n\n' toot_text += f'\n@{white_player} {draw_and_str} @{black_player} {agreed_draw_str}\n\n'
toot_text += '\n' + winned_games + "\n" toot_text += f'\n{winned_games}\n'
rating, played_games, wins = get_stats(black_player) rating, played_games, wins = get_stats(black_player)
toot_text += black_player + ': ' + str(wins) + ' ' + wins_of_many + ' ' + str(played_games) + "\n" toot_text += f'{black_player}: {str(wins)} {wins_of_many} {str(played_games)}\n'
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
@ -2393,7 +2166,7 @@ if __name__ == '__main__':
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
elif query_word == search_panel: elif query_word == search_panel:
@ -2401,7 +2174,7 @@ if __name__ == '__main__':
create_panel(username, rating, played_games, wins) create_panel(username, rating, played_games, wins)
toot_text = '@'+username toot_text = f'@{username}'
saved_panel = 'app/panel/' + username + '_panel.png' saved_panel = 'app/panel/' + username + '_panel.png'
@ -2409,7 +2182,7 @@ if __name__ == '__main__':
mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility, media_ids={image_id}) mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility, media_ids={image_id})
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
elif query_word[:4] == search_config: elif query_word[:4] == search_config:
@ -2417,13 +2190,13 @@ if __name__ == '__main__':
lang_changed, player_lang = set_lang(account_id, username, new_lang) lang_changed, player_lang = set_lang(account_id, username, new_lang)
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
if lang_changed: if lang_changed:
locale_change_successfully = get_locale("locale_change_successfully", new_lang) locale_change_successfully = get_locale("locale_change_successfully", new_lang)
toot_text = '@'+username + ' ' + locale_change_successfully + ' ' + new_lang toot_text = f'@{username} {locale_change_successfully} {new_lang}'
mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility)
@ -2431,7 +2204,7 @@ if __name__ == '__main__':
locale_not_changed = get_locale("locale_not_changed", player_lang) locale_not_changed = get_locale("locale_not_changed", player_lang)
toot_text = '@'+username + ' ' + new_lang + ' ' + locale_not_changed toot_text = f'@{username} {new_lang} {locale_not_changed}'
mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility) mastodon.status_post(toot_text, in_reply_to_id=status_id, visibility=visibility)
@ -2439,7 +2212,7 @@ if __name__ == '__main__':
toot_help() toot_help()
help_text = '@'+username help_text = f'@{username}'
help_panel = 'app/panel/help_panel.png' help_panel = 'app/panel/help_panel.png'
@ -2447,7 +2220,7 @@ if __name__ == '__main__':
mastodon.status_post(help_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) mastodon.status_post(help_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id})
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
else: else:
@ -2456,13 +2229,13 @@ if __name__ == '__main__':
username_lang = get_lang(username) username_lang = get_lang(username)
is_not_your_turn = get_locale("is_not_your_turn", username_lang) is_not_your_turn = get_locale("is_not_your_turn", username_lang)
toot_text = '@'+username + ' ' + is_not_your_turn + '\n' toot_text = f'@{username} {is_not_your_turn}\n'
else: else:
is_the_turn_of = get_locale("is_the_turn_of", player_lang) is_the_turn_of = get_locale("is_the_turn_of", player_lang)
toot_text = '@'+username + ' ' + is_the_turn_of + ' ' + playing_user + "\n" toot_text = f'@{username} {is_the_turn_of} {playing_user}\n'
toot_text += '\n' toot_text += '\n'
@ -2470,7 +2243,7 @@ if __name__ == '__main__':
chess_hashtag = get_locale("chess_hashtag", player_lang) chess_hashtag = get_locale("chess_hashtag", player_lang)
toot_text += game_name + ': ' + str(game_id) + ' ' + chess_hashtag + '\n' toot_text += f'{game_name}: {str(game_id)} {chess_hashtag}\n'
board = chess.Board(on_going_game) board = chess.Board(on_going_game)
@ -2490,8 +2263,11 @@ if __name__ == '__main__':
mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id})
update_replies(status_id, username, now) mastodon.notifications_dismiss(notification_id)
else:
mastodon.notifications_dismiss(notification_id)
i += 1 i += 1

Veure arxiu

@ -1,3 +1,4 @@
wheel>=0.36.2a
Mastodon.py>=1.5.1 Mastodon.py>=1.5.1
chess>=1.3.0 chess>=1.3.0
psycopg2-binary>=2.8.6 psycopg2-binary>=2.8.6