handling old posts & /bk_week_top

This commit is contained in:
2025-02-26 22:13:50 +01:00
parent 2423e62c06
commit 832d9afd8c
9 changed files with 194 additions and 52 deletions
+25 -2
View File
@@ -1,6 +1,7 @@
import os
import json
from typing import Final
import time
import bot as botPy
from macros import *
@@ -136,13 +137,35 @@ def set_approve_post(bot: botPy.Bot, approved: bool, url: str) -> bool:
def remove_post(bot: botPy.Bot, url: str, removed_by: str = "UNKNOWN", reason: str = "None") -> bool:
if url in bot.data[BK_WEEKLY]:
bot.data[BK_WEEKLY][url] = { "removed": True, "removed_by": removed_by, "remove_reason": reason }
weekly = bot.data[BK_WEEKLY]
if url in weekly:
weekly[url] = {
"removed": True,
"removed_by": removed_by,
"remove_reason": reason,
"post_data": { "date_unix": weekly[url]["post_data"]["date_unix"] }
}
return True
else:
return False
def remove_old_posts(bot: botPy.Bot, max_age: int) -> bool:
now = int(time.time())
weekly = bot.data[BK_WEEKLY]
remove: list[str] = []
for url, post in weekly.items():
if now - post["post_data"]["date_unix"] > max_age:
remove.append(url)
for key in remove:
weekly.pop(key)
return True
def set_vote_post(
bot: botPy.Bot,
url: str,
+16 -4
View File
@@ -2,13 +2,14 @@ import emoji
from asyncpraw import models
import asyncprawcore as prawcore
import asyncpraw.exceptions as exc
import time
import data
import bot as botPy
from macros import *
async def add_new_posts(bot: botPy.Bot) -> bool:
async def add_new_posts(bot: botPy.Bot, max_age: int) -> bool:
check_emoji = emoji.emojize(":check_mark_button:")
cross_emoji = emoji.emojize(":cross_mark:")
@@ -20,6 +21,7 @@ async def add_new_posts(bot: botPy.Bot) -> bool:
added_posts = 0
without_media = 0
not_added = 0
old_posts = 0
for post in posts:
media = has_media(post)
@@ -32,14 +34,23 @@ async def add_new_posts(bot: botPy.Bot) -> bool:
f"\n {check_emoji if media[0] else cross_emoji} Media ({media[1]}) [{media[2]}]",
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]:
without_media += 1
continue
post_added = False
post_added = data.add_post_to_data(
bot,
get_post_details(post)
details
)
if post_added: added_posts += 1
@@ -48,7 +59,8 @@ async def add_new_posts(bot: botPy.Bot) -> 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"and {not_added} weren't added because they are removed or already existed")
f"{not_added} are removed or already existed, " +
f"and {old_posts} were older than the max age threshold.")
return True
+2 -1
View File
@@ -75,12 +75,13 @@ async def json_to_func(v: dict, bot: botPy.Bot) -> dict:
match v["value"]:
case "update_data_file": r = data .write_data (bot)
case "add_new_posts": r = await posts.add_new_posts (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 "change_sr": r = await bot .change_sr (*v["args"])
case "stop_praw": r = await bot .stop ()
case _: value_supported = False