added preset commands and MORE DEBUG INFO LES GOOOOO

This commit is contained in:
2025-02-05 22:11:53 +01:00
parent 99b30f4cfa
commit b178968940
10 changed files with 117 additions and 38 deletions
+5 -17
View File
@@ -65,7 +65,7 @@ def read_data(bot: botPy.Bot):
bot.data_f = open(data_path + "\\reddit_data.json", "r+")
except FileNotFoundError:
print("reddit_data.json not found, creating new from preset...")
py_print("reddit_data.json not found, creating new from preset...")
with open(data_path + "\\reddit_data.json", "w") as f:
f.write(open(data_path + "\\reddit_data_preset.json", "r").read())
@@ -84,28 +84,16 @@ def write_data(bot: botPy.Bot):
bot.data_f.truncate()
def update_post_in_data(bot: botPy.Bot, new_data: PostData):
def add_post_to_data(bot: botPy.Bot, new_data: PostData) -> bool:
if new_data.url not in bot.data[BK_WEEKLY]:
bot.data[BK_WEEKLY][new_data.url] = new_data.to_json()
py_print(f"Added post \"{new_data.url}\"")
return
return True
elif "removed" in bot.data[BK_WEEKLY][new_data.url]:
py_print(f"Failed to add post \"{new_data.url}\": Removed flag is True.")
return
return False
else:
py_print(f"Failed to add post \"{new_data.url}\": Already exists.")
def remove_old_posts(bot: botPy.Bot, max_age_unix: int):
for post in bot.data[BK_WEEKLY]:
if post["date_unix"] > max_age_unix:
dict(bot.data[BK_WEEKLY]).pop(post)
def clear_posts_without_media(bot: botPy.Bot):
for post in bot.data[BK_WEEKLY]:
post_data = post["post_data"]
if post_data["media_type"] == None and len(post_data["media_urls"]) == 0:
dict(bot.data[BK_WEEKLY]).pop(post)
return False
+3
View File
@@ -13,6 +13,9 @@ def main():
py_print("Reading data...")
data.read_data(bot)
# TEMPORARY, will be replaced with a
# schedule and/or Discord bot command
# config file or options ^
posts.add_new_posts(bot)
+17 -2
View File
@@ -12,8 +12,13 @@ def add_new_posts(bot: botPy.Bot, debug_print: bool = False):
posts = reddit.fetch_posts_with_flair(bot, "Original Art")
py_print("Evaluating posts...\n")
added_posts = 0
without_media = 0
not_added = 0
for post in posts:
media = reddit.has_media(post)
media_urls = "\n ".join(media[3])
if debug_print: print(
@@ -22,8 +27,12 @@ def add_new_posts(bot: botPy.Bot, debug_print: bool = False):
f"\n {check_emoji if media[0] else cross_emoji} Media ({media[1]}) [{media[2]}]",
f"\n {media_urls}\n"
)
if not media[0]:
without_media += 1
continue
data.update_post_in_data(
post_added = data.add_post_to_data(
bot,
data.PostData(
post.shortlink,
@@ -36,6 +45,12 @@ def add_new_posts(bot: botPy.Bot, debug_print: bool = False):
)
)
py_print(f"Sucessfully fetched {len(posts)} posts")
if post_added: added_posts += 1
else: not_added += 1
py_print(f"Sucessfully 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")
data.write_data(bot)
+1
View File
@@ -0,0 +1 @@
# communicate with rust.