diff --git a/cohost.py b/cohost.py index 6006b39..364fb88 100644 --- a/cohost.py +++ b/cohost.py @@ -233,7 +233,7 @@ def get_author_classic(pid: int): author = list( filter( lambda x: x["href"].startswith("/api/v1/project/") - and not x["href"].endswith("/posts"), + and not x["href"].endswith("/posts"), basic_info["_links"], ) ) @@ -409,9 +409,13 @@ def create_share( return known_pid -def edit_share(pid: int, handle: str, share_of: int, - blocks: list[Union[MarkdownBlock, AskBlock, AttachmentBlock]], - tags: list[str]): +def edit_share( + pid: int, + handle: str, + share_of: int, + blocks: list[Union[MarkdownBlock, AskBlock, AttachmentBlock]], + tags: list[str], +): model = EditPostModel( projectHandle=handle, postId=pid, @@ -423,7 +427,7 @@ def edit_share(pid: int, handle: str, share_of: int, headline="", postState=1, shareOfPostId=share_of, - ) + ), ) dumped = model.model_dump(mode="json") resp = _trpc_post("posts.update", dumped) @@ -465,7 +469,11 @@ def list_asks(handle: str) -> list[AskModel]: def ask_reject(ask_id: str): - _try_with_backoff("https://cohost.org/api/v1/trpc/asks.reject?batch=1", method="POST", json={"0": ask_id}) + _try_with_backoff( + "https://cohost.org/api/v1/trpc/asks.reject?batch=1", + method="POST", + json={"0": ask_id}, + ) def next_id() -> int: diff --git a/manager.py b/manager.py index a770b9a..8afe388 100644 --- a/manager.py +++ b/manager.py @@ -71,7 +71,7 @@ def parse(content: str, context: AskModel): def main(): logging.basicConfig(level=logging.INFO, handlers=[RichHandler()]) - max_duration = 120 # seconds + max_duration = 120 # seconds while os.path.exists(".lock"): time.sleep(1) max_duration -= 1 @@ -82,7 +82,7 @@ def main(): if not yesno: rp("[bold bright_red]not logged in!![/]") exit(1) - with open('.lock', 'w') as f: + with open(".lock", "w") as f: pass try: asks = list_asks(POST_TO) @@ -92,5 +92,5 @@ def main(): os.remove(".lock") -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/randomizer.py b/randomizer.py index 910e39a..f18fba0 100644 --- a/randomizer.py +++ b/randomizer.py @@ -10,7 +10,7 @@ from rich.logging import RichHandler import cohost -from settings import POST_TO +from settings import POST_TO, banned_tags, banned_cws log = logging.getLogger("randomizer") j2env = Environment(loader=FileSystemLoader(""), autoescape=select_autoescape()) @@ -23,8 +23,14 @@ def effective_tags(topmost: cohost.PostModel, share: cohost.PostModel): return set(topmost.tags) & set(share.tags) +def check_ban(model: cohost.PostModel) -> bool: + return bool(set(map(lambda x: x.lower(), model.tags)) & banned_tags) or bool( + set(map(lambda x: x.lower(), model.cws)) & banned_cws + ) + + def main(): - max_duration = 120 # seconds + max_duration = 120 # seconds while os.path.exists(".lock"): time.sleep(1) max_duration -= 1 @@ -37,7 +43,7 @@ def main(): rp("[bold bright_red]not logged in!![/]") exit(1) - with open('.lock', 'w') as f: + with open(".lock", "w") as f: pass try: latest = cohost.next_id() @@ -58,7 +64,9 @@ def main(): verify_count = -1 while 1: if max_att <= 0: - log.critical("ran out of attempts or there is no more content to look at") + log.critical( + "ran out of attempts or there is no more content to look at" + ) exit(1) choiced = random.randint(last, latest) if choiced in ban_list: @@ -77,12 +85,27 @@ def main(): continue if post_info.post.effectiveAdultContent: log.info( - f"SKIP {describe}: [red]adult content[/]", extra={"markup": True} + f"SKIP {describe}: [red]adult content[/]", + extra={"markup": True}, ) continue if post_info.post.postingProject.handle in ban_list: - log.info(f"SKIP {describe}: [red]banlist[/]", extra={"markup": True}) + log.info( + f"SKIP {describe}: [red]banlist[/]", extra={"markup": True} + ) + continue + if check_ban(post_info.post): + log.info( + f"SKIP {describe}: [red]tag ban[/]", extra={"markup": True} + ) continue + if post_info.post.shareTree: + if check_ban(post_info.post.shareTree[0]): + log.info( + f"SKIP {describe}: [red]tag ban[/]", extra={"markup": True} + ) + continue + if not post_info.post.canShare: log.info( f"SKIP {describe}: [red]can't share[/]", extra={"markup": True} @@ -102,7 +125,9 @@ def main(): ) continue if post_info.post.shareTree: - eft = list(effective_tags(post_info.post.shareTree[0], post_info.post)) + eft = list( + effective_tags(post_info.post.shareTree[0], post_info.post) + ) if not eft: log.info( f"SKIP {describe}: [red]no effective tags[/] " @@ -145,10 +170,12 @@ def main(): pid=post_info.post.postId, handle=post_info.post.postingProject.handle, uid=post_info.post.postingProject.projectId, - timestamp=datetime.now(tz=timezone.utc).strftime("%a %m %B, %Y %H:%M:%S %Z"), + timestamp=datetime.now(tz=timezone.utc).strftime( + "%a %m %B, %Y %H:%M:%S %Z" + ), total_count=latest - last, percentage=f"{100.0 / (latest - last):.3f}", - delete_url="[[delete_url]]" + delete_url="[[delete_url]]", ) cohost.switchn(POST_TO) # required for the locking for some reason. pid = cohost.create_share( @@ -163,7 +190,7 @@ def main(): permitted = [post_info.post.postingProject.handle] if post_info.post.shareTree: permitted.append(post_info.post.shareTree[0].postingProject.handle) - perm_string = ''.join(map(lambda x: f'&permitted={x}', permitted)) + perm_string = "".join(map(lambda x: f"&permitted={x}", permitted)) new_content = the_template.render( original_href=post_info.post.singlePostPageUrl, typeof=cohost.typeof(post_info.post), @@ -174,10 +201,12 @@ def main(): pid=post_info.post.postId, handle=post_info.post.postingProject.handle, uid=post_info.post.postingProject.projectId, - timestamp=datetime.now(tz=timezone.utc).strftime("%a %m %B, %Y %H:%M:%S %Z"), + timestamp=datetime.now(tz=timezone.utc).strftime( + "%a %m %B, %Y %H:%M:%S %Z" + ), total_count=latest - last, percentage=f"{100.0 / (latest - last):.3f}", - delete_url=f"https://penguinencounter.github.io/cohost-randombot/del.html?target={pid}{perm_string}" + delete_url=f"https://penguinencounter.github.io/cohost-randombot/del.html?target={pid}{perm_string}", ) cohost.edit_share( pid, diff --git a/settings.py b/settings.py index b11d4b6..245c06f 100644 --- a/settings.py +++ b/settings.py @@ -16,3 +16,8 @@ COHOST_COOKIE = environ.get("COHOST_COOKIE") SCRATCHPAD_HANDLE = environ.get("SCRATCHPAD_HANDLE") POST_TO = environ.get("POST_TO") + +banned_tags = {"vent", "vent post", "venting", "randomizer/disallow"} +banned_cws = banned_tags | { + "randomizer/disallow" +} # intentional overlap until I get more tags