major update thingy. Clippy is happy, bugfixes, new "max_results" arg to /re_updateDiscord, etc.

This commit is contained in:
2025-08-09 14:37:15 +02:00
parent b4044956de
commit 0db5937c39
17 changed files with 97 additions and 86 deletions
+7 -6
View File
@@ -9,7 +9,8 @@ import toml
RE_DATA_POSTS: Final[str] = "posts"
CFG_DATA_RE: Final[str] = "reddit"
# TODO: add wipe arg
# TODO: add test-bot arg
class Bot:
args: dict[str, Any] = {"NO_RUST": True, "dev": True, "py": True, "port": 2920}
r_id: str | None = os.environ.get("ASSISTANT_R_ID")
@@ -21,7 +22,7 @@ class Bot:
useragent: str =\
f"{username} by u/RandomPersonDotExe aka u/Byte_Dice"\
if r_id == "YmZjr4zLr2qtHdpQXtj0sBOOdJzrXQ"\
if r_id == "YmZjr4zLr2qtHdpQXtj0sBOOdJzrXQ" or r_id == "Q-eBDGS8sFHlUCi9kpBepQ"\
else f"{username} (Original program by u/RandomPersonDotExe aka u/Byte_Dice)"
if password is None:
@@ -37,16 +38,13 @@ class Bot:
password = self.password,
user_agent = self.useragent
)
self.sr_list: list[str] = ["bytedicetesting"]
self.sr_list: list[str] = []
self.sr = None
self.data_f: TextIOWrapper | None = None
self.data: dict[str, Any] = {}
self.flairs: list[str] = []
self.aliases: dict[str, list[str]] = {}
async def initialize(self):
self.sr = await self.r.subreddit("+".join(self.sr_list))
async def set_args(self, args: dict[str, Any]):
self.args = args
@@ -67,5 +65,8 @@ class Bot:
self.fetch_limit = new_cfg[CFG_DATA_RE]["fetch_limit"]
self.flairs = new_cfg[CFG_DATA_RE]["search_flairs"]
self.aliases = new_cfg[CFG_DATA_RE]["aliases"]
self.sr_list = new_cfg[CFG_DATA_RE]["subreddits"]
self.sr = await self.r.subreddit("+".join(self.sr_list))
init_lang(new_cfg["general"]["lang"])
py_print("Successfully updated the configs!")
return True
+4 -6
View File
@@ -7,17 +7,13 @@ import bot as botPy
import py_data
import py_websocket
async def main():
sys.stdout.reconfigure(encoding="utf-8") # type: ignore
py_print("Creating Reddit bot...")
bot = botPy.Bot()
await bot.initialize()
py_print(f"Successfully created Reddit bot: {await bot.r.user.me()}")
# args is supposed to be undefined.
# args and lang_name are supposed to be undefined.
# It gets defined in Rust.
try:
await bot.set_args(args) # type: ignore
@@ -47,7 +43,9 @@ async def main():
if data_retries == 5 and not rd:
raise Exception("Couldn't read re_data.json: File doesn't exist")
py_print("Successfully read data!")
py_print("Successfully read all data!")
py_print(f"Successfully created Reddit bot: {await bot.r.user.me()}")
if not bot.args["py"]:
py_print("Connecting to local websocket...")
+23 -23
View File
@@ -9,40 +9,33 @@ import bot as botPy
from macros import *
async def add_new_posts(bot: botPy.Bot, max_age: int) -> bool:
async def add_new_posts(bot: botPy.Bot, max_age: int, max_results: int) -> bool:
check_emoji = emoji.emojize(":check_mark_button:")
cross_emoji = emoji.emojize(":cross_mark:")
py_print("Fetching posts...")
posts = await fetch_posts_with_flair(bot, bot.flairs)
posts = await fetch_posts_with_flair(bot, bot.flairs, max_age, max_results)
py_print("Evaluating posts...")
added_posts = 0
without_media = 0
not_added = 0
old_posts = 0
for post in posts:
media = has_media(post)
details = get_post_details(post)
media_urls = "\n ".join(media[3])
media_urls = "\n ".join(details.media_urls)
media_check = check_emoji if details.media_type is not None else cross_emoji
if bot.args["dev"]:
py_print(
f"\n{post.title}",
f"\n {post.shortlink}"
f"\n {check_emoji if media[0] else cross_emoji} Media ({media[1]}) [{media[2]}]",
f"\n{details.title}",
f"\n {details.url}"
f"\n {media_check} Media ({details.media_type}) [{len(details.media_urls)}]",
f"\n {media_urls}\n"
)
details = get_post_details(post)
now = int(time.time())
if now - details.date_unix > max_age and max_age > 0:
old_posts += 1
continue
if not media[0]:
if details.media_type is not None:
without_media += 1
continue
@@ -57,25 +50,32 @@ async def add_new_posts(bot: botPy.Bot, max_age: int) -> bool:
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"{not_added} are removed or already existed, " +
f"and {old_posts} were older than the max age threshold.")
f" {not_added} are removed or already existed, ")
py_data.write_data(bot)
return True
async def fetch_posts_with_flair(bot: botPy.Bot, flair_names: list[str]) -> list[models.Submission]:
async def fetch_posts_with_flair(
bot: botPy.Bot,
flair_names: list[str],
max_age_secs: int,
max_results: int
) -> 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)
f"flair:{flair_names[0].replace(" ", "_")}" if len(flair_names) == 1\
else " OR ".join(f"flair:{flair.replace(" ", "_")}" for flair in flair_names)
if bot.sr is None: return []
# ~36 OG-art posts per week, round limit to 50, 75 or 100
async for post in bot.sr.search(f"{flair_names_str}", sort="new", limit=bot.fetch_limit):
now = int(time.time())
# ~20 OG-art posts per week, round limit to 50, 75 or 100 for 2 subreddits
async for post in bot.sr.search(f"{flair_names_str}", sort="new", limit=max_results):
if now - int(post.created_utc) > max_age_secs and max_age_secs > 0: continue
posts.append(post)
return posts