|
|
|
@ -56,7 +56,7 @@ class BotInstance:
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return "Bot instance!"
|
|
|
|
|
|
|
|
|
|
## Database Conn, Server and Cursor
|
|
|
|
|
# Database Conn, Server and Cursor
|
|
|
|
|
def start_tunnel(self):
|
|
|
|
|
tunnel = None
|
|
|
|
|
try:
|
|
|
|
@ -90,6 +90,8 @@ class BotInstance:
|
|
|
|
|
# print("Error: ", e)
|
|
|
|
|
return conn, cursor
|
|
|
|
|
|
|
|
|
|
# Database Queries
|
|
|
|
|
|
|
|
|
|
def get_all_blocks(self):
|
|
|
|
|
rows = None
|
|
|
|
|
try:
|
|
|
|
@ -100,7 +102,6 @@ class BotInstance:
|
|
|
|
|
print("Get DBconnection and cursor...")
|
|
|
|
|
conn, cursor = self.get_db_cursor(server)
|
|
|
|
|
|
|
|
|
|
print("SQL statement:\n")
|
|
|
|
|
sql = "select concat(that.username,'@',that.host ) as blocker, " \
|
|
|
|
|
"this.username as blockee, " \
|
|
|
|
|
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
|
|
|
|
@ -109,7 +110,6 @@ class BotInstance:
|
|
|
|
|
"join \"user\" that on that.id = b.\"blockerId\" " \
|
|
|
|
|
"where this.\"username\" like \'{0}\' " \
|
|
|
|
|
"and this.host is null;".format(self.mk_user)
|
|
|
|
|
print(sql)
|
|
|
|
|
|
|
|
|
|
print("Execute SQL statement...")
|
|
|
|
|
cursor.execute(sql)
|
|
|
|
@ -117,9 +117,6 @@ class BotInstance:
|
|
|
|
|
print("Fetch all!")
|
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
for row in rows:
|
|
|
|
|
print(row)
|
|
|
|
|
|
|
|
|
|
print("Closing cursor...")
|
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
@ -140,7 +137,6 @@ class BotInstance:
|
|
|
|
|
print("Get DBconnection and cursor...")
|
|
|
|
|
conn, cursor = self.get_db_cursor(server)
|
|
|
|
|
|
|
|
|
|
print("SQL statement:\n")
|
|
|
|
|
sql = "select concat(that.username,'@',that.host ) as blocker, " \
|
|
|
|
|
"this.username as blockee, " \
|
|
|
|
|
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
|
|
|
|
@ -150,7 +146,6 @@ class BotInstance:
|
|
|
|
|
"where this.\"username\" like \'{0}\' " \
|
|
|
|
|
"and b.\"createdAt\" between current_date - interval '1 day' and current_date " \
|
|
|
|
|
"and this.host is null;".format(self.mk_user)
|
|
|
|
|
print(sql)
|
|
|
|
|
|
|
|
|
|
print("Execute SQL statement...")
|
|
|
|
|
cursor.execute(sql)
|
|
|
|
@ -178,7 +173,6 @@ class BotInstance:
|
|
|
|
|
print("Get DBconnection and cursor...")
|
|
|
|
|
conn, cursor = self.get_db_cursor(server)
|
|
|
|
|
|
|
|
|
|
print("SQL statement:\n")
|
|
|
|
|
sql = "select concat(that.username,'@',that.host ) as blocker, " \
|
|
|
|
|
"this.username as blockee, " \
|
|
|
|
|
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
|
|
|
|
@ -188,7 +182,6 @@ class BotInstance:
|
|
|
|
|
"where this.\"username\" like \'{0}\' " \
|
|
|
|
|
"and b.\"createdAt\" between current_date - interval '7 day' and current_date " \
|
|
|
|
|
"and this.host is null;".format(self.mk_user)
|
|
|
|
|
print(sql)
|
|
|
|
|
|
|
|
|
|
print("Execute SQL statement...")
|
|
|
|
|
cursor.execute(sql)
|
|
|
|
@ -216,7 +209,6 @@ class BotInstance:
|
|
|
|
|
print("Get DBconnection and cursor...")
|
|
|
|
|
conn, cursor = self.get_db_cursor(server)
|
|
|
|
|
|
|
|
|
|
print("SQL statement:\n")
|
|
|
|
|
sql = "select concat(that.username,'@',that.host ) as blocker, " \
|
|
|
|
|
"this.username as blockee, " \
|
|
|
|
|
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
|
|
|
|
@ -226,7 +218,6 @@ class BotInstance:
|
|
|
|
|
"where this.\"username\" like \'{0}\' " \
|
|
|
|
|
"and b.\"createdAt\" between current_date - interval '1 month' and current_date " \
|
|
|
|
|
"and this.host is null;".format(self.mk_user)
|
|
|
|
|
print(sql)
|
|
|
|
|
|
|
|
|
|
print("Execute SQL statement...")
|
|
|
|
|
cursor.execute(sql)
|
|
|
|
@ -254,7 +245,6 @@ class BotInstance:
|
|
|
|
|
print("Get DBconnection and cursor...")
|
|
|
|
|
conn, cursor = self.get_db_cursor(server)
|
|
|
|
|
|
|
|
|
|
print("SQL statement:\n")
|
|
|
|
|
sql = "select concat(that.username,'@',that.host ) as blocker, " \
|
|
|
|
|
"this.username as blockee, " \
|
|
|
|
|
"to_date(to_char(b.\"createdAt\", 'YYYY/MM/DD'), 'YYYY/MM/DD') as block_date " \
|
|
|
|
@ -265,7 +255,6 @@ class BotInstance:
|
|
|
|
|
"and this.host is null " \
|
|
|
|
|
"order by b.\"createdAt\" desc " \
|
|
|
|
|
"limit 1;".format(self.mk_user)
|
|
|
|
|
print(sql)
|
|
|
|
|
|
|
|
|
|
print("Execute SQL statement...")
|
|
|
|
|
cursor.execute(sql)
|
|
|
|
@ -282,6 +271,51 @@ class BotInstance:
|
|
|
|
|
print("Error: ", e)
|
|
|
|
|
return rows
|
|
|
|
|
|
|
|
|
|
# Reports
|
|
|
|
|
def parse_block_list(self, result, bullet=""):
|
|
|
|
|
block_line = ""
|
|
|
|
|
if len(result) == 0:
|
|
|
|
|
block_line += self.msg_no_block_text + "\n"
|
|
|
|
|
else:
|
|
|
|
|
for row in result:
|
|
|
|
|
blocker = row["blocker"]
|
|
|
|
|
block_date = row["block_date"]
|
|
|
|
|
block_line += "%s%s (%s)\n" % (bullet, blocker, block_date)
|
|
|
|
|
|
|
|
|
|
return block_line
|
|
|
|
|
|
|
|
|
|
def block_reports(self, report_type):
|
|
|
|
|
all_time_blocks = self.get_all_blocks()
|
|
|
|
|
latest_block = self.get_latest_block()
|
|
|
|
|
|
|
|
|
|
match report_type:
|
|
|
|
|
case "DAILY":
|
|
|
|
|
result = self.get_yesterdays_blocks()
|
|
|
|
|
report_header = "\nYesterday's blocks: %d\n" % len(result)
|
|
|
|
|
case "WEEKLY":
|
|
|
|
|
result = self.get_this_weeks_blocks()
|
|
|
|
|
report_header = "\nThis week's blocks: %d\n" % len(result)
|
|
|
|
|
case "MONTHLY":
|
|
|
|
|
result = self.get_this_months_blocks()
|
|
|
|
|
report_header = "\nThis month's blocks: %d\n" % len(result)
|
|
|
|
|
case "ALL_TIME":
|
|
|
|
|
result = all_time_blocks
|
|
|
|
|
report_header = "\nAll time blocks: %d\n" % len(result)
|
|
|
|
|
case "STATISTICS":
|
|
|
|
|
result = None
|
|
|
|
|
report_header = ""
|
|
|
|
|
case _:
|
|
|
|
|
result = None
|
|
|
|
|
report_header = ""
|
|
|
|
|
|
|
|
|
|
content = self.parse_block_list(result, "* ")
|
|
|
|
|
|
|
|
|
|
msg_latest = "\nYou were last blocked by %s\n" % self.parse_block_list(latest_block)
|
|
|
|
|
msg_all_time = "You have been blocked by %d people so far! :kekw:\n" % len(all_time_blocks)
|
|
|
|
|
|
|
|
|
|
msg_content = report_header + content + msg_latest + msg_all_time
|
|
|
|
|
self.post_note(msg_content)
|
|
|
|
|
|
|
|
|
|
# Misskey Post
|
|
|
|
|
def post_note(self, content=""):
|
|
|
|
|
# Do all the things to create the message
|
|
|
|
@ -302,41 +336,3 @@ class BotInstance:
|
|
|
|
|
# Some validation here, gotta add a logfile later smh
|
|
|
|
|
if create_note_request.status_code != 200:
|
|
|
|
|
print("Error: " + create_note_request.json()["error"]["message"], file=None)
|
|
|
|
|
|
|
|
|
|
def post_blocks_report(self, freq):
|
|
|
|
|
result = None
|
|
|
|
|
all_time_blocks = self.get_all_blocks()
|
|
|
|
|
msg_all_time = "- All time blocks %d " % len(all_time_blocks)
|
|
|
|
|
match freq:
|
|
|
|
|
case "ALL":
|
|
|
|
|
msg_prepend = "- Your all-time blocks are:\n"
|
|
|
|
|
result = all_time_blocks
|
|
|
|
|
case "DAILY":
|
|
|
|
|
msg_prepend = "- Your blocks for today are:\n"
|
|
|
|
|
result = self.get_yesterdays_blocks()
|
|
|
|
|
case "WEEKLY":
|
|
|
|
|
msg_prepend = "- Your blocks for this week are:\n"
|
|
|
|
|
result = self.get_this_weeks_blocks()
|
|
|
|
|
case "LATEST":
|
|
|
|
|
msg_prepend = "- Your latest block is: \n"
|
|
|
|
|
result = self.get_latest_block()
|
|
|
|
|
case "SUMMARY":
|
|
|
|
|
msg_prepend = ""
|
|
|
|
|
print("WIP/TODO: Elaborate some sort of summary here...")
|
|
|
|
|
case _:
|
|
|
|
|
msg_prepend = "You have new blocks (?)"
|
|
|
|
|
|
|
|
|
|
block_note_content = self.parse_block_list(result, msg_prepend)
|
|
|
|
|
self.post_note(block_note_content)
|
|
|
|
|
|
|
|
|
|
def parse_block_list(self, result, prepend_msg):
|
|
|
|
|
block_line = prepend_msg
|
|
|
|
|
if len(result) == 0:
|
|
|
|
|
block_line += self.msg_no_block_text + "\n"
|
|
|
|
|
else:
|
|
|
|
|
for row in result:
|
|
|
|
|
blocker = row["blocker"]
|
|
|
|
|
block_date = row["block_date"]
|
|
|
|
|
block_line += "* %s (%s)\n" % (blocker, block_date)
|
|
|
|
|
|
|
|
|
|
return block_line
|
|
|
|
|