|
|
|
@ -1,5 +1,7 @@
|
|
|
|
|
import psycopg2
|
|
|
|
|
import requests
|
|
|
|
|
from sshtunnel import SSHTunnelForwarder
|
|
|
|
|
from psycopg2.extras import RealDictCursor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BotInstance:
|
|
|
|
@ -9,6 +11,11 @@ class BotInstance:
|
|
|
|
|
|
|
|
|
|
use_ssh = True
|
|
|
|
|
|
|
|
|
|
msg_text = None
|
|
|
|
|
msg_visibility = None
|
|
|
|
|
msg_local_only = True
|
|
|
|
|
msg_no_block_text = None
|
|
|
|
|
|
|
|
|
|
ssh_address = None
|
|
|
|
|
ssh_key = None
|
|
|
|
|
ssh_user = None
|
|
|
|
@ -28,6 +35,12 @@ class BotInstance:
|
|
|
|
|
self.mk_token = config["mk_token"]
|
|
|
|
|
|
|
|
|
|
self.use_ssh = config["use_ssh"]
|
|
|
|
|
|
|
|
|
|
self.msg_text = config["message"]["text"]
|
|
|
|
|
self.msg_visibility = config["message"]["visibility"]
|
|
|
|
|
self.msg_local_only = config["message"]["localOnly"]
|
|
|
|
|
self.msg_no_block_text = config["message"]["no_block_text"]
|
|
|
|
|
|
|
|
|
|
self.ssh_address = config["tunnel"]["address"]
|
|
|
|
|
self.ssh_port = config["tunnel"]["port"]
|
|
|
|
|
self.ssh_key = config["tunnel"]["key"]
|
|
|
|
@ -43,6 +56,7 @@ class BotInstance:
|
|
|
|
|
def __repr__(self):
|
|
|
|
|
return "Bot instance!"
|
|
|
|
|
|
|
|
|
|
## Database Conn, Server and Cursor
|
|
|
|
|
def start_tunnel(self):
|
|
|
|
|
tunnel = None
|
|
|
|
|
try:
|
|
|
|
@ -66,7 +80,8 @@ class BotInstance:
|
|
|
|
|
'user': self.db_user,
|
|
|
|
|
'password': self.db_password,
|
|
|
|
|
'host': self.db_host,
|
|
|
|
|
'port': server.local_bind_port
|
|
|
|
|
'port': server.local_bind_port,
|
|
|
|
|
'cursor_factory': RealDictCursor
|
|
|
|
|
}
|
|
|
|
|
print("Server local port {0}", format(server.local_bind_port))
|
|
|
|
|
conn = psycopg2.connect(**params)
|
|
|
|
@ -102,6 +117,9 @@ class BotInstance:
|
|
|
|
|
print("Fetch all!")
|
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
for row in rows:
|
|
|
|
|
print(row)
|
|
|
|
|
|
|
|
|
|
print("Closing cursor...")
|
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
@ -111,8 +129,8 @@ class BotInstance:
|
|
|
|
|
print("Error: ", e)
|
|
|
|
|
return rows
|
|
|
|
|
|
|
|
|
|
def get_todays_blocks(self):
|
|
|
|
|
print("today's blocks!")
|
|
|
|
|
def get_yesterdays_blocks(self):
|
|
|
|
|
print("Get today's blocks")
|
|
|
|
|
rows = None
|
|
|
|
|
try:
|
|
|
|
|
print("Get server tunnel...")
|
|
|
|
@ -150,7 +168,7 @@ class BotInstance:
|
|
|
|
|
return rows
|
|
|
|
|
|
|
|
|
|
def get_this_weeks_blocks(self):
|
|
|
|
|
print("today's blocks!")
|
|
|
|
|
print("Get this week's blocks")
|
|
|
|
|
rows = None
|
|
|
|
|
try:
|
|
|
|
|
print("Get server tunnel...")
|
|
|
|
@ -187,8 +205,46 @@ class BotInstance:
|
|
|
|
|
print("Error: ", e)
|
|
|
|
|
return rows
|
|
|
|
|
|
|
|
|
|
def get_this_months_blocks(self):
|
|
|
|
|
print("Get this month's blocks")
|
|
|
|
|
rows = None
|
|
|
|
|
try:
|
|
|
|
|
print("Get server tunnel...")
|
|
|
|
|
server = self.start_tunnel()
|
|
|
|
|
server.start()
|
|
|
|
|
|
|
|
|
|
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 " \
|
|
|
|
|
"from blocking b " \
|
|
|
|
|
"join \"user\" this on this.id = b.\"blockeeId\" " \
|
|
|
|
|
"join \"user\" that on that.id = b.\"blockerId\" " \
|
|
|
|
|
"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)
|
|
|
|
|
|
|
|
|
|
print("Fetch all!")
|
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
print("Closing cursor...")
|
|
|
|
|
cursor.close()
|
|
|
|
|
|
|
|
|
|
print("Closing DB connection...")
|
|
|
|
|
conn.close()
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("Error: ", e)
|
|
|
|
|
return rows
|
|
|
|
|
|
|
|
|
|
def get_latest_block(self):
|
|
|
|
|
print("today's blocks!")
|
|
|
|
|
print("Get latest block")
|
|
|
|
|
rows = None
|
|
|
|
|
try:
|
|
|
|
|
print("Get server tunnel...")
|
|
|
|
@ -215,7 +271,7 @@ class BotInstance:
|
|
|
|
|
cursor.execute(sql)
|
|
|
|
|
|
|
|
|
|
print("Fetch all!")
|
|
|
|
|
rows = cursor.fetchone()
|
|
|
|
|
rows = cursor.fetchall()
|
|
|
|
|
|
|
|
|
|
print("Closing cursor...")
|
|
|
|
|
cursor.close()
|
|
|
|
@ -225,3 +281,62 @@ class BotInstance:
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print("Error: ", e)
|
|
|
|
|
return rows
|
|
|
|
|
|
|
|
|
|
# Misskey Post
|
|
|
|
|
def post_note(self, content=""):
|
|
|
|
|
# Do all the things to create the message
|
|
|
|
|
message = self.msg_text % self.mk_user
|
|
|
|
|
note_content = "%s\n%s" % (message, content)
|
|
|
|
|
|
|
|
|
|
# The request paylod
|
|
|
|
|
payload = {
|
|
|
|
|
"visibility": self.msg_visibility,
|
|
|
|
|
"text": note_content,
|
|
|
|
|
"localOnly": self.msg_local_only,
|
|
|
|
|
"i": self.mk_token
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Send the request
|
|
|
|
|
create_note_request = requests.post(self.mk_url + "notes/create", json=payload)
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|