uhhhh. Update sized update

This commit is contained in:
2025-02-13 17:44:25 +01:00
parent 069d615272
commit 2a10508534
9 changed files with 98 additions and 46 deletions
+8 -3
View File
@@ -57,14 +57,17 @@ class PostData:
}
def read_data(bot: botPy.Bot):
def read_data(bot: botPy.Bot) -> bool:
# Intentionally unreadable >:]
data_path = os.path.abspath(os.path.join(os.path.join(os.getcwd(), "data")))
try:
if os.path.isfile(data_path + "\\reddit_data.json"):
bot.data_f = open(data_path + "\\reddit_data.json", "r+")
except FileNotFoundError:
else:
if not bot.args["py"]:
return False
py_print("reddit_data.json not found, creating new from preset...")
with open(data_path + "\\reddit_data_preset.json", "r") as f:
data_preset_json = json.load(f)
@@ -83,6 +86,8 @@ def read_data(bot: botPy.Bot):
if not bot.data["file_created_correctly"]:
raise Exception("reddit_data.json file wasn't created properly. Delete the file and retry.")
return True
def write_data(bot: botPy.Bot) -> bool:
+16 -9
View File
@@ -27,21 +27,28 @@ async def main():
if bot.args["dev"]:
py_print("ARGS:", str(bot.args))
py_print("Reading data...")
data.read_data(bot)
dr = data.read_data(bot)
data_retries = 0
while not dr:
data_retries += 1
time.sleep(1)
py_print(f"Failed to read data: File doesn't exist yet. Retrying (#{data_retries}/5)...")
dr = data.read_data(bot)
if data_retries == 5 and not dr:
raise Exception("Couldn't read reddit_data.json: File doesn't exist")
py_print("Successfully read data!")
if not bot.args["py"]:
py_print("Connecting to local websocket...")
await py_websocket.websocket_client(bot)
""" ws_thread = threading.Thread(target=py_websocket.run_thread, args=(bot,))
ws_thread.start() """
while not py_websocket.is_connected:
py_print("Awaiting connection...")
time.sleep(1)
continue
await py_websocket.send_message("[Connection test] Hello from Python!")
# ws_thread = threading.Thread(target=py_websocket.run_thread, args=(bot,))
# ws_thread.start()
await bot.stop()
+3 -2
View File
@@ -43,7 +43,7 @@ async def add_new_posts(bot: botPy.Bot):
if post_added: added_posts += 1
else: not_added += 1
py_print(f"Sucessfully fetched {len(posts)} posts.\n" +
py_print(f"Successfully fetched {len(posts)} posts.\n" +
f" Out of which were {added_posts} added.\n" +
f" {without_media} had no media, " +
f"and {not_added} weren't added because they are removed or already existed")
@@ -114,11 +114,12 @@ def get_post_details(post: models.Submission) -> data.PostData:
)
async def add_post_url(bot, url: str) -> bool:
async def add_post_url(bot, url: str, approve: bool) -> bool:
result, post = await from_url(bot, url)
if not result:
return result
post_data = get_post_details(post)
post_data.approved_by_human = approve
return data.add_post_to_data(bot, post_data, True)
+14 -6
View File
@@ -2,7 +2,7 @@ import websockets
import asyncio
import json
from macros import py_print
from macros import *
import bot as botPy
import data
import posts
@@ -18,10 +18,13 @@ async def send_message(message: str):
async def websocket_client(bot: botPy.Bot):
global ws_global, is_connected
async with websockets.connect(f"ws://127.0.0.1:{bot.args["port"]}") as ws:
port = bot.args["port"]
async with websockets.connect(f"ws://127.0.0.1:{port}") as ws:
ws_global = ws
is_connected = True
py_print(f"Connected webSocket server on ws://127.0.0.1:{bot.args["port"]}")
py_print(f"Connected webSocket server on ws://127.0.0.1:{port}")
await send_message("[Connection test] Hello from Python!")
while True:
response = await ws.recv()
@@ -52,7 +55,8 @@ async def json_to_func(v: dict, bot: botPy.Bot) -> dict:
if bot.args["dev"]: py_print("JSON is not a dictionary or does not include \"type\" and \"value\" keys.")
return
if v["type"] != "function":
if bot.args["dev"]: py_print(f"Type \"{v['type']}\" is not supported.")
v_type = v["type"]
if bot.args["dev"]: py_print(f"Type \"{v_type}\" is not supported.")
return
value_supported = True
@@ -62,11 +66,15 @@ async def json_to_func(v: dict, bot: botPy.Bot) -> dict:
case "update_data_file": result = result_json(data.write_data(bot))
case "add_post_url": result = result_json(await posts.add_post_url(bot, *v["args"]))
case "set_approve_post": result = result_json(data.set_approve_post(bot, *v["args"]))
case "stop_praw": result = result_json(bot.stop())
case "stop_praw": result = result_json(await bot.stop())
case _: value_supported = False
if not isinstance(result.get("value"), bool):
py_print("Result JSON is invalid:", str(result))
if bot.args["dev"] and not value_supported:
py_print(f"Value {v['value']} is not supported")
val = v["value"]
py_print(f"Value \"{val}\" is not supported")
return result