Recoded the whole get_notifications_data module. Much faster now

This commit is contained in:
spla 2020-11-17 17:59:03 +01:00
pare e25ae8eecc
commit aecb8abbad

Veure arxiu

@ -91,12 +91,7 @@ def get_bot_id():
conn.close() conn.close()
def get_new_notifications(): def get_user_domain(account_id):
#############################################################################################################################
# check if any new notifications by comparing newest notification datetime with the last query datetime
last_notifications = [] # to store last 20 'Mention' type notitifications for our bot
try: try:
@ -106,43 +101,19 @@ def get_new_notifications():
cur = conn.cursor() cur = conn.cursor()
cur.execute("select * from notifications where activity_type = 'Mention' and account_id = (%s) order by created_at desc limit 1", (bot_id,)) cur.execute("select username, domain from accounts where id=(%s)", (account_id,))
row = cur.fetchone() row = cur.fetchone()
if row != None: if row != None:
last_notif_created_at = row[3] username = row[0]
last_notif_created_at = last_notif_created_at + timedelta(hours=2) domain = row[1]
last_notif_created_at = last_notif_created_at.strftime("%d/%m/%Y, %H:%M:%S")
if last_posted != "":
if last_notif_created_at <= last_posted:
cur.close()
conn.close()
print("No new notifications")
sys.exit(0)
cur.execute("select * from notifications where activity_type = 'Mention' and account_id = (%s) order by created_at desc limit 20", (bot_id,))
rows = cur.fetchall()
if rows != None:
for row in rows:
last_notifications.append(row)
cur.close() cur.close()
return last_notifications return (username, domain)
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
@ -158,54 +129,57 @@ def get_notification_data():
try: try:
account_id_lst = []
status_id_lst = []
text_lst = []
visibility_lst = []
search_text = ['nova','mou','fi']
conn = None conn = None
conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432") conn = psycopg2.connect(database = mastodon_db, user = mastodon_db_user, password = "", host = "/var/run/postgresql", port = "5432")
cur = conn.cursor() cur = conn.cursor()
cur.execute("select username, domain from accounts where id=(%s)", (user_id,)) i=0
row = cur.fetchone() while i < len(search_text):
if row != None: like_text = "%"+search_text[i]+"%"
username = row[0] select_query = "select account_id, id, text, visibility, created_at from statuses where text like (%s) and created_at + interval '60 minutes' > now() - interval '60 minutes'"
select_query += " and id=any (select status_id from mentions where account_id=(%s)) order by created_at asc"
domain = row[1] cur.execute(select_query, (like_text, str(bot_id)))
cur.execute("select status_id from mentions where id = (%s)", (activity_id,)) rows = cur.fetchall()
row = cur.fetchone() for row in rows:
if row != None: account_id_lst.append(row[0])
status_id = row[0] status_id_lst.append(row[1])
cur.execute("select text, visibility from statuses where id = (%s)", (status_id,)) text_lst.append(row[2])
row = cur.fetchone() if row[3] == 0:
visibility_lst.append('public')
elif row[3] == 1:
visibility_lst.append('unlisted')
elif row[3] == 2:
visibility_lst.append('private')
elif row[3] == 3:
visibility_lst.append('direct')
if row != None: i += 1
text = row[0]
visibility = row[1]
cur.close() cur.close()
if visibility == 0: return (account_id_lst, status_id_lst, text_lst, visibility_lst)
visibility = 'public'
elif visibility == 1:
visibility = 'unlisted'
elif visibility == 2:
visibility = 'private'
elif visibility == 3:
visibility = 'direct'
cur.close()
return (username, domain, status_id, text, visibility)
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
@ -796,34 +770,18 @@ if __name__ == '__main__':
create_dir() create_dir()
last_posted = last_notification()
bot_id = get_bot_id() bot_id = get_bot_id()
last_notifications = get_new_notifications() account_id_lst, status_id_lst, text_lst, visibility_lst = get_notification_data()
last_notifications = sorted(last_notifications)
####################################################################
i = 0 i = 0
while i < len(last_notifications): while i < len(account_id_lst):
user_id = last_notifications[i][5] account_id = account_id_lst[i]
activity_id = last_notifications[i][0]
n_created_at = last_notifications[i][3]
n_created_at = n_created_at + timedelta(hours=2) username, domain = get_user_domain(account_id)
n_created_datetime = n_created_at.strftime("%d/%m/%Y, %H:%M:%S") status_id = status_id_lst[i]
if n_created_datetime <= last_posted:
i +=1
continue
username, domain, status_id, text, visibility = get_notification_data()
replied = check_replies(status_id) replied = check_replies(status_id)
@ -841,8 +799,12 @@ if __name__ == '__main__':
# listen them or not # listen them or not
text = text_lst[i]
reply, query_word, moving = replying() reply, query_word, moving = replying()
visibility = visibility_lst[i]
is_playing, game_id, white_user, black_user, on_going_game, waiting, playing_user = check_games() is_playing, game_id, white_user, black_user, on_going_game, waiting, playing_user = check_games()
if game_id == '': if game_id == '':