As the title says, I'm unable to find the function in PRAW to reply to a post on Reddit.
I've written a rather simple bot so far, which looks for new posts on a certain subreddit. My problem is that once finding the submission, I can't find how to post a reply to it.
It's unclear if by "submission" you mean a submission to reddit or if you're referencing a comment on a submission to reddit. If it's the former, you're looking for the Submission#add_comment method. If you have the submission bound to variable name s, then you can do the following:
s.add_comment('This is my comment. There are many like it, but this one is mine.')
If you're looking for how to reply to a comment, you want to use the Comment#reply method. If you have the comment bound to variable name c, then you can do the following:
c.reply("Here is a reply to your comment.")
As of October 2018, submission.add_comment() is no longer possible in PRAW.
The new call is submission.reply()
Related
i need to write code that will read messages from user and reply but i’ve got to use exactly this function: GetDialogs Request, not event method. please help, can't find any information about it
The request you mention, GetDialogsRequest, is part of what's known as "raw API". Both pages of the documentation contain examples. However, as noted in the first link, you probably want to use client.iter_dialogs (the link again contains an example):
# Print all dialog IDs and the title, nicely formatted
async for dialog in client.iter_dialogs():
print('{:>14}: {}'.format(dialog.id, dialog.title))
This may not be the request you're looking for though, as this returns a list of open conversations. Following the documentation would be a good way to understand what's happening and what you should use.
This is my first time coding, and as such I think my problem is most likely general confusion and difficulty with terms. I have the login function and the reply functions working on my bot, but I'm stuck at what command to use to narrow my bot's search-for-keyword range to a specific thread and or user, instead of an entire subreddit.
I've tried looking at the PRAW documentation and Build-A-Bot tutorials online, but I can't find any compatible commands in Python/PRAW to search a specific Redditor, comment, or subreddit thread.
This is the original command for PRAW that makes my bot search the subreddit for its key phrase:
for comment in r.subreddit('').comments(limit=25):
But I'm trying to hone it in on searching more specifically, so I tried this:
for comment in r.submission('#portion of the URL that has the submission ID in it').comments(limit=25):
But that just returns "TypeError: 'CommentForest' object is not callable."
I've also tried:
for comment in r.user('#Redditor name').comments(limit=25):
But that just returns "TypeError: 'User' object is not callable."
I have zero coding background and I'm actually having a lot of fun with Python thus far! I'm just stuck at this point. Any help and or suggestions would be appreciated!
I think what you want is redditor rather than user. From the praw docs:
# assume you have a Submission instance bound to variable `submission`
redditor1 = submission.author
print(redditor1.name) # Output: name of the redditor
# assume you have a Reddit instance bound to variable `reddit`
redditor2 = reddit.redditor('bboe')
print(redditor2.link_karma) # Output: bboe's karma
You may have already seen them, but the docs can be found here.
I'm using praw to write a bot for reddit. I already know how to use the get_comments() function to get all the comments in a subreddit. However, I would like to get the titles of all the posts in a subreddit, however, after going through the docs for praw, I could not find a function which does so.
I just want to go into a subreddit, and fetch all the titles of the posts, and then store them in an object.
Could someone tell me how I go around achieving this?
import praw
r=praw.Reddit('demo')
subreddit=r.get_subreddit('stackoverflow')
for submission in subreddit.get_hot(limit=10):
print submission.title
This information is available in the PRAW documentation.
Seems pretty late but anyways you can go through the official reddit api JSON responses format. From there you can see all the attributes that are available to you for a particular object.
Here's the github link for the reddit API
Edit: You can also use pprint(vars(object_name))
According to their documentation:
This should be enough to get the hottest new reddit submissions:
r = client.get(r'http://www.reddit.com/api/hot/', data=user_pass_dict)
But it doesn't and I get a 404 error. Am I getting the url for data request wrong?
http://www.reddit.com/api/login works though.
Your question specifically asks what you need to do to get the "hottest new" submissions. "Hottest new" doesn't really make sense as there is the "hot" view and a "new" view. The URLs for those two views are http://www.reddit.com/hot and http://www.reddit.com/new respectively.
To make those URLs more code-friendly, you can append .json to the end of the URL (any reddit URL for that matter) to get a json-representation of the data. For instance, to get the list of "hot" submissions make a GET request to http://www.reddit.com/hot.json.
For completeness, in your example, you attempt to pass in data=user_pass_dict. That's definitely not going to work the way you expect it to. While logging in is not necessary for what you want to do, if you happen to have need for more complicated use of reddit's API using python, I strongly suggest using PRAW. With PRAW you can iterate over the "hot" submissions via:
import praw
r = praw.Reddit('<REPLACE WITH A UNIQUE USER AGENT>')
for submission in r.get_frontpage():
# do something with the submission
print(vars(submission))
According to the docs, use /hot rather than /api/hot:
r = client.get(r'http://www.reddit.com/hot/', data=user_pass_dict)
I Have an IRC bot I'm working on, and one of the features I would like it to have is to take any link a person posts and use BeautifulSoup to parse that page. Now, I have the bot working, getting the messages people post, etc. But, how would I pull a link from the IRC message? Say someone says this:
Person: Check out http://www.site.com, it's cool!
How would I take the link out and assign it to a variable for later use, without pulling the other parts of the message?
I think it's something to do with regexs, but I'm not sure.
You will indeed need to use regular expressions.
There's a decent article with a regular expression for matching URLs and somewhat of a description of what it's doing at daring fireball.
You can look at how Django does it here.
Finally, Python's regular expression documentation may also be useful.
You are on the exact track to finish this. You gave yourself the answer with the last sentence of your question. You will use a regular expression with a capture group to get the url and from there you can parse/grab the page that the user has said in the irc.
This site may be of some use for you: http://www.regular-expressions.info/