Added language support to python. TODO: add language placeholders
This commit is contained in:
+3
-3
@@ -41,6 +41,7 @@ class Bot:
|
||||
self.sr = None
|
||||
self.data_f: TextIOWrapper = None
|
||||
self.data: dict = {}
|
||||
self.flairs: list[str] = []
|
||||
|
||||
async def initialize(self):
|
||||
self.sr = await self.r.subreddit("+".join(self.sr_list))
|
||||
@@ -58,13 +59,12 @@ class Bot:
|
||||
|
||||
async def update_cfg_str(self, new_cfg: str) -> bool:
|
||||
json_cfg = toml.loads(new_cfg)
|
||||
self.sr_list = json_cfg[CFG_DATA_RE]["subreddits"].split("+")
|
||||
self.sr = await self.r.subreddit("+".join(self.sr_list))
|
||||
self.fetch_limit = json_cfg[CFG_DATA_RE]["fetch_limit"]
|
||||
self.update_cfg(json_cfg)
|
||||
return True
|
||||
|
||||
async def update_cfg(self, new_cfg: dict) -> bool:
|
||||
self.sr_list = new_cfg[CFG_DATA_RE]["subreddits"].split("+")
|
||||
self.sr = await self.r.subreddit("+".join(self.sr_list))
|
||||
self.fetch_limit = new_cfg[CFG_DATA_RE]["fetch_limit"]
|
||||
self.flairs = new_cfg[CFG_DATA_RE]["search_flair"]
|
||||
return True
|
||||
+35
-1
@@ -1,5 +1,13 @@
|
||||
import json
|
||||
import os
|
||||
from printColors import PrintColors
|
||||
|
||||
|
||||
G_LANG_NAME: str = ""
|
||||
G_LANG: dict[str, str] = {}
|
||||
DATA_PATH_LANG: str = "./data/lang/"
|
||||
|
||||
|
||||
def py_print(*args):
|
||||
print(
|
||||
PrintColors.FG.blue + "Py",
|
||||
@@ -14,4 +22,30 @@ def py_error(*args):
|
||||
"-",
|
||||
" ".join(args) + PrintColors.Special.reset
|
||||
)
|
||||
quit()
|
||||
quit()
|
||||
|
||||
|
||||
def lang(k: str) -> str:
|
||||
t = G_LANG.get(k)
|
||||
if k is None: py_error(f"Key not found in language \"{G_LANG_NAME}\": {k}")
|
||||
return str(t)
|
||||
|
||||
|
||||
def init_lang(lang_name: str):
|
||||
global G_LANG, G_LANG_NAME
|
||||
G_LANG_NAME = lang_name
|
||||
|
||||
full_path = f"{DATA_PATH_LANG}{lang_name}.json"
|
||||
|
||||
if not os.path.exists(full_path):
|
||||
py_error(f"File for language \"{lang_name}\" ({lang_name}.json) not found!\n Hint: You can download official language files at https://github.com/ByteDice/ByteDiceAssistant in the data/langs/... folder")
|
||||
|
||||
with open(full_path, "r") as f:
|
||||
str_data = f.read()
|
||||
|
||||
try:
|
||||
json_data = json.loads(str_data)
|
||||
except json.JSONDecodeError as e:
|
||||
py_error(f"Failed to parse JSON for language \"{lang_name}\":\n{e}")
|
||||
|
||||
G_LANG = json_data
|
||||
+5
-2
@@ -19,9 +19,12 @@ async def main():
|
||||
|
||||
# args is supposed to be undefined.
|
||||
# It gets defined in Rust.
|
||||
try: await bot.set_args(args)
|
||||
try:
|
||||
await bot.set_args(args)
|
||||
init_lang(lang_name)
|
||||
except NameError:
|
||||
py_print("No command args found from Rust. Don't worry though, we have backup in place.")
|
||||
py_print("No command args or language name found from Rust. Don't worry though, we have backup in place.")
|
||||
init_lang("en")
|
||||
|
||||
if bot.args["dev"]:
|
||||
py_print("ARGS:", str(bot.args))
|
||||
|
||||
+7
-3
@@ -14,7 +14,7 @@ async def add_new_posts(bot: botPy.Bot, max_age: int) -> bool:
|
||||
cross_emoji = emoji.emojize(":cross_mark:")
|
||||
|
||||
py_print("Fetching posts...")
|
||||
posts = await fetch_posts_with_flair(bot, "Original Art")
|
||||
posts = await fetch_posts_with_flair(bot, bot.flairs)
|
||||
|
||||
py_print("Evaluating posts...")
|
||||
|
||||
@@ -65,11 +65,15 @@ async def add_new_posts(bot: botPy.Bot, max_age: int) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
async def fetch_posts_with_flair(bot: botPy.Bot, flair_name: str) -> list[models.Submission]:
|
||||
async def fetch_posts_with_flair(bot: botPy.Bot, flair_names: list[str]) -> list[models.Submission]:
|
||||
posts: list[models.Submission] = []
|
||||
|
||||
flair_names_str = \
|
||||
f"flair:{flair_names[0]}" if len(flair_names) == 1\
|
||||
else " OR ".join(f"flair:{flair}" for flair in flair_names)
|
||||
|
||||
# ~36 OG-art posts per week, round limit to 50, 75 or 100
|
||||
async for post in bot.sr.search(f"flair:\"{flair_name}\"", sort="new", limit=bot.fetch_limit):
|
||||
async for post in bot.sr.search(f"{flair_names_str}", sort="new", limit=bot.fetch_limit):
|
||||
posts.append(post)
|
||||
|
||||
return posts
|
||||
|
||||
+11
-10
@@ -7,6 +7,7 @@ import bot as botPy
|
||||
import data
|
||||
import posts
|
||||
import cmds
|
||||
import macros
|
||||
|
||||
ws_global = None
|
||||
is_connected = False
|
||||
@@ -74,16 +75,16 @@ async def json_to_func(v: dict, bot: botPy.Bot) -> dict:
|
||||
r = False
|
||||
|
||||
match v["value"]:
|
||||
case "update_data_file": r = data .write_data (bot)
|
||||
case "respond_mentions": r = await cmds .respond_to_mention(bot)
|
||||
case "add_new_posts": r = await posts.add_new_posts (bot, *v["args"])
|
||||
case "add_post_url": r = await posts.add_post_url (bot, *v["args"])
|
||||
case "remove_post_url": r = data .remove_post (bot, *v["args"])
|
||||
case "set_approve_post": r = data .set_approve_post (bot, *v["args"])
|
||||
case "set_vote_post": r = data .set_vote_post (bot, *v["args"])
|
||||
case "remove_old_posts": r = data .remove_old_posts (bot, *v["args"])
|
||||
case "update_cfg": r = await bot .update_cfg_str (*v["args"])
|
||||
case "stop_praw": r = await bot .stop ()
|
||||
case "update_data_file": r = data .write_data (bot)
|
||||
case "respond_mentions": r = await cmds .respond_to_mention(bot)
|
||||
case "add_new_posts": r = await posts .add_new_posts (bot, *v["args"])
|
||||
case "add_post_url": r = await posts .add_post_url (bot, *v["args"])
|
||||
case "remove_post_url": r = data .remove_post (bot, *v["args"])
|
||||
case "set_approve_post": r = data .set_approve_post (bot, *v["args"])
|
||||
case "set_vote_post": r = data .set_vote_post (bot, *v["args"])
|
||||
case "remove_old_posts": r = data .remove_old_posts (bot, *v["args"])
|
||||
case "update_cfg": r = await bot .update_cfg_str (*v["args"])
|
||||
case "stop_praw": r = await bot .stop ()
|
||||
case _: value_supported = False
|
||||
|
||||
print_result = v["print"]
|
||||
|
||||
Reference in New Issue
Block a user