Skip to content

Commit

Permalink
ban #vent, #vent post, #venting and cws of same name
Browse files Browse the repository at this point in the history
  • Loading branch information
penguinencounter committed Mar 15, 2024
1 parent ee33ad6 commit bdb49e9
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 21 deletions.
20 changes: 14 additions & 6 deletions cohost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
)
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -92,5 +92,5 @@ def main():
os.remove(".lock")


if __name__ == '__main__':
if __name__ == "__main__":
main()
53 changes: 41 additions & 12 deletions randomizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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
Expand All @@ -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()
Expand All @@ -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:
Expand All @@ -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}
Expand All @@ -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[/] "
Expand Down Expand Up @@ -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(
Expand All @@ -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),
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bdb49e9

Please sign in to comment.