control commit

pull/1/head
Captain Arepa 2 years ago
parent 4a1d2cb22a
commit d9a5f398f4

@ -27,25 +27,9 @@ def init_bot(config):
def execute_bot(config, freq):
print("execute bot...")
print("execute bot... %s" % freq)
bot = init_bot(config)
match freq:
case "ALL":
print("all time bot")
rows = bot.get_all_blocks()
case "DAILY":
print("daily bot")
rows = bot.get_todays_blocks()
case "WEEKLY":
print("daily bot")
rows = bot.get_this_weeks_blocks()
case "LATEST":
print("latest bot")
rows = bot.get_latest_block()
case _:
print("ALL TIME")
rows = bot.get_all_blocks()
print(rows)
# bot.post_blocks_report(freq)
def main():
@ -54,7 +38,7 @@ def main():
print("Getting config...")
config = load_user_config()
if config is not None:
execute_bot(config, sys.argv[0])
execute_bot(config, sys.argv[1])
else:
print("Bot couldn't be instantiated. Exiting...")
sys.exit(1)

@ -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

@ -1,7 +1,13 @@
{
"mk_url": "url-to-misskey.instance",
"mk_url": "https://misskey-instance.tld/api/",
"mk_username": "misskey-username",
"mk_token": "misskey-access-token",
"mk_token": "misskey-token",
"message": {
"visibility": "public",
"text": "Notification text mentioning your misskey username -> @%s :some_emoji_here:",
"localOnly": true,
"no_blocks_text": "In case you want to emphasize your block virginity lmao :hehe:"
},
"use_ssh": true,
"tunnel": {
"user": "ssh-user",

Loading…
Cancel
Save