PRAW: Comment Submitter's Username
Asked Answered
T

2

6

I'm developing a reddit bot that needs to know which user submitted a comment.

According to the PRAW API wrapper docs, there's no specific way to get the username of a Comment object's author. Ideally I could directly get the username back. If that's not possible, is there a way to get the fullname of the author and then convert it to a username?

Thorn answered 29/12, 2013 at 6:42 Comment(0)
A
13

I'm a maintainer for PRAW. Where does it say that you cannot get the username of a Comment objects author? Because that is incorrect and needs to be fixed.

Anyway, a Comment has a author attribute, which is a Redditor instance of the author.

import praw

r = praw.Reddit(UNIQUE_AND_DESCRIPTIVE_USERAGENT)
submission = r.get_submission("http://www.reddit.com/r/redditdev/comments/16m0uu/praw_20_is_coming_release_in_2_days/")
comment = submission.comments[0]
author = comment.author  # This returns a ``Redditor`` object.
print(author.name)  # The username
Adelric answered 5/1, 2014 at 21:11 Comment(4)
I was using the readthedocs.org documentation, and nowhere in there was a mention of how to get the author of the comment. I also checked the github source (somewhat briefly) and couldn't find anything there. Thanks for the reply! I've been stalled on this project for a while now.Thorn
Yeah, the code overview section only shows the classes and their methods, but none of the attributes for each class.Brana
I'm another one who has had trouble with this. I can't find any documentation on class attributes for PRAW. To people who use the internet as a resource (nearly everyone), lack of documentation for an attribute indicates that it does not exist.Freedman
Class attributes in PRAW are created dynamically based on the return from Reddit, then converted into proper types. This allows PRAW to instantly use new attributes as they become available, no need to update. It's a pretty nifty feature, but it means that creating a static resource via docstrings is going to be inaccurate and misleading eventually. This is why the documentation shows and recommend using standard python introspection to see what attributes are available. praw.readthedocs.org/en/v2.1.16/pages/writing_a_bot.htmlAdelric
R
2

Can't comment as don't have enough reputation. @Humus mentioned in his comment that it was no where mentioned in the PRAW readthedocs.org documentation. There is a simple workaround.We can use dir(object_name) to get a list of the attributes for that object. Then it is just a guessing game thereafter.

Edit: You can also use pprint(vars(object_name))

Raul answered 7/9, 2016 at 8:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.