I tried to implement asynchronous functions into a PRAW bot but I am definitely doing something wrong
async def replytopost(post, item, comments): if ("".join(item.lower().split()) in "".join(post.title.lower().split()) or "".join(item.lower().split()) in "".join(post.selftext.lower()).split()) and post.author != username and username not in [com.author for com in comments]: await post.reply(item) # if mentioned, comment the item, then stop searching return False return True async def replytocomment(comment, item): if "".join(item.lower().split()) in "".join(comment.body.lower().split()) and comment.author != username and username not in [com.author for com in comment.replies]: await comment.reply(item) return False return True async def main(): async for post in subreddit.h: #iterates over top 20 hot posts in subreddit print(post.title) if not post.stickied: #makes sure post isnt a pinned post comments = post.comments.list() for item in search.items(): #tests each item to see if the post mentions it if await replytopost(post, search[item], comments): break for comment in comments: #does the same thing but with comments for item in search.items(): try: if await replytocomment(comment, search[item]): break except AttributeError: pass if __name__ == '__main__': asyncio.run(main())
submitted by /u/Crate-Of-Loot
[link] [comments]
r/learnpython I tried to implement asynchronous functions into a PRAW bot but I am definitely doing something wrong async def replytopost(post, item, comments): if (“”.join(item.lower().split()) in “”.join(post.title.lower().split()) or “”.join(item.lower().split()) in “”.join(post.selftext.lower()).split()) and post.author != username and username not in [com.author for com in comments]: await post.reply(item) # if mentioned, comment the item, then stop searching return False return True async def replytocomment(comment, item): if “”.join(item.lower().split()) in “”.join(comment.body.lower().split()) and comment.author != username and username not in [com.author for com in comment.replies]: await comment.reply(item) return False return True async def main(): async for post in subreddit.h: #iterates over top 20 hot posts in subreddit print(post.title) if not post.stickied: #makes sure post isnt a pinned post comments = post.comments.list() for item in search.items(): #tests each item to see if the post mentions it if await replytopost(post, search[item], comments): break for comment in comments: #does the same thing but with comments for item in search.items(): try: if await replytocomment(comment, search[item]): break except AttributeError: pass if __name__ == ‘__main__’: asyncio.run(main()) submitted by /u/Crate-Of-Loot [link] [comments]
I tried to implement asynchronous functions into a PRAW bot but I am definitely doing something wrong
async def replytopost(post, item, comments): if ("".join(item.lower().split()) in "".join(post.title.lower().split()) or "".join(item.lower().split()) in "".join(post.selftext.lower()).split()) and post.author != username and username not in [com.author for com in comments]: await post.reply(item) # if mentioned, comment the item, then stop searching return False return True async def replytocomment(comment, item): if "".join(item.lower().split()) in "".join(comment.body.lower().split()) and comment.author != username and username not in [com.author for com in comment.replies]: await comment.reply(item) return False return True async def main(): async for post in subreddit.h: #iterates over top 20 hot posts in subreddit print(post.title) if not post.stickied: #makes sure post isnt a pinned post comments = post.comments.list() for item in search.items(): #tests each item to see if the post mentions it if await replytopost(post, search[item], comments): break for comment in comments: #does the same thing but with comments for item in search.items(): try: if await replytocomment(comment, search[item]): break except AttributeError: pass if __name__ == '__main__': asyncio.run(main())
submitted by /u/Crate-Of-Loot
[link] [comments]