From eeeefb51b6d3e1a72707eb187af7a2fdbd8c894b Mon Sep 17 00:00:00 2001 From: spla Date: Fri, 20 Nov 2020 09:42:19 +0100 Subject: [PATCH] Added link to game in games list --- mastochess.py | 92 ++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 76 insertions(+), 16 deletions(-) diff --git a/mastochess.py b/mastochess.py index 03e7ef3..d352f86 100644 --- a/mastochess.py +++ b/mastochess.py @@ -101,6 +101,8 @@ def get_notification_data(): visibility_lst = [] + url_lst = [] + search_text = ['fi','mou','nova','jocs'] conn = None @@ -115,7 +117,7 @@ def get_notification_data(): like_text = "%"+search_text[i]+"%" - select_query = "select account_id, id, text, visibility, created_at from statuses where text like (%s) and created_at + interval '60 minutes' > now() - interval '5 minutes'" + select_query = "select account_id, id, text, visibility, url, created_at from statuses where text like (%s) and 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, (like_text, str(bot_id))) @@ -139,11 +141,13 @@ def get_notification_data(): elif row[3] == 3: visibility_lst.append('direct') + url_lst.append(row[4]) + i += 1 cur.close() - return (account_id_lst, status_id_lst, text_lst, visibility_lst) + return (account_id_lst, status_id_lst, text_lst, visibility_lst, url_lst) except (Exception, psycopg2.DatabaseError) as error: @@ -233,6 +237,8 @@ def current_games(): game_status_lst = [] + game_link_lst = [] + try: conn = None @@ -241,7 +247,7 @@ def current_games(): cur = conn.cursor() - cur.execute("select white_user, black_user, chess_status from games where not finished") + cur.execute("select white_user, black_user, chess_status, chess_link from games where not finished") rows = cur.fetchall() @@ -259,9 +265,11 @@ def current_games(): game_status_lst.append(row[2]) + game_link_lst.append(row[3]) + cur.close() - return (player1_name_lst, player2_name_lst, game_status_lst) + return (player1_name_lst, player2_name_lst, game_status_lst, game_link_lst) except (Exception, psycopg2.DatabaseError) as error: @@ -369,7 +377,47 @@ def check_games(): conn.close() -def new_game(): +def get_status_url(toot_id): + + toot_id = str(toot_id.id) + + try: + + conn = None + + conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + select_query = "select uri from statuses where id=(%s)" + + cur.execute(select_query, (toot_id,)) + + row = cur.fetchone() + + if row != None: + + toot_url = row[0] + + else: + + toot_url = '' + + cur.close() + + return toot_url + + except (Exception, psycopg2.DatabaseError) as error: + + sys.exit(error) + + finally: + + if conn is not None: + + conn.close() + +def new_game(toot_url): try: @@ -385,9 +433,9 @@ def new_game(): cur = conn.cursor() - insert_query = 'INSERT INTO games(created_at, white_user, chess_game, chess_status, waiting, updated_at) VALUES (%s, %s, %s, %s, %s, %s) ON CONFLICT DO NOTHING' + insert_query = 'INSERT INTO games(created_at, white_user, chess_game, chess_status, waiting, updated_at, chess_link) VALUES (%s, %s, %s, %s, %s, %s, %s) ON CONFLICT DO NOTHING' - cur.execute(insert_query, (now, username, board_game, game_status, waiting, now)) + cur.execute(insert_query, (now, username, board_game, game_status, waiting, now, toot_url)) insert_query = 'INSERT INTO stats(created_at, white_user) VALUES (%s,%s) ON CONFLICT DO NOTHING' @@ -407,7 +455,7 @@ def new_game(): conn.close() -def update_game(board_game): +def update_game(board_game, toot_url): try: @@ -419,7 +467,7 @@ def update_game(board_game): cur = conn.cursor() - cur.execute("update games set chess_game=(%s), updated_at=(%s) where game_id=(%s)", (board_game, now, game_id,)) + cur.execute("update games set chess_game=(%s), chess_link=(%s), updated_at=(%s) where game_id=(%s)", (board_game, toot_url, now, game_id,)) conn.commit() @@ -834,7 +882,7 @@ if __name__ == '__main__': bot_id = get_bot_id() - account_id_lst, status_id_lst, text_lst, visibility_lst = get_notification_data() + account_id_lst, status_id_lst, text_lst, visibility_lst, url_lst = get_notification_data() i = 0 while i < len(account_id_lst): @@ -867,6 +915,8 @@ if __name__ == '__main__': visibility = visibility_lst[i] + status_url = url_lst[i] + if query_word != "jocs": is_playing, game_id, white_user, black_user, on_going_game, waiting, playing_user = check_games() @@ -897,9 +947,11 @@ if __name__ == '__main__': image_id = mastodon.media_post(board_file, "image/png").id - mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) + toot_id = mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) - new_game() + toot_url = get_status_url(toot_id) + + new_game(toot_url) update_replies(status_id, username, now) @@ -1061,11 +1113,13 @@ if __name__ == '__main__': image_id = mastodon.media_post(board_file, "image/png").id - mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) + toot_id = mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility, media_ids={image_id}) + + toot_url = get_status_url(toot_id) board_game = board.fen() - update_game(board_game) + update_game(board_game, toot_url) game_moves = board.ply() @@ -1139,7 +1193,7 @@ if __name__ == '__main__': elif query_word == 'jocs': - player1_name_lst, player2_name_lst, game_status_lst = current_games() + player1_name_lst, player2_name_lst, game_status_lst, game_link_lst = current_games() if len(player1_name_lst) > 0: @@ -1156,10 +1210,16 @@ if __name__ == '__main__': toot_text += "\n" + player1_name_lst[i] + " / " + player2_name_lst[i] + " (en joc)" + "\n" - mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) + if game_link_lst[i] != None: + + toot_text += str(game_link_lst[i]) + "\n" i += 1 + mastodon.status_post(toot_text, in_reply_to_id=status_id,visibility=visibility) + + update_replies(status_id, username, now) + else: toot_text = "@"+username + " cap partida en joc" + "\n"